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 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/instance.c | 10 +++++++ src/instance.h | 1 + 3 files changed, 91 insertions(+), 2 deletions(-) (limited to 'src') 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; } diff --git a/src/instance.c b/src/instance.c index 148f71d..ca80a45 100644 --- a/src/instance.c +++ b/src/instance.c @@ -123,6 +123,16 @@ int l2_instance_del_instance(struct l2_instance *inst) return INSTANCE_SUCCESS; } +int l2_instance_rename_instance(struct l2_instance *inst, const char *newname) +{ + char *nn = strdup(newname); + if (!nn) return INSTANCE_ERRNO; + + free((char *)inst->name); + inst->name = nn; + return INSTANCE_SUCCESS; +} + /* internal utilities */ int l2_instance__load_all_from_object(json_t *obj) diff --git a/src/instance.h b/src/instance.h index e7e5943..1e60df8 100644 --- a/src/instance.h +++ b/src/instance.h @@ -26,5 +26,6 @@ int l2_instance_save_all(void); int l2_instance_add_instance(const struct l2_instance *inst); int l2_instance_del_instance(struct l2_instance *inst); +int l2_instance_rename_instance(struct l2_instance *inst, const char *newname); #endif /* include guard */ -- cgit v1.2.3-70-g09d2