feat: provider sync cmd

This commit is contained in:
Jozef Steinhübl 2024-07-22 15:42:38 +02:00
parent aebaf1faf2
commit 28750f38e6
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
3 changed files with 51 additions and 8 deletions

View file

@ -1,4 +1,5 @@
#include "../../config.h" #include "../../config.h"
#include <linux/limits.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -29,17 +30,11 @@ int provider_info(int argc, char **argv) {
if (!cfg) if (!cfg)
return 1; return 1;
struct gimi_config_provider *provider = NULL; struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]);
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); config_free(cfg);
if (!provider) { if (!provider) {
printf("No such provider '%s'", argv[1]); printf("error: no such provider '%s'", argv[1]);
return 1; return 1;
} }
@ -52,6 +47,35 @@ int provider_info(int argc, char **argv) {
return 0; return 0;
} }
int provider_sync(int argc, char **argv) {
if (argc == 1) {
printf("usage: gimi provider sync [--all] [name]");
return 1;
}
struct gimi_config *cfg = config_read();
if (!cfg)
return 1;
struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]);
config_free(cfg);
char command[100];
snprintf(command, sizeof(command), "git remote add %s %s", provider->name,
provider->ssh);
int ret = system(command);
if (ret != 0) {
printf("error: failed to sync provider '%s' with git's exit code %d.",
provider->name, ret);
} else {
printf("info: provider '%s' has been successfully synced with git.",
provider->name);
}
return 0;
}
int cli_command_provider(int argc, char **argv) { int cli_command_provider(int argc, char **argv) {
if (argc == 1) { if (argc == 1) {
return providers(); return providers();
@ -67,5 +91,9 @@ int cli_command_provider(int argc, char **argv) {
return provider_info(argc, argv); return provider_info(argc, argv);
} }
if (strcmp(subcommand, "sync") == 0) {
return provider_sync(argc, argv);
}
return 0; return 0;
} }

View file

@ -67,3 +67,15 @@ void config_free(struct gimi_config *cfg) {
free(cfg->providers); free(cfg->providers);
free(cfg); free(cfg);
} }
struct gimi_config_provider *config_find_provider(struct gimi_config *cfg,
char *name) {
struct gimi_config_provider *provider = NULL;
for (int i = 0; i < cfg->providers_size; i++) {
if (strcmp(cfg->providers[i]->name, name) == 0) {
provider = cfg->providers[i];
}
}
return provider;
}

View file

@ -13,3 +13,6 @@ struct gimi_config {
struct gimi_config *config_read(); struct gimi_config *config_read();
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,
char *name);