From aebaf1faf297feb9b10d32c31a210436140fd92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 22 Jul 2024 15:21:03 +0200 Subject: [PATCH] feat: provider cli command --- src/cli/cli.c | 5 +++ src/cli/command/provider.c | 71 ++++++++++++++++++++++++++++++++++++++ src/cli/command/provider.h | 1 + src/config.c | 2 ++ src/config.h | 1 + 5 files changed, 80 insertions(+) create mode 100644 src/cli/command/provider.c create mode 100644 src/cli/command/provider.h diff --git a/src/cli/cli.c b/src/cli/cli.c index eddee24..0802e3b 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -1,6 +1,7 @@ #include "../gimi_constants.h" #include "command/config.h" #include "command/init.h" +#include "command/provider.h" #include #include @@ -26,5 +27,9 @@ int cli_handle(int argc, char **argv) { return cli_command_config(argc, argv); } + if (strcmp(sub_command, "provider") == 0) { + return cli_command_provider(argc, argv); + } + return 0; } diff --git a/src/cli/command/provider.c b/src/cli/command/provider.c new file mode 100644 index 0000000..befbec2 --- /dev/null +++ b/src/cli/command/provider.c @@ -0,0 +1,71 @@ +#include "../../config.h" +#include +#include +#include +#include + +int providers() { + struct gimi_config *cfg = config_read(); + if (!cfg) + return 1; + + for (int i = 0; i < cfg->providers_size; i++) { + struct gimi_config_provider *provider = cfg->providers[i]; + printf("%s\n", provider->name); + } + + config_free(cfg); + + return 0; +} + +int provider_info(int argc, char **argv) { + if (argc == 1) { + printf("usage: gimi provider info "); + return 1; + } + + struct gimi_config *cfg = config_read(); + if (!cfg) + return 1; + + struct gimi_config_provider *provider = NULL; + for (int i = 0; i < cfg->providers_size; i++) { + if (strcmp(cfg->providers[i]->name, argv[1]) == 0) { + provider = cfg->providers[i]; + } + } + + config_free(cfg); + + if (!provider) { + printf("No such provider '%s'", argv[1]); + return 1; + } + + printf("name: %s\n", provider->name); + printf("ssh: %s\n", provider->ssh); + printf("primary: %d\n", provider->primary); + + free(provider); + + return 0; +} + +int cli_command_provider(int argc, char **argv) { + if (argc == 1) { + return providers(); + } + + // remove "provider" from args + argc -= 1; + argv += 1; + + char *subcommand = argv[0]; + + if (strcmp(subcommand, "info") == 0) { + return provider_info(argc, argv); + } + + return 0; +} diff --git a/src/cli/command/provider.h b/src/cli/command/provider.h new file mode 100644 index 0000000..d9abdd7 --- /dev/null +++ b/src/cli/command/provider.h @@ -0,0 +1 @@ +int cli_command_provider(int argc, char **argv); diff --git a/src/config.c b/src/config.c index 85eb7db..3c0c66e 100644 --- a/src/config.c +++ b/src/config.c @@ -41,6 +41,8 @@ struct gimi_config *config_read() { (struct gimi_config_provider *)malloc( sizeof(struct gimi_config_provider)); + provider->name = strdup(key); + toml_datum_t ssh = toml_string_in(toml_provider, "ssh"); provider->ssh = strdup(ssh.u.s); diff --git a/src/config.h b/src/config.h index 8a80308..fc9b940 100644 --- a/src/config.h +++ b/src/config.h @@ -1,6 +1,7 @@ #include struct gimi_config_provider { + char *name; char *ssh; bool primary; };