aboutsummaryrefslogtreecommitdiffstats
path: root/src/add.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/add.c')
-rw-r--r--src/add.c57
1 files changed, 51 insertions, 6 deletions
diff --git a/src/add.c b/src/add.c
index f7a49bf..9959204 100644
--- a/src/add.c
+++ b/src/add.c
@@ -1,8 +1,16 @@
+/* add.c - the add action ----------------------------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
#include <libgen.h>
-#include "actions.h"
#include "dblayer.h"
-#include "queries.h"
#include "ckerrlog.h"
ERRLOG(add);
@@ -160,7 +168,8 @@ static int add_insert_relationship(DB *db, const int pid, const int cid) {
return 1;
}
-int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) {
+/* Returns 1 in error, 0 otherwise */
+static int add_transaction_try(DB *db, const AddOpt * const opt, const char *home) {
__BEGIN_TRANSACTION__
int pid = add_get_or_insert_program_to_db(db, opt->progName);
if (db->error == SQL_ERR_SQLITE) {
@@ -221,7 +230,7 @@ static int move_config(const AddOpt *opt, char *progDir, char *ret) {
return 0;
}
-AddOpt add_make_options(cklist *args) {
+static AddOpt add_make_options(cklist *args) {
list_rewind(args);
/* since we are here, the first two arguments must exist */
AddOpt addOpt = {
@@ -253,7 +262,7 @@ AddOpt add_make_options(cklist *args) {
return addOpt;
}
-void add_print_opts(AddOpt *opt) {
+static 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");
@@ -273,7 +282,7 @@ static void get_or_make_program_dir(const AddOpt *opt, const Conf *conf, char *r
strcpy(ret, tmp);
}
-int add_make_link(const AddOpt *opt, const Conf *conf) {
+static int add_make_link(const AddOpt *opt, const Conf *conf) {
char progDir[STR_L] = "";
get_or_make_program_dir(opt, conf, progDir);
char newPath[STR_L] = "";
@@ -285,3 +294,39 @@ int add_make_link(const AddOpt *opt, const Conf *conf) {
}
return 0;
}
+
+int run_ADD(UserOpt * opt, Conf *conf) {
+ DB db;
+ if (open_DB(&db, opt)) {
+ return -1;
+ }
+ AddOpt addOpt = add_make_options(opt->args);
+ switch (addOpt.err) {
+ case ADD_NO_ERR:
+ break;
+ case ADD_ERR_WRONG_CONFIG:
+ ERR("The config file specified doesn't exist or is a link.");
+ goto error;
+ case ADD_ERR_WRONG_FLAGS:
+ ERR("Flags are: -s for secret and -p for primary.");
+ goto error;
+ }
+ add_print_opts(&addOpt);
+ /* Try adding the new config to the DB */
+ if (add_transaction_try(&db, &addOpt, conf->home_dir)) {
+ goto error;
+ }
+ if (add_make_link(&addOpt, conf)) {
+ error:
+ close_DB(&db);
+ sERR("Could not complete add transaction.");
+ return -1;
+ }
+ close_DB(&db);
+ hLOG("ckdb updated succesfully.");
+ return 0;
+}
+
+void print_ADD_help() {
+ HELP("ck add PROGRAM_NAME CONFIG_PATH [-p] [-s]");
+}