feat: return proper codes

This commit is contained in:
Jozef Steinhübl 2024-07-22 15:20:39 +02:00
parent 68fb86bfb4
commit fac687bd68
No known key found for this signature in database
GPG key ID: E6BC90C91973B08F
5 changed files with 16 additions and 10 deletions

View file

@ -19,11 +19,11 @@ int cli_handle(int argc, char **argv) {
char *sub_command = argv[0];
if (strcmp(sub_command, "init") == 0) {
cli_command_init(argc, argv);
return cli_command_init(argc, argv);
}
if (strcmp(sub_command, "config") == 0) {
cli_command_config(argc, argv);
return cli_command_config(argc, argv);
}
return 0;

View file

@ -4,17 +4,19 @@
#define INIT_CONFIG "[providers]\n"
void cli_command_config(int argc, char **argv) {
int cli_command_config(int argc, char **argv) {
struct gimi_config *cfg = config_read();
if (!cfg) {
printf("Missing gimi config. Initialize it using gimi init command.");
return;
return 1;
}
for (int i = 0; i < cfg->providers_size; i++) {
struct gimi_config_provider *provider = cfg->providers[i];
printf("ssh: %s | primary: %d\n", provider->ssh, provider->primary);
printf("name: %s | ssh: %s | primary: %d\n", provider->name, provider->ssh,
provider->primary);
}
config_free(cfg);
return 0;
}

View file

@ -1 +1 @@
void cli_command_config(int argc, char **argv);
int cli_command_config(int argc, char **argv);

View file

@ -8,24 +8,26 @@
#define INIT_CONFIG "[providers]\n"
void cli_command_init(int argc, char **argv) {
int cli_command_init(int argc, char **argv) {
errno = 0;
int ret = mkdir(".gimi", S_IRWXU);
if (ret == -1 && errno != EEXIST) {
printf("Failed to initialize gimi.\n");
return;
return 1;
}
struct stat stats;
if (stat(".git", &stats) != 0 || !S_ISDIR(stats.st_mode)) {
printf("Missing .git directory. Use git init to initialize git.\n");
return;
return 1;
}
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) == NULL) {
printf("Failed to get current working directory.\n");
return 1;
}
printf("Initialized gimi in %s/.gimi\n", cwd);
FILE *file_ptr;
@ -33,4 +35,6 @@ void cli_command_init(int argc, char **argv) {
fprintf(file_ptr, INIT_CONFIG);
fclose(file_ptr);
return 0;
}

View file

@ -1 +1 @@
void cli_command_init(int argc, char **argv);
int cli_command_init(int argc, char **argv);