update: refactor fixes

This commit is contained in:
ngn 2024-08-01 19:05:26 +03:00
parent 4354fb8b89
commit c747ca759b
7 changed files with 83 additions and 52 deletions

View File

@ -40,10 +40,8 @@ bool cmd_gen(ctx_t *ctx, args_t *args) {
return ret; return ret;
} }
success(_("Loaded repository configuration"));
if (!config_load(ctx, &cfg, "confer.ini")) { if (!config_load(ctx, &cfg, "confer.ini")) {
error(_("Failed to load the configuration file (confer.ini):")); error(_("Failed to load the configuration file (" FG_BOLD "confer.ini" FG_RESET "):"));
details(errch); details(errch);
return ret; return ret;
} }
@ -62,7 +60,7 @@ bool cmd_gen(ctx_t *ctx, args_t *args) {
bar_free(); bar_free();
error(_("Failed to copy the target:")); error(_("Failed to copy the target:"));
details(errch); details(errch);
goto END; goto end;
} }
bar(counter, cfg.t_len); bar(counter, cfg.t_len);
@ -72,7 +70,7 @@ bool cmd_gen(ctx_t *ctx, args_t *args) {
bar_free(); bar_free();
ret = true; ret = true;
END: end:
config_free(&cfg); config_free(&cfg);
return ret; return ret;
} }

View File

@ -47,7 +47,7 @@ bool cmd_pull(ctx_t *ctx, args_t *args) {
if (url_is_local(url)) { if (url_is_local(url)) {
repo_root = url; repo_root = url;
goto COPY; goto copy;
} }
else if (url_is_name(url)) else if (url_is_name(url))
@ -65,7 +65,7 @@ bool cmd_pull(ctx_t *ctx, args_t *args) {
error(_("Failed to access the URL")); error(_("Failed to access the URL"));
details(errch); details(errch);
} }
goto END; goto end;
} }
rmrf(ctx->tmp_path); rmrf(ctx->tmp_path);
@ -78,7 +78,7 @@ bool cmd_pull(ctx_t *ctx, args_t *args) {
opts.fetch_opts.callbacks.transfer_progress = pull_progress; opts.fetch_opts.callbacks.transfer_progress = pull_progress;
opts.fetch_opts.callbacks.payload = NULL; opts.fetch_opts.callbacks.payload = NULL;
info(_("Cloning %s"), repo_url); info(_("Cloning " FG_BOLD "%s" FG_RESET), repo_url);
bar_init(); bar_init();
if (git_clone(&repo, repo_url, repo_root, &opts) < 0) { if (git_clone(&repo, repo_url, repo_root, &opts) < 0) {
@ -86,29 +86,29 @@ bool cmd_pull(ctx_t *ctx, args_t *args) {
error(_("Failed to clone the %s:"), repo_url); error(_("Failed to clone the %s:"), repo_url);
details(e->message); details(e->message);
bar_free(); bar_free();
goto END; goto end;
} }
bar_free(); bar_free();
git_libgit2_shutdown(); git_libgit2_shutdown();
COPY: copy:
if (chdir(repo_root) < 0) { if (chdir(repo_root) < 0) {
error(_("Failed to chdir to %s"), repo_root); error(_("Failed to chdir to %s"), repo_root);
goto END; goto end;
} }
if (!config_load(ctx, &repo_cfg, "mc.cfg")) { if (!config_load(ctx, &repo_cfg, "confer.ini")) {
error(_("Failed to load the configuration file (mc.cfg):")); error(_("Failed to load the configuration file (" FG_BOLD "confer.ini" FG_RESET "):"));
details(errch); details(errch);
goto END; goto end;
} }
success(_("Loaded repository configuration")); success(_("Loaded repository configuration"));
config_print(&repo_cfg); config_print(&repo_cfg);
if (!yesno(_("Do you want to continue?"))) if (args_get_bool(args, "--yes") && !yesno(_("Do you want to continue?")))
goto END; goto end;
target_t *cur = repo_cfg.t_first; target_t *cur = repo_cfg.t_first;
int counter = 0; int counter = 0;
@ -120,7 +120,7 @@ COPY:
if (!exists(cur->src)) { if (!exists(cur->src)) {
bar_free(); bar_free();
error(_("Failed to access the source for the target \"%s\""), cur->name); error(_("Failed to access the source for the target \"%s\""), cur->name);
goto END; goto end;
} }
cur = cur->next; cur = cur->next;
@ -133,44 +133,41 @@ COPY:
cur = repo_cfg.t_first; cur = repo_cfg.t_first;
info(_("Copying all the targets")); info(_("Copying all the targets"));
char **argv = NULL; char **argv = malloc(sizeof(char *));
int argc = 0, indx = 0; int argc = 1, indx = 1;
argv[0] = "install";
while (cur && ++counter > 0) { while (cur && ++counter > 0) {
target_print(cur); target_print(cur);
if (!yesno(_("Install the target?"))) { if (args_get_bool(args, "--yes") && !yesno(_("Install the target?"))) {
info(_("Skipping target")); info(_("Skipping target"));
goto NEXT; goto next;
} }
if (!target_copy(cur, false)) { if (!target_copy(cur, false)) {
error(_("Failed to copy the target:")); error(_("Failed to copy the target:"));
details(errch); details(errch);
goto END; goto end;
} }
argc += cur->require.s; argc += cur->require.s;
if (0 == argc) if (0 == argc)
goto NEXT; goto next;
if (NULL == argv) argv = realloc(argv, sizeof(char *) * argc);
argv = malloc(sizeof(char *) * argc);
else
argv = realloc(argv, sizeof(char *) * argc);
for (int i = 0; i < cur->require.s; i++) { for (int i = 0; i < cur->require.s; i++)
argv[indx] = cur->require.c[i]; argv[indx++] = cur->require.c[i];
indx++;
}
NEXT: next:
cur = cur->next; cur = cur->next;
} }
if (NULL == argv) { if (argc <= 1) {
ret = true; ret = true;
goto END; goto end;
} }
argc++; argc++;
@ -179,17 +176,18 @@ COPY:
ret = true; ret = true;
info(_("Installing all the requirements")); info(_("Installing all the requirements"));
if (!run_cmd_root("mp-install", argv)) { if (!run_cmd_root("matt", argv)) {
ret = false; ret = false;
error(_("Failed to run the mp-install command")); error(_("Failed to run the " FG_BOLD "matt" FG_RESET " command"));
details(errch); details(errch);
} }
free(argv); end:
END:
if (repo_cfg.name != NULL) if (repo_cfg.name != NULL)
config_free(&repo_cfg); config_free(&repo_cfg);
free(repo_url); free(repo_url);
free(argv);
return ret; return ret;
} }

View File

@ -3,24 +3,41 @@
#include <strings.h> #include <strings.h>
#include "ctx.h" #include "ctx.h"
#include "error.h"
#include "intl.h"
#include "util.h" #include "util.h"
bool ctx_init(ctx_t *ctx, char *homedir) { bool ctx_init(ctx_t *ctx, char *homedir) {
bzero(ctx, sizeof(ctx_t)); bzero(ctx, sizeof(ctx_t));
if (NULL == homedir) if (NULL == homedir) {
errno = HomedirInvalid;
error_set(_("Home directory is NULL"));
return false; return false;
}
if (!exists(homedir) && !is_dir(homedir)) {
errno = HomedirAccessFail;
error_set(_("Failed to access to the home directory"));
return false;
}
size_t home_len = strlen(homedir); size_t home_len = strlen(homedir);
ctx->dir_path = malloc(home_len + 20); ctx->dir_path = malloc(home_len + 25);
ctx->tmp_path = malloc(home_len + 25); ctx->tmp_path = malloc(home_len + 30);
ctx->lock_path = malloc(home_len + 25); ctx->lock_path = malloc(home_len + 30);
ctx->home_path = strdup(homedir); ctx->home_path = strdup(homedir);
join(ctx->dir_path, homedir, ".local/share/mc"); join(ctx->dir_path, homedir, ".local/share/confer");
join(ctx->lock_path, ctx->dir_path, "lock"); join(ctx->lock_path, ctx->dir_path, "lock");
join(ctx->tmp_path, ctx->dir_path, "tmp"); join(ctx->tmp_path, ctx->dir_path, "tmp");
if (!mksubdirsp(ctx->dir_path, 0755)) {
errno = HomedirAccessFail;
error_set(_("Failed to access to the home directory"));
return false;
}
return true; return true;
} }

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
enum ErrorCodes { enum ErrorCodes {
HomedirInvalid = 953,
HomedirAccessFail = 954,
ConfigUnknown = 955, ConfigUnknown = 955,
ConfigNotFound = 954, ConfigNotFound = 954,
ConfigInvalid = 953, ConfigInvalid = 953,

View File

@ -33,6 +33,7 @@
#include "args.h" #include "args.h"
#include "cmd.h" #include "cmd.h"
#include "ctx.h" #include "ctx.h"
#include "error.h"
#include "intl.h" #include "intl.h"
#include "lock.h" #include "lock.h"
#include "log.h" #include "log.h"
@ -73,7 +74,16 @@ int main(int argc, char *argv[]) {
homedir = getenv("HOME"); homedir = getenv("HOME");
if(!ctx_init(&ctx, homedir)){ if(!ctx_init(&ctx, homedir)){
error(_("Please specify the homedir with "FG_BOLD"--home"FG_RESET" option or specify the "FG_BOLD"HOME"FG_RESET" environment variable")); switch (errno) {
case HomedirInvalid:
error(_("Please specify the home directory with "FG_BOLD"--home"FG_RESET" option or specify the "FG_BOLD"HOME"FG_RESET" environment variable"));
break;
case HomedirAccessFail:
error(_("Cannot access to the home directory ("FG_BOLD"%s"FG_RESET")"), homedir);
break;
}
goto end; goto end;
} }
@ -88,7 +98,7 @@ int main(int argc, char *argv[]) {
goto end; goto end;
case LOCK_ERROR: case LOCK_ERROR:
error(_("Failed to lock, are you root?")); error(_("Failed to lock, do you have access to the home directory?"));
goto end; goto end;
default: default:

View File

@ -13,24 +13,29 @@
extern char **environ; extern char **environ;
char *run_find_cmd(char *cmd) { char *run_find_cmd(char *cmd) {
char *path = getenv("PATH"), *save, *el; char *path = getenv("PATH"), *save = NULL, *el = NULL;
while ((el = strtok_r(path, ":", &save)) != NULL) { if ((el = strtok_r(path, ":", &save)) == NULL)
return NULL;
do {
char fp[strlen(el) + strlen(cmd) + 2]; char fp[strlen(el) + strlen(cmd) + 2];
join(fp, el, cmd); join(fp, el, cmd);
if (exists(fp)) if (exists(fp))
return strdup(fp); return strdup(fp);
} } while ((el = strtok_r(NULL, ":", &save)) != NULL);
return NULL; return NULL;
} }
bool run_cmd(char **all) { bool run_cmd(char **all) {
pid_t pid; int status = 0;
pid_t pid = 0;
if (posix_spawn(&pid, all[0], NULL, NULL, all, environ) != 0) if (posix_spawn(&pid, all[0], NULL, NULL, all, environ) != 0)
return false; return false;
int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
return status == 0; return status == 0;
} }

View File

@ -54,7 +54,7 @@ char *url_complete(char *name) {
} }
curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl/mp"); curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl/confer");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull); curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
CURLcode res = curl_easy_perform(curl); CURLcode res = curl_easy_perform(curl);