From 8b522693a7b0ef3847aaa5da25fdc07c86271770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 22 Jul 2024 09:17:46 +0200 Subject: [PATCH] feat: basic config --- .gimi/config.toml | 6 +++++ .gitignore | 3 +++ .gitmodules | 3 +++ CMakeLists.txt | 6 +++++ libs/tomlc99 | 1 + src/cli/cli.c | 5 ++++ src/cli/command/config.c | 13 ++++++++++ src/cli/command/config.h | 1 + src/cli/command/init.c | 27 ++++++++++++++++++- src/config.c | 56 ++++++++++++++++++++++++++++++++++++++++ src/config.h | 13 ++++++++++ 11 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 .gimi/config.toml create mode 100644 .gitmodules create mode 160000 libs/tomlc99 create mode 100644 src/cli/command/config.c create mode 100644 src/cli/command/config.h create mode 100644 src/config.c create mode 100644 src/config.h diff --git a/.gimi/config.toml b/.gimi/config.toml new file mode 100644 index 0000000..4c0a19a --- /dev/null +++ b/.gimi/config.toml @@ -0,0 +1,6 @@ +[providers] +[providers.sourcehut] +ssh = "git@git.sr.ht:~hyro/gimi" +primary = true +[providers.github] +ssh = "git@github.com:xhyrom/gimi.git" diff --git a/.gitignore b/.gitignore index 7465206..75c87bc 100644 --- a/.gitignore +++ b/.gitignore @@ -188,6 +188,9 @@ _deps # External projects *-prefix/ +### clangd ### +.cache/ + ### VisualStudioCode ### .vscode/* !.vscode/settings.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5b50831 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/tomlc99"] + path = libs/tomlc99 + url = https://github.com/cktan/tomlc99 diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b3db5e..e8be2eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,10 +4,16 @@ project(gimi VERSION 0.1.0 LANGUAGES C) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED true) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build) +add_library(toml STATIC ./libs/tomlc99/toml.h ./libs/tomlc99/toml.c) + file(GLOB_RECURSE SRC CONFIGURE_DEPENDS ./src/*.c ./src/*.h) configure_file(src/gimi_constants.h.in src/gimi_constants.h) +include_directories(libs/tomlc99) + add_executable(gimi ${SRC}) +target_link_libraries(gimi PUBLIC toml) diff --git a/libs/tomlc99 b/libs/tomlc99 new file mode 160000 index 0000000..5221b3d --- /dev/null +++ b/libs/tomlc99 @@ -0,0 +1 @@ +Subproject commit 5221b3d3d66c25a1dc6f0372b4f824f1202fe398 diff --git a/src/cli/cli.c b/src/cli/cli.c index 600eb84..305c6d5 100644 --- a/src/cli/cli.c +++ b/src/cli/cli.c @@ -1,4 +1,5 @@ #include "../gimi_constants.h" +#include "command/config.h" #include "command/init.h" #include #include @@ -21,5 +22,9 @@ int cli_handle(int argc, char **argv) { cli_command_init(argc, argv); } + if (strcmp(sub_command, "config") == 0) { + cli_command_config(argc, argv); + } + return 0; } diff --git a/src/cli/command/config.c b/src/cli/command/config.c new file mode 100644 index 0000000..809bdbe --- /dev/null +++ b/src/cli/command/config.c @@ -0,0 +1,13 @@ +#include "../../config.h" +#include "../cli.h" +#include + +#define INIT_CONFIG "[providers]\n" + +void cli_command_config(int argc, char **argv) { + gimi_config *cfg = config_read(); + for (int i = 0; i < cfg->providers_size; i++) { + gimi_config_provider *provider = cfg->providers[i]; + printf("ssh: %s | primary: %d\n", provider->ssh, provider->primary); + } +} diff --git a/src/cli/command/config.h b/src/cli/command/config.h new file mode 100644 index 0000000..b855828 --- /dev/null +++ b/src/cli/command/config.h @@ -0,0 +1 @@ +void cli_command_config(int argc, char **argv); diff --git a/src/cli/command/init.c b/src/cli/command/init.c index d469162..14c596b 100644 --- a/src/cli/command/init.c +++ b/src/cli/command/init.c @@ -1,5 +1,30 @@ #include "../cli.h" +#include +#include +#include #include +#include #include -void cli_command_init(int argc, char **argv) { printf("init"); } +#define INIT_CONFIG "[providers]\n" + +void cli_command_init(int argc, char **argv) { + errno = 0; + int ret = mkdir(".gimi", S_IRWXU); + if (ret == -1 && errno != EEXIST) { + printf("Failed to initialize gimi.\n"); + return; + } + + char cwd[PATH_MAX]; + if (getcwd(cwd, sizeof(cwd)) == NULL) { + printf("Failed to get current working directory.\n"); + } + printf("Initialized gimi in %s/.gimi\n", cwd); + + FILE *file_ptr; + file_ptr = fopen(".gimi/config.toml", "w"); + + fprintf(file_ptr, INIT_CONFIG); + fclose(file_ptr); +} diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..b35ea8f --- /dev/null +++ b/src/config.c @@ -0,0 +1,56 @@ +#include "config.h" +#include +#include +#include +#include +#include + +gimi_config *config_read() { + FILE *file_ptr; + char errbuf[200]; + + file_ptr = fopen(".gimi/config.toml", "r"); + + toml_table_t *toml_cfg = toml_parse_file(file_ptr, errbuf, sizeof(errbuf)); + fclose(file_ptr); + + toml_table_t *toml_providers = toml_table_in(toml_cfg, "providers"); + + int size = 0; + const char *key; + while ((key = toml_key_in(toml_providers, size))) { + size++; + } + + gimi_config *cfg = (gimi_config *)malloc(sizeof(gimi_config)); + + cfg->providers_size = size; + cfg->providers = malloc(cfg->providers_size * sizeof(gimi_config_provider)); + + for (int i = 0; i < size; i++) { + const char *key = toml_key_in(toml_providers, i); + + toml_table_t *toml_provider = toml_table_in(toml_providers, key); + + gimi_config_provider *provider = + (gimi_config_provider *)malloc(sizeof(gimi_config_provider)); + + toml_datum_t ssh = toml_string_in(toml_provider, "ssh"); + provider->ssh = strdup(ssh.u.s); + + toml_datum_t toml_primary = toml_bool_in(toml_provider, "primary"); + if (!toml_primary.ok) { + provider->primary = 0; + } else { + provider->primary = toml_primary.u.b; + } + + cfg->providers[i] = provider; + + free(ssh.u.s); + } + + toml_free(toml_cfg); + + return cfg; +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..09c8122 --- /dev/null +++ b/src/config.h @@ -0,0 +1,13 @@ +#include + +typedef struct { + char *ssh; + bool primary; +} gimi_config_provider; + +typedef struct { + int providers_size; + gimi_config_provider **providers; +} gimi_config; + +gimi_config *config_read();