feat: provider cli command

This commit is contained in:
Jozef Steinhübl 2024-07-22 15:21:03 +02:00
parent fac687bd68
commit aebaf1faf2
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
5 changed files with 80 additions and 0 deletions

View file

@ -1,6 +1,7 @@
#include "../gimi_constants.h"
#include "command/config.h"
#include "command/init.h"
#include "command/provider.h"
#include <stdio.h>
#include <string.h>
@ -26,5 +27,9 @@ int cli_handle(int argc, char **argv) {
return cli_command_config(argc, argv);
}
if (strcmp(sub_command, "provider") == 0) {
return cli_command_provider(argc, argv);
}
return 0;
}

View file

@ -0,0 +1,71 @@
#include "../../config.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int providers() {
struct gimi_config *cfg = config_read();
if (!cfg)
return 1;
for (int i = 0; i < cfg->providers_size; i++) {
struct gimi_config_provider *provider = cfg->providers[i];
printf("%s\n", provider->name);
}
config_free(cfg);
return 0;
}
int provider_info(int argc, char **argv) {
if (argc == 1) {
printf("usage: gimi provider info <name>");
return 1;
}
struct gimi_config *cfg = config_read();
if (!cfg)
return 1;
struct gimi_config_provider *provider = NULL;
for (int i = 0; i < cfg->providers_size; i++) {
if (strcmp(cfg->providers[i]->name, argv[1]) == 0) {
provider = cfg->providers[i];
}
}
config_free(cfg);
if (!provider) {
printf("No such provider '%s'", argv[1]);
return 1;
}
printf("name: %s\n", provider->name);
printf("ssh: %s\n", provider->ssh);
printf("primary: %d\n", provider->primary);
free(provider);
return 0;
}
int cli_command_provider(int argc, char **argv) {
if (argc == 1) {
return providers();
}
// remove "provider" from args
argc -= 1;
argv += 1;
char *subcommand = argv[0];
if (strcmp(subcommand, "info") == 0) {
return provider_info(argc, argv);
}
return 0;
}

View file

@ -0,0 +1 @@
int cli_command_provider(int argc, char **argv);

View file

@ -41,6 +41,8 @@ struct gimi_config *config_read() {
(struct gimi_config_provider *)malloc(
sizeof(struct gimi_config_provider));
provider->name = strdup(key);
toml_datum_t ssh = toml_string_in(toml_provider, "ssh");
provider->ssh = strdup(ssh.u.s);

View file

@ -1,6 +1,7 @@
#include <stdbool.h>
struct gimi_config_provider {
char *name;
char *ssh;
bool primary;
};