mirror of
https://github.com/xHyroM/gimi.git
synced 2024-11-24 16:21:05 +01:00
feat: provider sync cmd
This commit is contained in:
parent
aebaf1faf2
commit
28750f38e6
3 changed files with 51 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
#include "../../config.h"
|
||||
#include <linux/limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -29,17 +30,11 @@ int provider_info(int argc, char **argv) {
|
|||
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];
|
||||
}
|
||||
}
|
||||
|
||||
struct gimi_config_provider *provider = config_find_provider(cfg, argv[1]);
|
||||
config_free(cfg);
|
||||
|
||||
if (!provider) {
|
||||
printf("No such provider '%s'", argv[1]);
|
||||
printf("error: no such provider '%s'", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -52,6 +47,35 @@ int provider_info(int argc, char **argv) {
|
|||
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) {
|
||||
if (argc == 1) {
|
||||
return providers();
|
||||
|
@ -67,5 +91,9 @@ int cli_command_provider(int argc, char **argv) {
|
|||
return provider_info(argc, argv);
|
||||
}
|
||||
|
||||
if (strcmp(subcommand, "sync") == 0) {
|
||||
return provider_sync(argc, argv);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
12
src/config.c
12
src/config.c
|
@ -67,3 +67,15 @@ void config_free(struct gimi_config *cfg) {
|
|||
free(cfg->providers);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -13,3 +13,6 @@ struct gimi_config {
|
|||
|
||||
struct gimi_config *config_read();
|
||||
void config_free(struct gimi_config *cfg);
|
||||
|
||||
struct gimi_config_provider *config_find_provider(struct gimi_config *cfg,
|
||||
char *name);
|
||||
|
|
Loading…
Reference in a new issue