aboutsummaryrefslogtreecommitdiffstats
path: root/src/actions.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/actions.c')
-rw-r--r--src/actions.c93
1 files changed, 79 insertions, 14 deletions
diff --git a/src/actions.c b/src/actions.c
index 4e9e4ca..77680eb 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -8,16 +8,14 @@
* GPLv3 (see LICENCE for the full notice)
*
* -------------------------------------------------------------------------- */
-#include <stdio.h>
-
#include "actions.h"
#include "dblayer.h"
-
+#include "ckutil.h"
int run_INIT(UserOpt * opt, Conf *conf) {
if (db_exists(opt)) {
- printf("conf dir: %s\n", opt->confDir);
- printf("ck is already initialized.\n");
+ printf("Current configuration file location: %s\n", opt->confDir);
+ PRINT_ERR("ck is already initialized.\n");
return 0;
}
if (init_create_config_file(opt)) {
@@ -31,21 +29,89 @@ int run_INIT(UserOpt * opt, Conf *conf) {
return 1;
}
+AddOpt make_add_options(const int argc, char **argv) {
+ AddOpt addOpt = {
+ .progName = argv[0],
+ .confPath = NULL,
+ .secret = 0,
+ .prime = 0,
+ .err = ADD_NO_ERR
+ };
+
+ /* the first two argumens have to exist since we are here */
+ if (!util_is_file_rw(argv[1])) {
+ addOpt.err = ADD_ERR_WRONG_CONFIG;
+ return addOpt;
+ }
+ addOpt.confPath = argv[1];
+
+ if (argc == 3) {
+ if (strcmp(argv[2], "-s") == 0) {
+ addOpt.secret = 1;
+ } else if (strcmp(argv[2], "-p") == 0) {
+ addOpt.prime = 1;
+ } else {
+ addOpt.err = ADD_ERR_WRONG_FLAGS;
+ return addOpt;
+ }
+ } else if (argc == 4) {
+ if (strcmp(argv[2], "-s") == 0) {
+ addOpt.secret = 1;
+ } else if (strcmp(argv[2], "-p") == 0) {
+ addOpt.prime = 1;
+ } else {
+ addOpt.err = ADD_ERR_WRONG_FLAGS;
+ return addOpt;
+ }
+ if (strcmp(argv[3], "-s") == 0) {
+ addOpt.secret = 1;
+ } else if (strcmp(argv[3], "-p") == 0) {
+ addOpt.prime = 1;
+ } else {
+ addOpt.err = ADD_ERR_WRONG_FLAGS;
+ return addOpt;
+ }
+ }
+ 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");
+ }
+}
+
int run_ADD(UserOpt * opt, Conf *conf) {
- printf("Running %s\n", "add");
DB db = open_DB(opt);
if (db.ptr == NULL) {
if (db.error == SQL_ERR_NO_TABLES) {
- printf("no tables\n");
+ PRINT_ERR("The database file is currupted. Run ck init anew.\n");
}
}
- for (int i = 0; i < opt->argc; i++) {
- printf("[%d]: %s\n", i, opt->argv[i]);
+ AddOpt addOpt = make_add_options(opt->argc, opt->argv);
+ switch (addOpt.err) {
+ case ADD_NO_ERR:
+ break;
+ case ADD_ERR_WRONG_CONFIG:
+ PRINT_ERR("The config file specified doesn't exist.\n");
+ close_DB(&db);
+ return 0;
+ case ADD_ERR_WRONG_FLAGS:
+ PRINT_ERR("Flags are: -s for secret and -p for primary.\n");
+ close_DB(&db);
+ return 0;
}
- // figure out user opt
- if (add_transaction_begin(&db, opt->argv[0], opt->argv[1], 0, 0) == 0) {
+ add_print_opts(&addOpt);
+ if (add_transaction_begin(&db, addOpt.progName,
+ addOpt.confPath, addOpt.secret, addOpt.prime) == 0) {
return 0;
}
+ // do the linking
close_DB(&db);
return 1;
}
@@ -88,16 +154,15 @@ int run_HELP(UserOpt * opt, Conf *conf) {
void print_INIT_result(int ok) {
if (ok) {
printf("Initialized empty ckdb.\n");
- return;
}
}
void print_ADD_result(int ok) {
if (ok) {
- printf("succes\n");
+ printf("ckdb updated succesfully.\n");
return;
}
- printf("failure\n");
+ printf("Could not complete add transaction.\n");
}
void print_DEL_result(int ok) {