feat: support for config writing, provider add cmd

This commit is contained in:
Jozef Steinhübl 2024-08-27 22:59:32 +02:00
parent 00b74be500
commit f5229d52e8
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
5 changed files with 64 additions and 3 deletions

View file

@ -1,5 +1,4 @@
#include "../../config.h" #include "../../config.h"
#include "../cli.h"
#include <stdio.h> #include <stdio.h>
#define INIT_CONFIG "[providers]\n" #define INIT_CONFIG "[providers]\n"

View file

@ -27,6 +27,41 @@ int providers() {
return 0; return 0;
} }
int provider_add(int argc, char **argv) {
if (argc <= 2) {
printf("usage: gimi provider info <name> <ssh>\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) { int provider_info(int argc, char **argv) {
if (argc == 1) { if (argc == 1) {
printf("usage: gimi provider info <name>\n"); printf("usage: gimi provider info <name>\n");
@ -99,6 +134,10 @@ int cli_command_provider(int argc, char **argv) {
return 0; return 0;
} }
if (strcmp(subcommand, "add") == 0) {
return provider_add(argc, argv);
}
if (strcmp(subcommand, "info") == 0) { if (strcmp(subcommand, "info") == 0) {
return provider_info(argc, argv); return provider_info(argc, argv);
} }

View file

@ -63,6 +63,30 @@ struct gimi_config *config_read() {
return cfg; 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) { void config_free(struct gimi_config *cfg) {
for (int i = 0; i < cfg->providers_size; i++) { for (int i = 0; i < cfg->providers_size; i++) {
free(cfg->providers[i]->name); free(cfg->providers[i]->name);

View file

@ -21,6 +21,7 @@ struct gimi_config {
}; };
struct gimi_config *config_read(); struct gimi_config *config_read();
void config_write(struct gimi_config *cfg);
void config_free(struct gimi_config *cfg); void config_free(struct gimi_config *cfg);
struct gimi_config_provider *config_find_provider(struct gimi_config *cfg, struct gimi_config_provider *config_find_provider(struct gimi_config *cfg,

View file

@ -1,5 +1,3 @@
#pragma once
// Do not edit manually! Generated by cmake // Do not edit manually! Generated by cmake
#define GIMI_VERSION_MAJOR 0 #define GIMI_VERSION_MAJOR 0
#define GIMI_VERSION_MINOR 1 #define GIMI_VERSION_MINOR 1