From da31bfd28301d63571eccb4abdd6b0a65b05c621 Mon Sep 17 00:00:00 2001 From: gramanas Date: Tue, 20 Nov 2018 13:49:36 +0200 Subject: Add export action, fix util_is_link bug --- CMakeLists.txt | 3 +- README.html | 246 +++++++++++++++++++++++++++++++++++++------------------- README.org | 19 ++++- ck.1 | 34 ++++++++ src/add.c | 2 +- src/ckutil.c | 10 +-- src/clparser.c | 2 + src/clparser.h | 1 + src/export.c | 48 +++++++++++ src/restore.c | 2 +- test/export.sh | 28 +++++++ test/restore.sh | 4 +- 12 files changed, 303 insertions(+), 96 deletions(-) create mode 100644 src/export.c create mode 100644 test/export.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index c0700c9..ce41ade 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ project(ck C) # version set(ck_MAJOR_VERSION 0) set(ck_MINOR_VERSION 9) -set(ck_PATCH_VERSION 3) +set(ck_PATCH_VERSION 4) # Feature test macros set(FEATURE_TEST_MACROS "-D_DEFAULT_SOURCE") @@ -70,6 +70,7 @@ set(ckLib_src ${SRC_DIR}/restore.c ${SRC_DIR}/search.c ${SRC_DIR}/help.c + ${SRC_DIR}/export.c ) set(ckLib_hdr diff --git a/README.html b/README.html index 286583d..6c38c37 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + @@ -232,36 +232,36 @@ for the JavaScript code in this tag.

Table of Contents

-
-

ck

-
+
+

ck

+

The Config Keeper

@@ -281,7 +281,7 @@ Have you ever wondered:

-"Jeez Luiz, how can I manage all my configs across my desktop and server?" +"Jeez Luise, how can I manage all my configs across my desktop and server?"

@@ -325,11 +325,16 @@ and instruct it to save them in a different folder, so they won't be in the same place with the normal ones (in the event you want to share your configs with the rest of us).

+ +

+You can even export all ck data with the export action. This will create a +.tar.gz file. +

-
-

Technicalities

-
+
+

Technicalities

+

Upon adding a config to ck, it moves it to the specified folder and adds a symbolic link back where it came from (ln -s). @@ -342,9 +347,9 @@ majority should).

-
-

Download

-
+
+

Download

+

Go ahead and download ck and give it a try. It comes with a help sub-command that explains any inquires you might have. @@ -362,13 +367,13 @@ You can also read the manpage down below.

-
-

build it

+
+

build it

-
-

requirements

-
+
+

requirements

+
  • cmake
  • sqlite3-dev
  • @@ -377,9 +382,9 @@ You can also read the manpage down below.
-
-

make && install

-
+
+

make && install

+

Use -DCMAKE_INSTALL_PREFIX when running cmake to change the install path.

@@ -402,16 +407,16 @@ Use -DCMAKE_INSTALL_PREFIX when running cmake to change the install
-
-

for devs

-
+
+

for devs

+

Please be kind to each other.

-
-

CMake options

-
+
+

CMake options

+

cmake accepts the following options:

@@ -432,9 +437,9 @@ To use any one of them append it after the cmake command like so:
-
-

compiler

-
+
+

compiler

+

Pick your favorite

@@ -464,9 +469,9 @@ Pick your favorite
-
-

tests

-
+
+

tests

+

The testing "suite" is a bash script that runs regression and unit tests. Regression tests are under the tests/ directory @@ -475,9 +480,9 @@ under unit/ directory and test the code.

-
-

run tests

-
+
+

run tests

+

First make sure you build ck with the -DCK_TESTS=1 option. Then go to the build directory and type: @@ -486,12 +491,23 @@ go to the build directory and type:

$ ./test-ck
 
+

+Or if you want to run a particular regression test use the filter option +

+
+
$ ./test-ck -f add
+
+
+

+This will match any test scrits with add in their name. +

-
-

test suite

-
+ +
+

test suite

+
$ ./test-ck -h
 ck test suite
@@ -500,8 +516,10 @@ use without flags to run all tests
 flags:
   -u, --unit            run only the unit tests
   -r, --regression      run only the regression tests
+  -f, --filter  run regression test matching the argument
   -c, --clear           remove test files
                          use if the tests crush unexpectedly
+  -v, --verbose         verbose test output
   -h, --help, *         print this
 
@@ -509,17 +527,17 @@ flags:
-
-

ck configuration

-
+
+

ck configuration

+

See the manpage below.

-
-

Usage

+
+

Usage

ck's goal is to assist with the configuration file management. @@ -529,9 +547,9 @@ ck's goal is to assist with the configuration file management. This section is an example usage.

-
-

Initialize

-
+
+

Initialize

+
cd ~
 # make the directories for the configs
@@ -544,9 +562,9 @@ $ ck init configs/vc configs/sec
 
-
-

Add configs

-
+
+

Add configs

+
# add emacs configs
 ## primary config
@@ -574,9 +592,9 @@ $ sudo ck -c /home/ckuser add ssh /etc/ssh/sshd_config -s
 
-
-

Using the ck actions

-
+
+

Using the ck actions

+
# list the configs in a treelike structure with basename only
 $ ck list tree -b
@@ -628,11 +646,11 @@ $ ck --help e
 
-
-

manpage

+
+

manpage

- + @@ -722,7 +740,8 @@ manage configuration across the system

-

init VERSION_CONTROL_DIR SECRET_DIR

+

init VERSION_CONTROL_DIR +[SECRET_DIR]

@@ -892,6 +911,19 @@ manage configuration across the system

help action

+ + + + + +

ck

+ + + + +

export

+ +

DESCRIPTION @@ -899,8 +931,7 @@ manage configuration across the system

- -

ck +

ck manages configuration files in a Linux system. To that end it provides an action based command line interface.

@@ -908,10 +939,10 @@ interface.

ck needs a database and an rc file to run. It also needs two directories (stored in the rc file), the -VERSION_CONRTOL_DIR and the SECRET_DIR. This -is where the configurations will end up after they are added -to ck. The init action takes care of them. For -more details see the +VERSION_CONRTOL_DIR and (optionally) the +SECRET_DIR. This is where the configurations will end +up after they are added to ck. The init action +takes care of them. For more details see the ACTIONS and FILES sections below.

@@ -1003,7 +1034,10 @@ section above.


Create the ck database (ckdb) and initialize it. Create the ck config file (ckrc) and add the -directory paths to it.
+directory paths to it. If SECRET_DIR is not passed, +the −s flag will be disabled in the add +action, and this ck instance won’t be able to +store secret configs.
USAGE

ck

- - +
+ -

init VERSION_CONTROL_DIR SECRET_DIR

+

init VERSION_CONTROL_DIR +[SECRET_DIR]

@@ -1062,7 +1097,8 @@ exist
.

EXAMPLES

$ ck init /home/ckuser/configs/vc home/ckuser/configs/sec
-$ ck i configs/vc configs/sec
+$ ck i configs/vc configs/sec +$ ck i ~/scripts # no secret dir provided

ADD @@ -1776,6 +1812,52 @@ $ ck h d $ ck --help ls +

EXPORT +CONFIGS
+ck
can export the config files it tracks as well as the +ckrc and ckdb files into a tar.gz archive. The +file is named ck.tar.gz
+USAGE

+ + + + + + + + +
+ + +

ck

+ + +

export

+
+ +

ALIASES

+ + +

export−−export−ex

+ +

ARGUMENTS

+ + + + + + +
+ + +

None.

+
+ +

EXAMPLES

+
$ ck export
+

EXIT STATUS

@@ -1854,7 +1936,7 @@ flags
: @CMAKE_C_FLAGS@

-

Created: 2018-11-19 Mon 01:39

+

Created: 2018-11-20 Tue 13:48

Validate

diff --git a/README.org b/README.org index 756ed3a..fb15d30 100644 --- a/README.org +++ b/README.org @@ -5,16 +5,16 @@ Have you ever wondered: #+BEGIN_QUOTE - "Jeez Luiz, how can I manage all my configs across my desktop and server?" + "Jeez Luise, how can I manage all my configs across my desktop and server?" - -- You + -- You #+END_QUOTE or maybe: #+BEGIN_QUOTE "How can I possibly keep all my configs in sync across computers?" - -- You again + -- You again #+END_QUOTE *ck* is the solution you've been looking for all your life. @@ -33,6 +33,9 @@ and instruct it to save them in a different folder, so they won't be in the same place with the normal ones (in the event you want to share your configs with the rest of us). +You can even export all *ck* data with the =export= action. This will create a +=.tar.gz= file. + ** Technicalities Upon adding a config to *ck*, it moves it to the specified folder and adds a symbolic link back where it came from (=ln -s=). @@ -126,6 +129,12 @@ go to the build directory and type: #+BEGIN_SRC sh $ ./test-ck #+END_SRC +Or if you want to run a particular regression test use the filter option +#+BEGIN_SRC sh +$ ./test-ck -f add +#+END_SRC +This will match any test scrits with =add= in their name. + *** test suite #+BEGIN_SRC sh @@ -136,8 +145,10 @@ use without flags to run all tests flags: -u, --unit run only the unit tests -r, --regression run only the regression tests + -f, --filter run regression test matching the argument -c, --clear remove test files - use if the tests crush unexpectedly + use if the tests crush unexpectedly + -v, --verbose verbose test output -h, --help, * print this #+END_SRC * ck configuration diff --git a/ck.1 b/ck.1 index 9944b3f..020242d 100644 --- a/ck.1 +++ b/ck.1 @@ -91,6 +91,10 @@ ck \- manage configuration across the system .SY ck .BI help \ action .YS +\" Export +.SY ck +.B export +.YS .SH DESCRIPTION .B ck manages configuration files in a Linux system. To that end it provides an @@ -774,6 +778,36 @@ $ ck help add $ ck h d $ ck --help ls .EE +.SS "EXPORT CONFIGS" +.B ck +can export the config files it tracks as well as the +.I ckrc +and +.I ckdb +files into a tar.gz archive. The file is named ck.tar.gz +.TP 2 +.B USAGE +.ns +.RS 2 +.SY ck +.B export +.YS +.RE +.TP 2 +.B ALIASES +.BR export , \ \-\-export , \ \-ex +.TP 2 +.B ARGUMENTS +.ns +.RS 2 +.TP 21 +None. +.RE +.TP 2 +.B EXAMPLES +.EX +$ ck export +.EE .SH EXIT STATUS .B ck shall return 0 if the action was completed without an error, -1 otherwise diff --git a/src/add.c b/src/add.c index 79227d1..c38db24 100644 --- a/src/add.c +++ b/src/add.c @@ -246,7 +246,7 @@ static AddOpt add_make_options(cklist *args, DB *db) { addOpt.err = ADD_ERR_WRONG_CONFIG; return addOpt; } - if (!util_is_file_link(addOpt.confPath)) { + if (util_is_file_link(addOpt.confPath)) { addOpt.err = ADD_ERR_LINK_CONFIG; return addOpt; } diff --git a/src/ckutil.c b/src/ckutil.c index bc86c02..9eed9a5 100644 --- a/src/ckutil.c +++ b/src/ckutil.c @@ -34,7 +34,7 @@ int util_is_dir(const char *path) { } int util_file_exists(const char* path, char *absPath) { - if (!path || !absPath) { + if (!path) { return 0; } struct stat st = {0}; @@ -61,12 +61,12 @@ int util_is_file_link(const char *path) { if (!path) { return 0; } + if (!util_file_exists(path, NULL)) { + return 0; + } struct stat buf; lstat(path, &buf); - if (S_ISLNK(buf.st_mode)) { - return 0; - } - return 1; + return S_ISLNK(buf.st_mode); } void util_mkdir(const char *name) { diff --git a/src/clparser.c b/src/clparser.c index 5b0852b..3a2a9a6 100644 --- a/src/clparser.c +++ b/src/clparser.c @@ -25,6 +25,7 @@ const char* const strLIST[] = {"5", "list", "ls", "l", "-l", "-ls"}; const char* const strSEARCH[] = {"4", "search", "grep", "s", "-s"}; const char* const strHELP[] = {"5", "help", "h", "-?", "-h", "--help"}; const char* const strRESTORE[] = {"3", "restore","r", "-r"}; +const char* const strEXPORT[] = {"3", "export", "ex", "--export"}; const char* const strConfDir[] = {"2", "--config", "-c"}; const char* const strVerbose1[] = {"2", "--verbose", "-v"}; const char* const strVersion[] = {"2", "version", "--version"}; @@ -276,6 +277,7 @@ static void print_parser_help() { ckhelp("List\t%s", get_possible_action_strings(names, CKA_LIST)); ckhelp("Search\t%s", get_possible_action_strings(names, CKA_SEARCH)); ckhelp("Restore\t%s", get_possible_action_strings(names, CKA_RESTORE)); + ckhelp("Export\t%s", get_possible_action_strings(names, CKA_EXPORT)); ckhelp("Help\t%s", get_possible_action_strings(names, CKA_HELP)); report_help(); } diff --git a/src/clparser.h b/src/clparser.h index afb6260..6e4cbea 100644 --- a/src/clparser.h +++ b/src/clparser.h @@ -29,6 +29,7 @@ X(LIST, 1, 6) \ X(SEARCH, 1, 1) \ X(RESTORE, 1, 2) \ + X(EXPORT, 0, 0) \ X(HELP, 1, 1) enum ParseErrors { diff --git a/src/export.c b/src/export.c new file mode 100644 index 0000000..b9f6aa3 --- /dev/null +++ b/src/export.c @@ -0,0 +1,48 @@ +/* export.c - the export action ---------------------------------------*- C -*- + * + * This file is part of ck, the config keeper + * + * ----------------------------------------------------------------------------- + * + * Copyright (C) 2018 Anastasis Grammenos + * GPLv3 (see LICENCE for the full notice) + * + * -------------------------------------------------------------------------- */ +#include + +#include "dblayer.h" +#include "ckerrlog.h" + +ERRLOG(export); + +int run_EXPORT(UserOpt *opt, Conf *conf) { + if (system("which tar > /dev/null 2>&1") != 0) { + ERR("No tar avaliable. Please make sure you have tar installed."); + return -1; + } + + char cmd[STR_L] = "tar -zcvf ck.tar.gz"; + if(conf->scrt_dir) { + strcat(cmd, " -C "); + strcat(cmd, conf->scrt_dir); + strcat(cmd, " ../"); + strcat(cmd, basename(conf->scrt_dir)); + } + strcat(cmd, " -C "); + strcat(cmd, conf->vc_dir); + strcat(cmd, " ../"); + strcat(cmd, basename(conf->vc_dir)); + strcat(cmd, " -C "); + strcat(cmd, opt->confDir); + strcat(cmd, " ckrc -C "); + strcat(cmd, opt->confDir); + strcat(cmd, " ckdb"); + + hLOG("Running: %s", cmd); + strcat(cmd, " > /dev/null 2>&1"); + return system(cmd); +} + +void print_EXPORT_help() { + HELP("ck export"); +} diff --git a/src/restore.c b/src/restore.c index 3aff790..a18f906 100644 --- a/src/restore.c +++ b/src/restore.c @@ -23,7 +23,7 @@ static int restore_make_links(cklist *from, cklist *to) { && list_size(from) == list_size(to)) { do { if (util_file_exists(list_get(to), NULL) - || !util_is_file_link(list_get(to))) { + || util_is_file_link(list_get(to))) { ERR("File %s already exists.", list_get(to)); sERR("No links were created."); return -1; diff --git a/test/export.sh b/test/export.sh new file mode 100644 index 0000000..8daf76a --- /dev/null +++ b/test/export.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +init export + +# add configs to ck +path1=$BIN/test1.conf +path2=$BIN/test2.conf +path3=$BIN/test3.conf +path4=$BIN/test4.conf +path5=$BIN/test5.conf + +add_config prog1 $path1 +add_config prog1 $path2 +add_config prog2 $path3 +add_config prog2 $path4 +add_config prog3 $path5 + +# export +exec $BIN/ck -c $BIN export >&${V} & +wait $! + +if [ ! -f $BIN/ck.tar.gz ]; then + err "tar.gz was not created" +fi + +rm $BIN/ck.tar.gz +clear_tests +echo -e $PASS diff --git a/test/restore.sh b/test/restore.sh index 2d60cb9..ba131e4 100644 --- a/test/restore.sh +++ b/test/restore.sh @@ -23,7 +23,7 @@ wait $! for i in $($BIN/ck -c $BIN list -p prog1); do if [[ ! -L "$i" ]]; then - err "Couldn't restore path $i" + err "Couldn't restore path $i from -p prog1" exit 1 fi done @@ -37,7 +37,7 @@ wait $! for i in $($BIN/ck -c $BIN list paths); do if [[ ! -L "$i" ]]; then - err "Couldn't restore path $i" + err "Couldn't restore path $i from all" exit 1 fi done -- cgit v1.2.3