From f5229d52e87151ce61c9a9801c28a4d62621aa47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Tue, 27 Aug 2024 22:59:32 +0200 Subject: [PATCH] feat: support for config writing, provider add cmd --- src/cli/command/config.c | 1 - src/cli/command/provider.c | 39 ++++++++++++++++++++++++++++++++++++++ src/config.c | 24 +++++++++++++++++++++++ src/config.h | 1 + src/gimi_constants.h | 2 -- 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/cli/command/config.c b/src/cli/command/config.c index 04619b9..0f9eced 100644 --- a/src/cli/command/config.c +++ b/src/cli/command/config.c @@ -1,5 +1,4 @@ #include "../../config.h" -#include "../cli.h" #include #define INIT_CONFIG "[providers]\n" diff --git a/src/cli/command/provider.c b/src/cli/command/provider.c index a311c6f..b2f81ff 100644 --- a/src/cli/command/provider.c +++ b/src/cli/command/provider.c @@ -27,6 +27,41 @@ int providers() { return 0; } +int provider_add(int argc, char **argv) { + if (argc <= 2) { + printf("usage: gimi provider info \n"); + return 1; + } + + struct gimi_config *cfg = config_read(); + ASSERT_CONFIG_EXIST(cfg); + + if (config_find_provider(cfg, argv[1])) { + printf("error: provider '%s' already exists\n", argv[1]); + return 1; + } + + struct gimi_config_provider *provider = (struct gimi_config_provider *)malloc( + sizeof(struct gimi_config_provider)); + + provider->name = strdup(argv[1]); + provider->ssh = strdup(argv[2]); + provider->primary = 0; + + cfg->providers_size = cfg->providers_size + 1; + cfg->providers = + realloc(cfg->providers, + cfg->providers_size * sizeof(struct gimi_config_provider)); + + cfg->providers[cfg->providers_size - 1] = provider; + + config_write(cfg); + + config_free(cfg); + + return 0; +} + int provider_info(int argc, char **argv) { if (argc == 1) { printf("usage: gimi provider info \n"); @@ -99,6 +134,10 @@ int cli_command_provider(int argc, char **argv) { return 0; } + if (strcmp(subcommand, "add") == 0) { + return provider_add(argc, argv); + } + if (strcmp(subcommand, "info") == 0) { return provider_info(argc, argv); } diff --git a/src/config.c b/src/config.c index 48e4fdd..73137dc 100644 --- a/src/config.c +++ b/src/config.c @@ -63,6 +63,30 @@ struct gimi_config *config_read() { return cfg; } +void config_write(struct gimi_config *cfg) { + FILE *file_ptr; + + file_ptr = fopen(".gimi/config.toml", "w"); + if (!file_ptr) { + return; + } + + fprintf(file_ptr, "[providers]"); + + for (int i = 0; i < cfg->providers_size; i++) { + struct gimi_config_provider *provider = cfg->providers[i]; + + fprintf(file_ptr, "\n[providers.%s]\nssh = \"%s\"", provider->name, + provider->ssh); + + if (provider->primary) { + fprintf(file_ptr, "\nprimary = true"); + } + } + + fclose(file_ptr); +} + void config_free(struct gimi_config *cfg) { for (int i = 0; i < cfg->providers_size; i++) { free(cfg->providers[i]->name); diff --git a/src/config.h b/src/config.h index af28c2d..aeb0a2c 100644 --- a/src/config.h +++ b/src/config.h @@ -21,6 +21,7 @@ struct gimi_config { }; struct gimi_config *config_read(); +void config_write(struct gimi_config *cfg); void config_free(struct gimi_config *cfg); struct gimi_config_provider *config_find_provider(struct gimi_config *cfg, diff --git a/src/gimi_constants.h b/src/gimi_constants.h index f718b79..ae8ad72 100644 --- a/src/gimi_constants.h +++ b/src/gimi_constants.h @@ -1,5 +1,3 @@ -#pragma once - // Do not edit manually! Generated by cmake #define GIMI_VERSION_MAJOR 0 #define GIMI_VERSION_MINOR 1