From 68fb86bfb47eb2b544a879cfeb777ffc76213140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 22 Jul 2024 14:58:51 +0200 Subject: [PATCH] feat: handle missing cfg, git correctly --- src/cli/command/config.c | 5 +++++ src/cli/command/init.c | 6 ++++++ src/config.c | 3 +++ 3 files changed, 14 insertions(+) diff --git a/src/cli/command/config.c b/src/cli/command/config.c index 4562c72..cb9263a 100644 --- a/src/cli/command/config.c +++ b/src/cli/command/config.c @@ -6,6 +6,11 @@ void cli_command_config(int argc, char **argv) { struct gimi_config *cfg = config_read(); + if (!cfg) { + printf("Missing gimi config. Initialize it using gimi init command."); + return; + } + for (int i = 0; i < cfg->providers_size; i++) { struct gimi_config_provider *provider = cfg->providers[i]; printf("ssh: %s | primary: %d\n", provider->ssh, provider->primary); diff --git a/src/cli/command/init.c b/src/cli/command/init.c index 14c596b..e3f96ca 100644 --- a/src/cli/command/init.c +++ b/src/cli/command/init.c @@ -16,6 +16,12 @@ void cli_command_init(int argc, char **argv) { return; } + struct stat stats; + if (stat(".git", &stats) != 0 || !S_ISDIR(stats.st_mode)) { + printf("Missing .git directory. Use git init to initialize git.\n"); + return; + } + char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) == NULL) { printf("Failed to get current working directory.\n"); diff --git a/src/config.c b/src/config.c index 8237b52..85eb7db 100644 --- a/src/config.c +++ b/src/config.c @@ -10,6 +10,9 @@ struct gimi_config *config_read() { char errbuf[200]; file_ptr = fopen(".gimi/config.toml", "r"); + if (!file_ptr) { + return NULL; + } toml_table_t *toml_cfg = toml_parse_file(file_ptr, errbuf, sizeof(errbuf)); fclose(file_ptr);