aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/actionparser.c17
-rw-r--r--src/actions.c40
-rw-r--r--src/actions.h1
-rw-r--r--src/ckutil.c2
-rw-r--r--src/ckutil.h2
-rw-r--r--src/dblayer.c63
-rw-r--r--src/dblayer.h6
-rw-r--r--src/engine.c2
8 files changed, 108 insertions, 25 deletions
diff --git a/src/actionparser.c b/src/actionparser.c
index 0dca98c..756d6d5 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -102,7 +102,16 @@ int parse_DEL(UserOpt *opt) {
return -1;
}
int parse_EDIT(UserOpt *opt) {
- return -1;
+ /* EDIT expects 1 to 2 arguments */
+ if (optNum <= pos || optNum > pos + 2) {
+ opt->err = PERR_EDIT_WRONG;
+ return -1;
+ }
+ opt->argc = optNum - pos;
+ for (int i = 0; i < opt->argc; i++) {
+ get_opt(i, opt);
+ }
+ return 1;
}
int parse_LIST(UserOpt *opt) {
return -1;
@@ -237,7 +246,7 @@ void get_possible_action_strings(char *dest, CkAction ckAction) {
}
void print_parser_error(UserOpt *opt) {
- char errStr[STR_M];
+ char errStr[STR_L];
char names[STR_S];
get_possible_action_strings(names, opt->action);
@@ -251,13 +260,13 @@ void print_parser_error(UserOpt *opt) {
sprintf(errStr, "Initialize database\nUsage: %s version_control_dir secret_dir", names);
break;
case PERR_ADD_WRONG:
- sprintf(errStr, "Add config (new or existing)\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
+ sprintf(errStr, "Add config \nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_DEL_WRONG:
sprintf(errStr, "Delete config or program\nUsage: %s ProgramName ConfigPath [-s](secret) [-p](primary)", names);
break;
case PERR_EDIT_WRONG:
- sprintf(errStr, "Edit config\nUsage: add ProgramName ConfigPath [-s](secret) [-p](primary)");
+ sprintf(errStr, "Edit config with $EDITOR\nUsage: %s ProgramName or configBasename (or both)", names);
break;
case PERR_LIST_WRONG:
sprintf(errStr, "Usage: .........)");
diff --git a/src/actions.c b/src/actions.c
index 318c86b..3a4d8ac 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -111,6 +111,7 @@ int run_ADD(UserOpt * opt, Conf *conf) {
}
add_print_opts(&addOpt);
if (add_transaction_begin(&db, &addOpt) == 0) {
+ close_DB(&db);
return 0;
}
close_DB(&db);
@@ -128,12 +129,41 @@ int run_DEL(UserOpt * opt, Conf *conf) {
return 0;
}
-int run_EDIT(UserOpt * opt, Conf *conf) {
+int run_EDIT(UserOpt *opt, Conf *conf) {
printf("Running %s\n", "edit");
- return 0;
+ DB db = open_DB(opt);
+ if (db.ptr == NULL) {
+ if (db.error == SQL_ERR_NO_TABLES) {
+ PRINT_ERR("The database file is currupted. Run ck init anew.");
+ }
+ return 0;
+ }
+
+ char confPath[STR_M];
+ if (opt->argc == 1) {
+ char confName[STR_M];
+ int secret = 0;
+ if (edit_get_prime_config_from_program(&db, opt->argv[0], confName, &secret) == 1) {
+ str_join_dirname_with_basename(confPath, secret ? conf->SCRT_dir : conf->VC_dir, confName);
+ printf("%s\n", confPath);
+ } else {
+ PRINT_ERR("No primary config");
+ close_DB(&db);
+ return 0;
+ }
+ }
+ close_DB(&db);
+
+ char *editor = getenv("EDITOR");
+ char command[STR_L];
+ strcpy(command, str_is_empty(editor) ? "nano" : editor);
+ strcat(command, " ");
+ strcat(command, confPath);
+ system(command);
+ return 1;
}
-int run_LIST(UserOpt * opt, Conf *conf) {
+int run_LIST(UserOpt *opt, Conf *conf) {
printf("Running %s\n", "list");
DB db = open_DB(opt);
if (db.ptr == NULL) {
@@ -149,12 +179,12 @@ int run_LIST(UserOpt * opt, Conf *conf) {
return 0;
}
-int run_SEARCH(UserOpt * opt, Conf *conf) {
+int run_SEARCH(UserOpt *opt, Conf *conf) {
printf("Running %s\n", "search");
return 0;
}
-int run_HELP(UserOpt * opt, Conf *conf) {
+int run_HELP(UserOpt *opt, Conf *conf) {
printf("Running %s\n", "help");
return 0;
}
diff --git a/src/actions.h b/src/actions.h
index 20677dd..a429a65 100644
--- a/src/actions.h
+++ b/src/actions.h
@@ -43,4 +43,5 @@ struct AddOptions {
int prime;
AddOptErr err;
};
+
#endif /* ACTIONS_H */
diff --git a/src/ckutil.c b/src/ckutil.c
index a77aa01..74a6b00 100644
--- a/src/ckutil.c
+++ b/src/ckutil.c
@@ -83,7 +83,7 @@ int util_symlink_file(const char *path, const char* dest) {
return symlink(path, dest);
}
-void str_make_new_config_name(char *ret, const char *path,
+void str_make_ck_config_name(char *ret, const char *path,
const char *progName) {
char *basec = strdup(path);
char *bname = basename(basec);
diff --git a/src/ckutil.h b/src/ckutil.h
index 9955440..fc7cea2 100644
--- a/src/ckutil.h
+++ b/src/ckutil.h
@@ -47,7 +47,7 @@
/* Create the config name to be used when storing a new config to
* the VC or SCRT dir */
-extern void str_make_new_config_name(char *ret, const char *path,
+extern void str_make_ck_config_name(char *ret, const char *path,
const char *progName);
/* Joins the two strings into ret, with a '/' in between */
diff --git a/src/dblayer.c b/src/dblayer.c
index d9284df..a18d0d0 100644
--- a/src/dblayer.c
+++ b/src/dblayer.c
@@ -230,6 +230,8 @@ int insert_to_rel_table(DB *db, const int pid, const int cid) {
return 1;
}
+/* Returns -2 or error, -1 if program doesn't exist
+ * else the program ID */
int program_exists(DB *db, const char* name) {
sqlite3_stmt *stmt;
int rc;
@@ -279,7 +281,9 @@ int add_insert_relationship(DB *db, const int pid, const int cid) {
return insert_to_rel_table(db, pid, cid);
}
-int program_has_primary_config(DB *db, const int pid) {
+
+/* Returns the path of the found config via *ret */
+int program_has_primary_config(DB *db, const int pid, char *ret, int *sec) {
sqlite3_stmt *stmt;
int rc;
@@ -289,7 +293,13 @@ int program_has_primary_config(DB *db, const int pid) {
strcat(condition, ".");
strcat(condition, COL_PROGRAM_ID);
- dbh_form_query_select_from_joined_eq(sql, COL_CONFIG_PRIMARY, condition);
+ char selection[STR_M] = COL_CONFIG_PRIMARY;
+ strcat(selection, ", ");
+ strcat(selection, COL_CONFIG_PATH);
+ strcat(selection, ", ");
+ strcat(selection, COL_CONFIG_SECRET);
+
+ dbh_form_query_select_from_joined_eq(sql, selection, condition);
rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
@@ -299,13 +309,19 @@ int program_has_primary_config(DB *db, const int pid) {
sqlite3_bind_int(stmt, 1, pid);
int count = 0;
while (sqlite3_step(stmt) == SQLITE_ROW) {
- count += sqlite3_column_int(stmt, 0);
+ if (sqlite3_column_int(stmt, 0) == 1) {
+ if (ret) {
+ strcpy(ret,(char *)sqlite3_column_text(stmt, 1));
+ }
+ if (sec) {
+ *sec = sqlite3_column_int(stmt, 2);
+ }
+ sqlite3_finalize(stmt);
+ return 1;
+ }
}
sqlite3_finalize(stmt);
- if (count == 0) {
- return 0;
- }
- return 1;
+ return 0;
}
int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, const int secret, const int prime) {
@@ -316,7 +332,7 @@ int add_get_or_insert_config_to_db(DB *db, const int pid, const char *path, cons
}
/* If config doesnt exist insert it and return it's cid */
if (cid == -1) {
- if (program_has_primary_config(db, pid) && prime) {
+ if (program_has_primary_config(db, pid, NULL, NULL) && prime) {
db->error = SQL_ERR_PRIMARY_REDEFINITION;
return -1;
}
@@ -345,32 +361,53 @@ int add_transaction_begin(DB *db, const AddOpt * const opt) {
int pid = add_get_or_insert_program_to_db(db, opt->progName);
if (db->error == SQL_ERR_SQLITE) {
PRINT_ERR("Could not insert program to db.\n");
- close_DB(db);
return 0;
}
int cid = add_get_or_insert_config_to_db(db, pid, opt->confPath, opt->secret, opt->prime);
if (db->error == SQL_ERR_SQLITE) {
PRINT_ERR("Could not insert config to db.\n");
- close_DB(db);
return 0;
}
else if (db->error == SQL_CONFIG_PATH_EXISTS) {
PRINT_ERR("This config already exists in the database.\n");
- close_DB(db);
return 0;
}
else if (db->error == SQL_ERR_PRIMARY_REDEFINITION) {
PRINT_ERR("This program already has a primary config.\n");
- close_DB(db);
return 0;
}
add_insert_relationship(db, pid, cid);
if (db->error == SQL_ERR_SQLITE) {
PRINT_ERR("rel update failed\n");
- close_DB(db);
return 0;
}
__END_TRANSACTION__
return 1;
}
+
+int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret) {
+ int pid = program_exists(db, pName);
+ /* error */
+ if (pid == -2) {
+ return -1;
+ }
+
+ /* program exists */
+ if (pid > -1) {
+ char path[STR_M];
+ if (program_has_primary_config(db, pid, path, secret) == 1) {
+ if (!str_is_empty(path)) {
+ if (ret) {
+ char confName[STR_M];
+ str_make_ck_config_name(confName, path, pName);
+ strcpy(ret, confName);
+ }
+ return 1;
+ }
+ }
+ }
+
+ /* No prime config found */
+ return 0;
+}
diff --git a/src/dblayer.h b/src/dblayer.h
index e44ea72..cec6290 100644
--- a/src/dblayer.h
+++ b/src/dblayer.h
@@ -56,4 +56,10 @@ extern DB init_make_DB(const UserOpt *opt);
/*******/
extern int add_transaction_begin(DB *db, const AddOpt * const opt);
+
+/********/
+/* edit */
+/********/
+
+extern int edit_get_prime_config_from_program(DB *db, char *pName, char *ret, int *secret);
#endif /* DBLAYER_H */
diff --git a/src/engine.c b/src/engine.c
index b80c7e8..681cac6 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -38,7 +38,7 @@ void link_config(const AddOpt *opt, const char* newPath) {
void move_config(const AddOpt *opt, const Conf *conf, char *ret) {
char newName[STR_M];
- str_make_new_config_name(newName, opt->confPath, opt->progName);
+ 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);