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;
}
success(_("Loaded repository configuration"));
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);
return ret;
}
@ -62,7 +60,7 @@ bool cmd_gen(ctx_t *ctx, args_t *args) {
bar_free();
error(_("Failed to copy the target:"));
details(errch);
goto END;
goto end;
}
bar(counter, cfg.t_len);
@ -72,7 +70,7 @@ bool cmd_gen(ctx_t *ctx, args_t *args) {
bar_free();
ret = true;
END:
end:
config_free(&cfg);
return ret;
}

View File

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

View File

@ -3,24 +3,41 @@
#include <strings.h>
#include "ctx.h"
#include "error.h"
#include "intl.h"
#include "util.h"
bool ctx_init(ctx_t *ctx, char *homedir) {
bzero(ctx, sizeof(ctx_t));
if (NULL == homedir)
if (NULL == homedir) {
errno = HomedirInvalid;
error_set(_("Home directory is NULL"));
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);
ctx->dir_path = malloc(home_len + 20);
ctx->tmp_path = malloc(home_len + 25);
ctx->lock_path = malloc(home_len + 25);
ctx->dir_path = malloc(home_len + 25);
ctx->tmp_path = malloc(home_len + 30);
ctx->lock_path = malloc(home_len + 30);
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->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;
}

View File

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

View File

@ -33,6 +33,7 @@
#include "args.h"
#include "cmd.h"
#include "ctx.h"
#include "error.h"
#include "intl.h"
#include "lock.h"
#include "log.h"
@ -73,7 +74,16 @@ int main(int argc, char *argv[]) {
homedir = getenv("HOME");
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;
}
@ -88,7 +98,7 @@ int main(int argc, char *argv[]) {
goto end;
case LOCK_ERROR:
error(_("Failed to lock, are you root?"));
error(_("Failed to lock, do you have access to the home directory?"));
goto end;
default:

View File

@ -13,24 +13,29 @@
extern char **environ;
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];
join(fp, el, cmd);
if (exists(fp))
return strdup(fp);
}
} while ((el = strtok_r(NULL, ":", &save)) != NULL);
return NULL;
}
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)
return false;
int status;
waitpid(pid, &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_USERAGENT, "libcurl/mp");
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl/confer");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, devnull);
CURLcode res = curl_easy_perform(curl);