mirror of
https://github.com/xHyroM/gimi.git
synced 2024-11-21 07:31:05 +01:00
feat: basic config
This commit is contained in:
parent
4b5c9e3aca
commit
8b522693a7
11 changed files with 133 additions and 1 deletions
6
.gimi/config.toml
Normal file
6
.gimi/config.toml
Normal file
|
@ -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"
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -188,6 +188,9 @@ _deps
|
|||
# External projects
|
||||
*-prefix/
|
||||
|
||||
### clangd ###
|
||||
.cache/
|
||||
|
||||
### VisualStudioCode ###
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
|
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "libs/tomlc99"]
|
||||
path = libs/tomlc99
|
||||
url = https://github.com/cktan/tomlc99
|
|
@ -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)
|
||||
|
|
1
libs/tomlc99
Submodule
1
libs/tomlc99
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 5221b3d3d66c25a1dc6f0372b4f824f1202fe398
|
|
@ -1,4 +1,5 @@
|
|||
#include "../gimi_constants.h"
|
||||
#include "command/config.h"
|
||||
#include "command/init.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
13
src/cli/command/config.c
Normal file
13
src/cli/command/config.c
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include "../../config.h"
|
||||
#include "../cli.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
1
src/cli/command/config.h
Normal file
1
src/cli/command/config.h
Normal file
|
@ -0,0 +1 @@
|
|||
void cli_command_config(int argc, char **argv);
|
|
@ -1,5 +1,30 @@
|
|||
#include "../cli.h"
|
||||
#include <errno.h>
|
||||
#include <linux/limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
56
src/config.c
Normal file
56
src/config.c
Normal file
|
@ -0,0 +1,56 @@
|
|||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <toml.h>
|
||||
|
||||
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;
|
||||
}
|
13
src/config.h
Normal file
13
src/config.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
char *ssh;
|
||||
bool primary;
|
||||
} gimi_config_provider;
|
||||
|
||||
typedef struct {
|
||||
int providers_size;
|
||||
gimi_config_provider **providers;
|
||||
} gimi_config;
|
||||
|
||||
gimi_config *config_read();
|
Loading…
Reference in a new issue