/* actionhelper.c - helper functions for ck actions --------------------*- C -*-
*
* This file is part of ck, the config keeper
*
* -----------------------------------------------------------------------------
*
* Copyright (C) 2018 Anastasis Grammenos
* GPLv3 (see LICENCE for the full notice)
*
* -----------------------------------------------------------------------------
*
*
*
* -------------------------------------------------------------------------- */
#include "actionhelper.h"
char add_err[STR_M] = "";
int add_err_message(char *err) {
if (!str_is_empty(add_err)) {
if (err) {
strcpy(err, add_err);
}
return 1;
}
return 0;
}
int copy_config(char *path);
void link_config(const AddOpt *opt, const char* newPath) {
printf("Linking %s -> %s\n", newPath, opt->confPath);
if (util_symlink_file(newPath, opt->confPath) != 0) {
strcpy(add_err, "Could not link file.");
}
}
void move_config(const AddOpt *opt, const Conf *conf, char *ret) {
char newName[STR_M];
str_make_ck_config_name(newName, opt->confPath, opt->progName);
char newPath[STR_L];
str_join_dirname_with_basename(newPath, opt->secret ? conf->SCRT_dir : conf->VC_dir, newName);
printf("Moving %s -> %s\n", opt->confPath, newPath);
strcpy(ret, newPath);
if (util_move_file(opt->confPath, newPath) != 0) {
strcpy(add_err, "Could not move file.");
}
}
AddOpt add_make_options(cklist *args) {
list_rewind(args);
/* since we are here, the first two argumens must exist */
AddOpt addOpt = {
.progName = list_get(args),
.secret = 0,
.prime = 0,
.err = ADD_NO_ERR
};
list_next(args);
if (!util_is_file_rw(list_get(args))) {
addOpt.err = ADD_ERR_WRONG_CONFIG;
return addOpt;
}
realpath(list_get(args), addOpt.confPath);
while (list_next(args)) {
if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) {
addOpt.secret = 1;
} else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) {
addOpt.prime = 1;
} else {
addOpt.err = ADD_ERR_WRONG_FLAGS;
return addOpt;
}
}
list_rewind(args);
return addOpt;
}
void add_print_opts(AddOpt *opt) {
printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
if (opt->prime && opt->secret) {
printf("Options:\tsecret, primary\n");
} else if (opt->prime) {
printf("Options:\tprimary\n");
} else if (opt->secret) {
printf("Options:\tsecret\n");
}
}
void add_make_link(const AddOpt *opt,
const Conf *conf) {
char newPath[STR_L];
move_config(opt, conf, newPath);
if (add_err_message(NULL)) {
return;
}
link_config(opt, newPath);
if (add_err_message(NULL)) {
return;
}
}
edit_rc
edit_get_config_or_suggestions(cklist *args, char *ret) {
UNUSED(args);
UNUSED(ret);
return ERC_ERR;
}
ListOpt list_make_options(cklist *args) {
list_rewind(args);
ListOpt listOpt = {
._lt = LT_NONE,
._lst = LST_PLAIN,
.err = 0
};
if (list_size(args)) {
do {
if (strcmp(list_get(args), "-t") == 0) {
if (!list_next(args)) {
listOpt.err = 1;
break;
}
if (strcmp(list_get(args), "plain") == 0) {
listOpt._lst = LST_PLAIN;
}
else if (strcmp(list_get(args), "lisp") == 0) {
listOpt._lst = LST_LISP;
}
else if (strcmp(list_get(args), "python") == 0) {
listOpt._lst = LST_PYTHON;
}
else {
listOpt.err = 1;
}
}
else if (strcmp(list_get(args), "paths") == 0) {
listOpt._lt = LT_PATH;
}
else if (strcmp(list_get(args), "programs") == 0) {
listOpt._lt = LT_PROGRAM;
}
else if (strcmp(list_get(args), "tree") == 0) {
listOpt._lt = LT_TREE;
}
else {
listOpt.err = 1;
}
} while(list_next(args));
}
list_rewind(args);
return listOpt;
}
/*****************/
/* PRINT RESULTS */
/*****************/
void print_INIT_result(int err) {
if (!err) {
printf("Initialized empty ckdb.\n");
}
}
void print_ADD_result(int err) {
if (!err) {
printf("ckdb updated succesfully.\n");
return;
}
printf("Could not complete add transaction.\n");
}
void print_DEL_result(int err) {
if (!err) {
printf("succes\n");
return;
}
printf("Not Supported\n");
}
void print_EDIT_result(int err) {
if (!err) {
printf("succes\n");
return;
}
printf("failure\n");
}
void print_LIST_result(int err) {
if (!err) {
return;
}
printf("Wrong list arguments\n");
}
void print_SEARCH_result(int err) {
if (!err) {
printf("succes\n");
return;
}
printf("Not Supported\n");
}
void print_HELP_result(int err) {
if (!err) {
printf("succes\n");
return;
}
printf("failure\n");
}