From 28750f38e6fa3f661729155d0855599c7f431cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 22 Jul 2024 15:42:38 +0200 Subject: [PATCH] feat: provider sync cmd --- src/cli/command/provider.c | 44 +++++++++++++++++++++++++++++++------- src/config.c | 12 +++++++++++ src/config.h | 3 +++ 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/cli/command/provider.c b/src/cli/command/provider.c index befbec2..844e5fe 100644 --- a/src/cli/command/provider.c +++ b/src/cli/command/provider.c @@ -1,4 +1,5 @@ #include "../../config.h" +#include #include #include #include @@ -29,17 +30,11 @@ int provider_info(int argc, char **argv) { 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]; - } - } - + struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]); config_free(cfg); if (!provider) { - printf("No such provider '%s'", argv[1]); + printf("error: no such provider '%s'", argv[1]); return 1; } @@ -52,6 +47,35 @@ int provider_info(int argc, char **argv) { return 0; } +int provider_sync(int argc, char **argv) { + if (argc == 1) { + printf("usage: gimi provider sync [--all] [name]"); + return 1; + } + + struct gimi_config *cfg = config_read(); + if (!cfg) + return 1; + + struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]); + config_free(cfg); + + char command[100]; + snprintf(command, sizeof(command), "git remote add %s %s", provider->name, + provider->ssh); + + int ret = system(command); + if (ret != 0) { + printf("error: failed to sync provider '%s' with git's exit code %d.", + provider->name, ret); + } else { + printf("info: provider '%s' has been successfully synced with git.", + provider->name); + } + + return 0; +} + int cli_command_provider(int argc, char **argv) { if (argc == 1) { return providers(); @@ -67,5 +91,9 @@ int cli_command_provider(int argc, char **argv) { return provider_info(argc, argv); } + if (strcmp(subcommand, "sync") == 0) { + return provider_sync(argc, argv); + } + return 0; } diff --git a/src/config.c b/src/config.c index 3c0c66e..e66db50 100644 --- a/src/config.c +++ b/src/config.c @@ -67,3 +67,15 @@ void config_free(struct gimi_config *cfg) { free(cfg->providers); free(cfg); } + +struct gimi_config_provider *config_find_provider(struct gimi_config *cfg, + char *name) { + struct gimi_config_provider *provider = NULL; + for (int i = 0; i < cfg->providers_size; i++) { + if (strcmp(cfg->providers[i]->name, name) == 0) { + provider = cfg->providers[i]; + } + } + + return provider; +} diff --git a/src/config.h b/src/config.h index fc9b940..80612de 100644 --- a/src/config.h +++ b/src/config.h @@ -13,3 +13,6 @@ struct gimi_config { struct gimi_config *config_read(); void config_free(struct gimi_config *cfg); + +struct gimi_config_provider *config_find_provider(struct gimi_config *cfg, + char *name);