diff --git a/src/cli/command/ci.c b/src/cli/command/ci.c index 9eb6e19..ae62530 100644 --- a/src/cli/command/ci.c +++ b/src/cli/command/ci.c @@ -104,6 +104,8 @@ char *generate_sourcehut(struct gimi_config *cfg, int generate(int argc, char **argv) { struct gimi_config *cfg = config_read(); + ASSERT_CONFIG_EXIST(cfg); + struct gimi_config_provider *provider; if (argc == 2) { diff --git a/src/cli/command/config.c b/src/cli/command/config.c index e6f2294..04619b9 100644 --- a/src/cli/command/config.c +++ b/src/cli/command/config.c @@ -6,11 +6,7 @@ int cli_command_config(int argc, char **argv) { struct gimi_config *cfg = config_read(); - if (!cfg) { - printf( - "error: missing gimi config, initialize it using gimi init command."); - return 1; - } + ASSERT_CONFIG_EXIST(cfg); for (int i = 0; i < cfg->providers_size; i++) { struct gimi_config_provider *provider = cfg->providers[i]; diff --git a/src/cli/command/provider.c b/src/cli/command/provider.c index 79d9713..a02eb7a 100644 --- a/src/cli/command/provider.c +++ b/src/cli/command/provider.c @@ -7,8 +7,7 @@ int providers() { struct gimi_config *cfg = config_read(); - if (!cfg) - return 1; + ASSERT_CONFIG_EXIST(cfg); for (int i = 0; i < cfg->providers_size; i++) { struct gimi_config_provider *provider = cfg->providers[i]; @@ -27,8 +26,7 @@ int provider_info(int argc, char **argv) { } struct gimi_config *cfg = config_read(); - if (!cfg) - return 1; + ASSERT_CONFIG_EXIST(cfg); struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]); config_free(cfg); @@ -54,8 +52,7 @@ int provider_sync(int argc, char **argv) { } struct gimi_config *cfg = config_read(); - if (!cfg) - return 1; + ASSERT_CONFIG_EXIST(cfg); struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]); config_free(cfg); diff --git a/src/cli/command/push.c b/src/cli/command/push.c index c73edbe..9760880 100644 --- a/src/cli/command/push.c +++ b/src/cli/command/push.c @@ -55,8 +55,7 @@ int git_push(char *provider_name, char *branch_name, bool verbose) { int cli_command_push(int argc, char **argv) { struct gimi_config *cfg = config_read(); - if (!cfg) - return 1; + ASSERT_CONFIG_EXIST(cfg); bool verbose = argc == 2 && strcmp(argv[1], "--verbose") == 0; diff --git a/src/config.h b/src/config.h index 80612de..6f44b41 100644 --- a/src/config.h +++ b/src/config.h @@ -1,5 +1,12 @@ #include +#define ASSERT_CONFIG_EXIST(cfg) \ + if (!cfg) { \ + printf( \ + "error: missing gimi config, initialize it using gimi init command."); \ + return 1; \ + } + struct gimi_config_provider { char *name; char *ssh;