From 838b1269ac2eba5110dece8c17b468343a4f73ee Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Tue, 26 Dec 2023 04:53:54 -0600 Subject: implement remaining commands (but badly) TODO: refactor and sanitize inputs more --- src/cmd-instance.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 2 deletions(-) (limited to 'src/cmd-instance.c') diff --git a/src/cmd-instance.c b/src/cmd-instance.c index e187b7d..6982eb3 100644 --- a/src/cmd-instance.c +++ b/src/cmd-instance.c @@ -210,7 +210,7 @@ unsigned cmd_instance_remove(struct l2_context_node *ctx, char **args) char *removepath = NULL; for (char **cur = args; *cur; ++cur) { - if (!strcmp("--remove", *cur)) { + if (!strcmp("--delete", *cur)) { flag_remove = true; } else if (!strcmp("--id", *cur)) { flag_byid = true; @@ -317,10 +317,88 @@ cleanup: unsigned cmd_instance_list(struct l2_context_node *ctx, char **args) { -return CMD_RESULT_SUCCESS; + L2_UNUSED(ctx); + + if (*args) { + fprintf(stderr, "fatal: Unknown argument '%s'.\n", *args); + return CMD_RESULT_FAIL; + } + + int res = l2_instance_load_all(); + + if (res != INSTANCE_SUCCESS) { + fprintf(stderr, "fatal: Failed to load instances. (%d)\n", res); + return CMD_RESULT_FAIL; + } + + if (l2_state.instance_head) { + char idstr[UUID_STRLEN + 1]; + for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) { + l2_uuid_to_string(&inst->uuid, idstr); + printf("- %s (%s) at '%s'\n", inst->name, idstr, inst->path); + } + } else { + fputs("There are no instances.\n", stderr); + } + + return CMD_RESULT_SUCCESS; } unsigned cmd_instance_rename(struct l2_context_node *ctx, char **args) { + int res; + char *oldname = NULL; + char *newname = NULL; + struct l2_instance *to_rename = NULL; + + if (*args) { + fprintf(stderr, "fatal: Unknown argument '%s'.\n", *args); + return CMD_RESULT_FAIL; + } + + res = l2_instance_load_all(); + + if (res != INSTANCE_SUCCESS) { + fprintf(stderr, "fatal: Failed to load instances. (%d)\n", res); + return CMD_RESULT_FAIL; + } + + for (struct l2_context_node *cur = ctx; cur; cur = cur->next) { + if (!strcmp("oldname", cur->node->name)) { + oldname = cur->value; + } else if (!strcmp("newname", cur->node->name)) { + newname = cur->value; + } + } + + if (!(oldname && newname)) { /* TODO: sanitize newname like in "instance add" */ + fputs("fatal: Invalid usage.\n", stderr); + return CMD_RESULT_FAIL; + } + + for (struct l2_instance *cur = l2_state.instance_head; cur; cur = cur->next) { + if (!strcmp(cur->name, oldname)) { + to_rename = cur; + } else if (!strcmp(cur->name, newname)) { + fprintf(stderr, "fatal: An instance by the name '%s' already exists.\n", newname); + return CMD_RESULT_FAIL; + } + } + + if (!to_rename) { + fprintf(stderr, "fatal: No instance found by the name '%s'.\n", oldname); + return CMD_RESULT_FAIL; + } + + if ((res = l2_instance_rename_instance(to_rename, newname)) != INSTANCE_SUCCESS) { + fprintf(stderr, "fatal: Failed to rename the instance. (%d)\n", res); + return CMD_RESULT_FAIL; + } + + if ((res = l2_instance_save_all()) != INSTANCE_SUCCESS) { + fprintf(stderr, "fatal: Failed to save instances. (%d)\n", res); + return CMD_RESULT_FAIL; + } + return CMD_RESULT_SUCCESS; } -- cgit v1.2.3-70-g09d2