mirror of
https://github.com/xHyroM/gimi.git
synced 2024-11-23 08:01:06 +01:00
feat: support for config writing, provider add cmd
This commit is contained in:
parent
00b74be500
commit
f5229d52e8
5 changed files with 64 additions and 3 deletions
|
@ -1,5 +1,4 @@
|
|||
#include "../../config.h"
|
||||
#include "../cli.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define INIT_CONFIG "[providers]\n"
|
||||
|
|
|
@ -27,6 +27,41 @@ int providers() {
|
|||
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) {
|
||||
if (argc == 1) {
|
||||
printf("usage: gimi provider info <name>\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);
|
||||
}
|
||||
|
|
24
src/config.c
24
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);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
// Do not edit manually! Generated by cmake
|
||||
#define GIMI_VERSION_MAJOR 0
|
||||
#define GIMI_VERSION_MINOR 1
|
||||
|
|
Loading…
Reference in a new issue