From 5683c51d17b5eed7d2f070aa4e49cc21b65d82e5 Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 29 Oct 2018 17:20:28 +0200 Subject: Add list with basename and update readme --- CMakeLists.txt | 2 +- README.html | 657 +++++++++++++++++------------------------------------ README.org | 281 +++++------------------ ck.1 | 80 +++++++ src/actionhelper.c | 8 +- src/actionparser.c | 6 +- src/actions.c | 8 +- src/actions.h | 1 + src/dblayer.c | 36 ++- src/dblayer.h | 6 +- 10 files changed, 407 insertions(+), 678 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e9d9e9..b0b3fbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ project(ck C) # version set(ck_MAJOR_VERSION 0) set(ck_MINOR_VERSION 8) -set(ck_PATCH_VERSION 2) +set(ck_PATCH_VERSION 3) # Feature test macros set(FEATURE_TEST_MACROS "-D_DEFAULT_SOURCE") diff --git a/README.html b/README.html index f6bd550..655204c 100644 --- a/README.html +++ b/README.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + @@ -232,53 +232,46 @@ for the JavaScript code in this tag.

Table of Contents

-
-

ck

-
+
+

ck

+

The Config Keeper

@@ -334,9 +327,9 @@ with the rest of us).

-
-

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). @@ -349,9 +342,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. @@ -363,19 +356,19 @@ the build section.

-You can also read the manual down below. +You can also read the manpage down below.

-
-

build it

+
+

build it

-
-

requirements

-
+
+

requirements

+
  • cmake
  • sqlite3-dev
  • @@ -384,24 +377,24 @@ You can also read the manual down below.
-
-

make && install

-
+
+

make && install

+

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

-
# clone the repo
+
# clone the repo
 > cd ~/code; git clone https://gitlab.com/grm-grm/ck
-# make a build directory and enter it
-> mkdir ~/ck_build; cd ~/ck_build;
-# run cmake
+# make a build directory and enter it
+> mkdir ~/ck_build; cd ~/ck_build;
+# run cmake
 > cmake ~/code/ck 
-# run make
+# run make
 > make
-# install it
+# install it
 > make install
-# run ck
+# run ck
 > ck
 
@@ -409,23 +402,23 @@ 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:

-
option(CK_DEBUG "Build with debug symbols, asan and warnings")
-option(CK_TESTS "Make the tests")
-option(CK_SHARED "Build with shared lib")
+
option(CK_DEBUG "Build with debug symbols, asan and warnings")
+option(CK_TESTS "Make the tests")
+option(CK_SHARED "Build with shared lib")
 
@@ -439,41 +432,41 @@ To use any one of them append it after the cmake command like so:
-
-

compiler

-
+
+

compiler

+

Pick your favorite

-
> export CC=clang
-# or
-> export CC=gcc
+
> export CC=clang
+# or
+> export CC=gcc
 
-
# clone the repo
+
# clone the repo
 > cd ~/code; git clone https://gitlab.com/grm-grm/ck
-# make a build directory and enter it
-> mkdir ~/ck_build; cd ~/ck_build;
-# run cmake
+# make a build directory and enter it
+> mkdir ~/ck_build; cd ~/ck_build;
+# run cmake
 > cmake -DCK_DEBUG=1 -DCK_TESTS=1 ~/code/ck
-# run make
+# run make
 > make
-# check ck
+# check ck
 > ./test-ck
-# run ck
+# run ck
 > ./ck
 
-
-

tests

-
+
+

tests

+

The testing "suite" is a bash script that runs regression and unit tests. Regression tests are under the tests/ directory @@ -482,9 +475,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: @@ -496,9 +489,9 @@ go to the build directory and type:

-
-

test suite

-
+
+

test suite

+
$ ./test-ck -h
 ck test suite
@@ -516,400 +509,124 @@ flags:
 
-
-

manual

-
-

-ck's goal is to assist with the configuration file management. -To that end it tries to provides a cli interface that is pretty straight-forward -and intuitive. -

- +
+

ck configuration

+

-Example usage: +See the manpage below.

-
-
# initialize new ck
-$ ck init /path_to/where_you_want/your_configs/to_be \
-  /path_to/the_secret/directory
-
-# add emacs configs
-## primary config
-$ ck add emacs ~/.emacs.d/orgconf.org -p
-## secret config, with passwords and naughty words
-$ ck add emacs ~/.emacs.d/accounts.org -s
-## and another one for emacs
-$ ck add emacs ~/.emacs.d/init.el
-
-# add tmux config
-$ ck add tmux ~/.tmux.conf -p
-
-# list the configs
-$ ck list tree
-$ ck list paths -t lisp
-$ ck list programs -t python
-$ ck list -p emacs
-
-# search the configs
-$ ck search search-term
-$ ck search "\"search term with spaces\""
-$ ck search "\(" #escape symbols
-
- -

-The first command after typing ck is the action you wish to perform. Actions are -a very important concept of ck. With an action you can initialize ck, add/delete -configuration files to/from it, edit them, list them in various ways and search in their content. -

- -

-Actions expect their arguments in the order specified below. This is done to reduce the amount -of flags one has to pass to ck. -

-
-

ck configuration

-
-

-ck uses sqlite to index the configuration files. The init -action creates a .ck directory (under $HOME) -in witch the ckrc and the ckdb reside. The first one contains -the two directories described above while the other one is the -sqlite db. -

- -

-One can have multiple config directories with different configurations -each. -

- +
+

Usage

+

-Using the special keyword config (or -c) you can set the path -in which ck will search for the .ck directory. -

- -

-You can prefix every action with this and ck will use the configuration -directory of your choice. +ck's goal is to assist with the configuration file management.

-Usage: +This section is an example usage.

-
-
$ ck config ~/ ... # the default behaviour
-
-# /someplace/else must exist or
-# the action following it must be init
-$ ck -c /someplace/else ... 
-
-
-
- -
-

Actions

-
-
-
-

init

-
-

-or i or -i -

- -

-init takes exactly 2 arguments. -

- -
    -
  • config_dir: where all the configs will live
  • -
  • secret_dir: where all the secret configs will live
  • -
- -

-Use init to initialize a new ck database. -

- -

-Usage: -

+
+

Initialize

+
-
# initialize new ck
-$ ck init /path_to/where_you_want/your_configs/to_be \
-  /path_to/the_secret/directory
-
-
-
-
- -
-

add

-
-

-or a or -a -

- -

-Adds a configuration to the ck database. -Add takes 2 to 4 arguments. -

- -
    -
  • program_name: the name of the program you add a config to
  • -
  • config_path: the path to the config
  • -
  • Optional: (order doesn't matter) -
      -
    • -p: the config will be the primary (relevant on edit below)
    • -
    • -s: the config will be stored in the secret_dir
    • -
  • -
- -

-Keep in mind: -

-
    -
  • The config has to exist.
  • -
  • If you are adding a config to a program already existing in ckdb make -sure to use the same name.
  • -
  • Each program can have only one primary config.
  • -
+
cd ~
+# make the directories for the configs
+$ mkdir -p configs/vc configs/sec
 
-

-Usage: -

-
-
# add config to ck
-$ ck add program_name config_path [-s] [-p]
+# initialize new ck
+$ ck init configs/vc configs/sec
 
-
-

list

-
-

-or ls or l or -l -

- -

-List stuff ck knows about. -

- -

-You can use the keywords: -

-
    -
  • paths: to print all the paths ck tracks
  • -
  • programs: to print all the programs ck tracks
  • -
  • -p progName: (without the "<>") to print the paths of a specific program
  • -
- -

-With the flag -t and then one of the follwing types one can change -the way the list is printed: -

-
    -
  • plain: simple listing (default)
  • -
  • python: print like a python list
  • -
  • lisp: print like a lisp list
  • -
- -

-Using the keyword tree ck can list the configurations under their -corresponding program, in a treelike structure. -

- -

-Passing the -a flag will enable the listing of config attributes (secret or primary). -It is best used with tree or plain paths. -

- -

-With the keyword ckconf ck will list it's own configuration values (in ckrc). -

- -

-Usage: -

+
+

Add configs

+
-
# list tree structure, with attributes
-$ ck list tree -a
-# list paths in python
-$ ck l paths -t python
-# list programs in lisp
-$ ck ls programs -t lisp
-# list emacs' configurations [with attributes]
-$ ck ls -p emacs [-a]
-# list bash configurations in lisp
-$ ck ls -p bash -t lisp
-# list ck configuration
-$ ck -l ckconf
+
# add emacs configs
+## primary config
+$ ck add emacs .emacs.d/orgconf.org -p
+## secret config, with passwords and naughty words
+$ ck add emacs .emacs.d/accounts.org -s
+## and another one for emacs
+$ ck add emacs .emacs.d/init.el
+
+# add tmux config
+$ ck add tmux .tmux.conf -p
+
+# add X configs
+$ ck add X .xinitrc
+$ ck add X .Xresources
+
+# add ssh configs (secret)
+$ ck add ssh .ssh/config -s -p
+$ ck add ssh .ssh/authorized_keys -s
+# When running with sudo, we need to specify the ck config
+# location.
+$ sudo ck -c /home/ckuser add ssh /etc/ssh/sshd_config -s
 
-
-

search

-
-

-or grep or s or -s -

- -

-Perform infile grep in all the configurations ck keeps track of. -

- -

-Takes one argument, the search-term. -

- -

-To search for terms with spaces you have to put them in quotes. -

- -

-Usage: -

+
+

Using the ck actions

+
-
# search for parenthesis
-$ ck search \(
-# search term with spaces
-$ ck grep "This is a space"
-# both
-$ ck s "(add 2 4)"
-# and a normal one
-$ ck -s alias
-
-
- -

-If you want to use more advanced grep techniques or even -a different pattern matching program you can do it like so: -

- -
-
# with xargs
-$ ck ls paths | xargs grep -E 'A|B'
-# or in bash
-$ for i in $(ck ls paths); do grep -E 'A|B' $i; done
-# or in zsh
-$ for i ($(ck ls paths)) grep -E 'A|B' $i
-
-
-
-
- -
-

edit

-
-

-or e or -e -

- -

-Edit configurations with $EDITOR. -

- -

-Edit takes at least one and up to two arguments. -

+
# list the configs in a treelike structure with basename only
+$ ck list tree -b
+# or with the full path & attributes
+$ ck list tree -a
 
-

-The first argument is the programName. If the program has a primary configuration -edit will open this. If the program has only one configuration edit will open it. -If the program has more than 1 configurations and no primary, it will print the -avaliable configurations and exit. -

+# list only the paths in python or lisp like lists +$ ck list paths -t lisp +$ ck list programs -t python -b -a -

-The second argument is the configName. If it exists it will open, else it will -print the avaliable configurations and exit. -

+# list emacs configs +$ ck list -p emacs -

-Usage: -

-
-
# suppose this is our ck instance
-$ ck list tree -a
-emacs:
-|- init.el
-|- accounts.el [s]
-|- orgconf.org [p]
+# search the configs
+$ ck search Hostname
+$ ck search "search term with spaces"
+# escape symbols
+$ ck search \(
 
-# edit the primary emacs config
+# edit the primary config of emacs
 $ ck edit emacs
-
-# edit a specific emacs config, other than the primary
-$ ck edit emacs accounts.el
+# edit a non-primary config of ssh
+$ ck e ssh authorized_keys
+# edit a root config
+$ sudo ck -c /home/ckuser e ssh sshd_config
+
+# delete a program with all the configs
+$ ck delete emacs
+# or a specific config
+$ ck del -c /home/ckuser/.emacs.d/init.el
+
+# restore all links (on a new instalation)
+$ ck restore all
+# restore a program's links
+$ ck r -p emacs
+
+# get help for an action
+$ ck h add
+$ ck --help e
 
- -
-

restore

-
-

-or r or -r -

- -

-Restore links. -

- -

-Given a working ck instance (ckdb + ckrc + directories in ckrc with configs) -restore shall recreate the links from the config directories in ckrc -back to their corresponding position when added in ck. -

- -

-It is useful for copying your configs to a new linux installation -or restoring deleted links. -

- -

-It can either restore a specific program or all of them: -

-
-
# restore progName
-ck restore -p progName
-# restore all
-ck r all
-
-

-Note: -If ck tracks configs that are owned by root, simply running -`ck restore …` will fail due to permissions. To remedy this, ck will alter the -owner and group of a link to match the one in the ckrc directories. -Thus, running `sudo ck -c home/myuser.ck restore ..` will restore -the root user's links as it should and the user links will have -the user as the owner instead of the root. -

- -

-ck checks that the configs exist and that the location for the link -is avaliable before making any links. However, in the even that symlink -fails for some other reason, the process will stop as is. Make sure you -take care of the already created links, if that's the case. -

-
-
-
-
-
-

manpage

-
+
+

manpage

+
- + @@ -933,6 +650,7 @@ take care of the already created links, if that's the case. NAME
SYNOPSIS
DESCRIPTION
+CONFIGURATION
OPTIONS
ACTIONS
EXIT STATUS
@@ -968,11 +686,12 @@ ck add PROGRAM_NAME CONFIG_PATH [−p] ck delete PROGRAM_NAME|{−c CONFIG_PATH}
-ck list tree 
[−a]
+ck list tree 
[−a] [−b] +
ck list
{-p PROGRAM_NAME}|programs|paths [−t {plain|python|lisp}] [−a] -
+[−b]
ck list ckconf
ck edit
PROGRAM_NAME [CONFIG_BASENAME]
@@ -1015,6 +734,26 @@ you can sync the VERSION_CONRTOL_DIR and given these two directories and the correspondig rc file and database.

+

CONFIGURATION + +

+ + +

ck uses +sqlite to index the configuration files. The init +action by default creates a directory named .ck +under $HOME in witch the ckrc and ckdb +files reside. See the FILES section for more +details.

+ +

One can have +multiple config directories with different +configurations each. Using the +config or −c option one can +set the path in which ck will search for ckrc and +ckdb. See the OPTIONS section for more +details.

+

OPTIONS

@@ -1204,11 +943,11 @@ List programs, configs and ck configuration values.

USAGE

ck list -tree [−a]
+tree 
[−a] [−b]
ck list
{-p PROGRAM_NAME}|programs|paths [−t {plain|python|lisp}] [−a] -
+[−b]
ck list ckconf

@@ -1269,6 +1008,12 @@ or lisp to print it like a lisp list.

[root] if the file is owned by the root user.

+

−b

+ +

Print the config +basename instead of the full path.

+ +

EXAMPLES

$ ck list tree -a
@@ -1374,6 +1119,32 @@ installation or restoring deleted links. It can either restore a specific program or all of them.

+

NOTE: If +ck tracks configs that are owned by root, +simply running

+ +

$ ck restore ...

+ +

will fail due to permissions. +To remedy this, ck will alter the +owner and group of a link to match +the one in the original config file. Thus, +running

+ +

$ sudo ck -c /home/ckuser/.ck +restore ...

+ +

will restore the root +user’s links as it should and the user links will have +the user as the owner instead of the root.

+ +

ck +checks that the configs exist and that the location +for the link is avaliable before making any links. However, +in the even that symlink fails for some other reason, +the process will stop as is. The user will have to take care +of the already created links, if that’s the case.

+

USAGE

@@ -1514,7 +1285,7 @@ flags
: @CMAKE_C_FLAGS@

-

Created: 2018-10-28 Sun 13:22

+

Created: 2018-10-29 Mon 17:20

Validate

diff --git a/README.org b/README.org index 379e61c..3d05056 100644 --- a/README.org +++ b/README.org @@ -47,7 +47,7 @@ that explains any inquires you might have. Grab the latest zip/tarball from the tag section in the [[https://ubuntos.dynu.net/git/ck][repo]] and proceed to the [[#build-instructions][build]] section. -You can also read the manual [[#manual][down below]]. +You can also read the manpage [[#manpage][down below]]. * build it :PROPERTIES: @@ -140,254 +140,97 @@ flags: use if the tests crush unexpectedly -h, --help, * print this #+END_SRC -* manual +* ck configuration +See the [[#manpage][manpage]] below. + +* Usage :PROPERTIES: - :CUSTOM_ID: manual + :CUSTOM_ID: usage :END: ck's goal is to assist with the configuration file management. -To that end it tries to provides a cli interface that is pretty straight-forward -and intuitive. -Example usage: +This section is an example usage. +** Initialize #+BEGIN_SRC sh + cd ~ + # make the directories for the configs + $ mkdir -p configs/vc configs/sec + # initialize new ck - $ ck init /path_to/where_you_want/your_configs/to_be \ - /path_to/the_secret/directory + $ ck init configs/vc configs/sec +#+END_SRC +** Add configs +#+BEGIN_SRC sh # add emacs configs ## primary config - $ ck add emacs ~/.emacs.d/orgconf.org -p + $ ck add emacs .emacs.d/orgconf.org -p ## secret config, with passwords and naughty words - $ ck add emacs ~/.emacs.d/accounts.org -s + $ ck add emacs .emacs.d/accounts.org -s ## and another one for emacs - $ ck add emacs ~/.emacs.d/init.el + $ ck add emacs .emacs.d/init.el # add tmux config - $ ck add tmux ~/.tmux.conf -p - - # list the configs - $ ck list tree - $ ck list paths -t lisp - $ ck list programs -t python - $ ck list -p emacs - - # search the configs - $ ck search search-term - $ ck search "\"search term with spaces\"" - $ ck search "\(" #escape symbols -#+END_SRC - -The first command after typing *ck* is the action you wish to perform. Actions are -a very important concept of ck. With an action you can initialize *ck*, add/delete -configuration files to/from it, edit them, list them in various ways and search in their content. - -Actions expect their arguments in the order specified below. This is done to reduce the amount -of flags one has to pass to ck. - -** ck configuration -ck uses sqlite to index the configuration files. The init -action creates a *.ck* directory (under =$HOME=) -in witch the *ckrc* and the *ckdb* reside. The first one contains -the two directories described above while the other one is the -sqlite db. - -One can have multiple config directories with different configurations -each. - -Using the special keyword *config* (or *-c*) you can set the path -in which ck will search for the *.ck* directory. - -You can prefix every action with this and ck will use the configuration -directory of your choice. + $ ck add tmux .tmux.conf -p -Usage: -#+BEGIN_SRC sh - $ ck config ~/ ... # the default behaviour - - # /someplace/else must exist or - # the action following it must be init - $ ck -c /someplace/else ... -#+END_SRC - -** Actions -*** init -or i or -i + # add X configs + $ ck add X .xinitrc + $ ck add X .Xresources -init takes exactly 2 arguments. - -- *config_dir*: where all the configs will live -- *secret_dir*: where all the secret configs will live - -Use init to initialize a new ck database. - -Usage: -#+BEGIN_SRC sh - # initialize new ck - $ ck init /path_to/where_you_want/your_configs/to_be \ - /path_to/the_secret/directory + # add ssh configs (secret) + $ ck add ssh .ssh/config -s -p + $ ck add ssh .ssh/authorized_keys -s + # When running with sudo, we need to specify the ck config + # location. + $ sudo ck -c /home/ckuser add ssh /etc/ssh/sshd_config -s #+END_SRC -*** add -or a or -a - -Adds a configuration to the ck database. -Add takes 2 to 4 arguments. - -- *program_name*: the name of the program you add a config to -- *config_path*: the path to the config -- Optional: (order doesn't matter) - + *-p*: the config will be the primary (relevant on edit below) - + *-s*: the config will be stored in the secret_dir - -Keep in mind: -- The config has to exist. -- If you are adding a config to a program already existing in ckdb make - sure to use the same name. -- Each program can have only one primary config. - -Usage: +** Using the ck actions #+BEGIN_SRC sh - # add config to ck - $ ck add program_name config_path [-s] [-p] -#+END_SRC - -*** list -or ls or l or -l - -List stuff ck knows about. - -You can use the keywords: -- *paths*: to print all the paths ck tracks -- *programs*: to print all the programs ck tracks -- *-p progName*: (without the "<>") to print the paths of a specific program - -With the flag *-t* and then one of the follwing types one can change -the way the list is printed: -- *plain*: simple listing (default) -- *python*: print like a python list -- *lisp*: print like a lisp list - -Using the keyword *tree* ck can list the configurations under their -corresponding program, in a treelike structure. - -Passing the *-a* flag will enable the listing of config attributes (secret or primary). -It is best used with tree or plain paths. - -With the keyword *ckconf* ck will list it's own configuration values (in ckrc). - -Usage: -#+BEGIN_SRC sh - # list tree structure, with attributes + # list the configs in a treelike structure with basename only + $ ck list tree -b + # or with the full path & attributes $ ck list tree -a - # list paths in python - $ ck l paths -t python - # list programs in lisp - $ ck ls programs -t lisp - # list emacs' configurations [with attributes] - $ ck ls -p emacs [-a] - # list bash configurations in lisp - $ ck ls -p bash -t lisp - # list ck configuration - $ ck -l ckconf -#+END_SRC -*** search -or grep or s or -s - -Perform infile grep in all the configurations ck keeps track of. - -Takes one argument, the *search-term*. + # list only the paths in python or lisp like lists + $ ck list paths -t lisp + $ ck list programs -t python -b -a -To search for terms with spaces you have to put them in quotes. + # list emacs configs + $ ck list -p emacs -Usage: -#+BEGIN_SRC sh - # search for parenthesis + # search the configs + $ ck search Hostname + $ ck search "search term with spaces" + # escape symbols $ ck search \( - # search term with spaces - $ ck grep "This is a space" - # both - $ ck s "(add 2 4)" - # and a normal one - $ ck -s alias -#+END_SRC -If you want to use more advanced grep techniques or even -a different pattern matching program you can do it like so: + # edit the primary config of emacs + $ ck edit emacs + # edit a non-primary config of ssh + $ ck e ssh authorized_keys + # edit a root config + $ sudo ck -c /home/ckuser e ssh sshd_config -#+BEGIN_SRC sh - # with xargs - $ ck ls paths | xargs grep -E 'A|B' - # or in bash - $ for i in $(ck ls paths); do grep -E 'A|B' $i; done - # or in zsh - $ for i ($(ck ls paths)) grep -E 'A|B' $i -#+END_SRC - -*** edit -or e or -e - -Edit configurations with =$EDITOR=. - -Edit takes at least one and up to two arguments. + # delete a program with all the configs + $ ck delete emacs + # or a specific config + $ ck del -c /home/ckuser/.emacs.d/init.el -The first argument is the *programName*. If the program has a primary configuration -edit will open this. If the program has only one configuration edit will open it. -If the program has more than 1 configurations and no primary, it will print the -avaliable configurations and exit. + # restore all links (on a new instalation) + $ ck restore all + # restore a program's links + $ ck r -p emacs -The second argument is the *configName*. If it exists it will open, else it will -print the avaliable configurations and exit. - -Usage: -#+BEGIN_SRC sh -# suppose this is our ck instance -$ ck list tree -a -emacs: -|- init.el -|- accounts.el [s] -|- orgconf.org [p] - -# edit the primary emacs config -$ ck edit emacs - -# edit a specific emacs config, other than the primary -$ ck edit emacs accounts.el + # get help for an action + $ ck h add + $ ck --help e #+END_SRC -*** restore -or r or -r - -Restore links. - -Given a working ck instance (ckdb + ckrc + directories in ckrc with configs) -restore shall recreate the links from the config directories in ckrc -back to their corresponding position when added in ck. - -It is useful for copying your configs to a new linux installation -or restoring deleted links. - -It can either restore a specific program or all of them: -#+BEGIN_SRC sh - # restore progName - ck restore -p progName - # restore all - ck r all -#+END_SRC - -Note: -If ck tracks configs that are owned by root, simply running -`ck restore ...` will fail due to permissions. To remedy this, ck will alter the -owner and group of a link to match the one in the ckrc directories. -Thus, running `sudo ck -c /home/myuser/.ck restore ..` will restore -the root user's links as it should and the user links will have -the user as the owner instead of the root. - -ck checks that the configs exist and that the location for the link -is avaliable before making any links. However, in the even that symlink -fails for some other reason, the process will stop as is. Make sure you -take care of the already created links, if that's the case. * manpage + :PROPERTIES: + :CUSTOM_ID: manpage + :END: #+BEGIN_SRC sh :results output html :exports results groff ck.1 -mandoc -Thtml #+END_SRC diff --git a/ck.1 b/ck.1 index dd45d4f..d23d889 100644 --- a/ck.1 +++ b/ck.1 @@ -24,11 +24,13 @@ ck \- manage configuration across the system .br .B ck list .BR tree \ [ \-a ] +.RB [ \-b ] .br .B ck list .RB { -p \ \fIPROGRAM_NAME\fR}\fR| programs | paths \ [ \-t .RB { plain | python | lisp } \fR] .RB [ \-a ] +.RB [ \-b ] .br .B ck list ckconf .br @@ -87,6 +89,39 @@ and You can also .B restore the links given these two directories and the correspondig rc file and database. + +.SH CONFIGURATION +.B ck +uses +.B sqlite +to index the configuration files. The +.B init action +by default creates a directory named +.I .ck +under +.I $HOME +in witch the +.I ckrc +and +.I ckdb +files reside. See the +.B FILES +section for more details. +.P +One can have multiple +.B config directories +with different configurations each. +Using the +.BR config \ or \ \-c +option one can set the path +in which ck will search for +.I ckrc +and +.IR ckdb . +See the +.B OPTIONS +section for more details. +.P .SH OPTIONS Change .B ck @@ -310,11 +345,13 @@ List programs, configs and ck configuration values. .in +.2i .B ck list .BR tree \ [ \-a ] +.RB [ \-b ] .br .B ck list .RB { -p \ \fIPROGRAM_NAME\fR}\fR| programs | paths \ [ \-t .RB { plain | python | lisp } \fR] .RB [ \-a ] +.RB [ \-b ] .br .B ck list ckconf .br @@ -394,6 +431,13 @@ and .B [root] if the file is owned by the root user. .P +.in +.2i +.B \-b +.in +.2i +Print the +.B config +basename instead of the full path. +.P .B EXAMPLES .in +.2i $ ck list tree -a @@ -525,6 +569,42 @@ a specific .B program or all of them. .P +.BR NOTE : +If +.B ck +tracks +.B configs +that are owned by root, simply running +.br +.in +.2i +$ ck restore \.\.\. +.in +.br +will fail due to permissions. To remedy this, +.B ck +will alter the +.BR owner \ and \ group +of a link to match the one in the original +.B config +file. +Thus, running +.in +.2i +$ sudo ck -c /home/ckuser/.ck restore \.\.\. +.in +will +.B restore +the root user's links as it should and the user links will have +the user as the owner instead of the root. +.P +.B ck +checks that the +.B configs +exist and that the location for the link +is avaliable before making any links. However, in the even that +.B symlink +fails for some other reason, the process will stop as is. The user will have to +take care of the already created links, if that's the case. +.P .B USAGE .br .in +.2i diff --git a/src/actionhelper.c b/src/actionhelper.c index 6428ab0..5899024 100644 --- a/src/actionhelper.c +++ b/src/actionhelper.c @@ -202,6 +202,7 @@ ListOpt list_make_options(cklist *args) { ._lst = LST_PLAIN, .pName = NULL, .attr = 0, + .bName = 0, .err = 0 }; @@ -211,6 +212,10 @@ ListOpt list_make_options(cklist *args) { listOpt.attr = 1; continue; } + if (strcmp(list_get(args), "-b") == 0) { + listOpt.bName = 1; + continue; + } if (strcmp(list_get(args), "-t") == 0) { if (!list_next(args)) { listOpt.err = 1; @@ -421,7 +426,8 @@ void print_LIST_help() { ckhelp(" `-p program_name`: list only the configs of the specified program."); ckhelp(" `ckconf`: list the ck configuration values.\n"); ckhelp("There are also some flags:"); - ckhelp(" `-a`: add the attributes next to the config paths (secret,primary)"); + ckhelp(" `-a`: add the attributes next to the config paths (secret,primary,root)"); + ckhelp(" `-b`: print the config basenames instead of the full path"); ckhelp(" `-t`: change the list type. (does not work with tree or ckconf)"); ckhelp(" `plain`: default plain listing"); ckhelp(" `python`: enclose the list in [ , ]"); diff --git a/src/actionparser.c b/src/actionparser.c index f04c1d5..1648372 100644 --- a/src/actionparser.c +++ b/src/actionparser.c @@ -123,9 +123,9 @@ int parse_EDIT(UserOpt *opt) { } int parse_LIST(UserOpt *opt) { - /* List expects 1 to 5 arguments */ + /* List expects 1 to 6 arguments */ if (optNum < pos + 1 - || optNum > pos + 5) { + || optNum > pos + 6) { opt->err = PERR_LIST_WRONG; return -1; } @@ -360,7 +360,7 @@ void print_parser_error(UserOpt *opt) { sprintf(errStr, "Edit config with $EDITOR (%s)\nUsage: %s ProgramName [configBasename]", getenv("EDITOR"), names); break; case PERR_LIST_WRONG: - sprintf(errStr, "List programs, configs and more\nUsage: %s {programs|paths|-p ProgramName} [-t list-type] | {tree | ckconf} [-a]", names); + sprintf(errStr, "List programs, configs and more\nUsage: %s {programs|paths|-p ProgramName} [-t list-type] | {tree | ckconf} [-a] [-b]", names); break; case PERR_SEARCH_WRONG: sprintf(errStr, "Search through the configs with grep\nUsage: %s search-term", names); diff --git a/src/actions.c b/src/actions.c index 649ffb8..ddfe9b9 100644 --- a/src/actions.c +++ b/src/actions.c @@ -179,13 +179,13 @@ int run_LIST(UserOpt *opt, Conf *conf) { char tmp[STR_L] = ""; switch(listOpt._lt) { case LT_PATH: - list_get_paths(&db, the_list, listOpt.attr); + list_get_paths(&db, the_list, listOpt.bName, listOpt.attr); break; case LT_PROGRAM: list_get_programs(&db, the_list); break; case LT_TREE: - list_get_path_program_tree(&db, the_list, listOpt.attr); + list_get_path_program_tree(&db, the_list, listOpt.bName, listOpt.attr); list_print(the_list); goto close; case LT_CKCONF: @@ -207,7 +207,7 @@ int run_LIST(UserOpt *opt, Conf *conf) { ERR("Program %s doesn't exist in the database.", listOpt.pName); goto error; } - get_program_paths(&db, the_list, listOpt.pName, 0, listOpt.attr); + get_program_paths(&db, the_list, listOpt.pName, listOpt.bName, listOpt.attr); break; } switch(listOpt._lst) { @@ -237,7 +237,7 @@ int run_SEARCH(UserOpt *opt, Conf *conf) { } DB db = open_DB(opt); cklist *paths = list_make_new(); - list_get_paths(&db, paths, 0); + list_get_paths(&db, paths, 0 /*basename*/, 0/*attributes*/); close_DB(&db); if (list_size(paths) && list_size(opt->args)) { do { diff --git a/src/actions.h b/src/actions.h index 69c5616..e6fe336 100644 --- a/src/actions.h +++ b/src/actions.h @@ -77,6 +77,7 @@ struct ListOptions { ListShowType _lst; char *pName; int attr; + int bName; int err; }; diff --git a/src/dblayer.c b/src/dblayer.c index f517dbe..244db62 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -363,6 +363,16 @@ int add_get_or_insert_program_to_db(DB *db, const char *name) { return pid; } +int add_basename_exists(DB *db, const char *pName, const char *path) { + cklist *baseNames = list_make_new(); + get_program_paths(db, baseNames, pName, 1 /*basename */, 0); + char *tmp = strdup(path); + int rc = list_exists(baseNames, basename(tmp)); + free(tmp); + list_free(baseNames); + return rc; +} + int add_transaction_try(DB *db, const AddOpt * const opt) { __BEGIN_TRANSACTION__ int pid = add_get_or_insert_program_to_db(db, opt->progName); @@ -370,6 +380,10 @@ int add_transaction_try(DB *db, const AddOpt * const opt) { PRINT_ERR("Could not insert program to db.\n"); return 1; } + if (add_basename_exists(db, opt->progName, opt->confPath)) { + ERR("Cannot have two configs with the same basename, for the same program."); + return 1; + } 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"); @@ -540,7 +554,7 @@ int get_program_paths(DB *db, cklist *ckl, const char* pName, int bname, int att return -1; } -int list_get_paths(DB *db, cklist *ckl, int attr) { +int list_get_paths(DB *db, cklist *ckl, int bName, int attr) { sqlite3_stmt *stmt; int rc; @@ -553,8 +567,15 @@ int list_get_paths(DB *db, cklist *ckl, int attr) { } while (sqlite3_step(stmt) == SQLITE_ROW) { + char *tmp = strdup((char *)sqlite3_column_text(stmt, 0)); char path[STR_L] = ""; - strcat(path, (char *)sqlite3_column_text(stmt, 0)); + if (bName) { + strcat(path, basename(tmp)); + } + else { + strcat(path, tmp); + } + free(tmp); if (attr) { /* secret */ if (sqlite3_column_int(stmt, 1)) { @@ -596,7 +617,7 @@ int list_get_programs(DB *db, cklist *ckl) { return 1; } -int list_get_path_program_tree(DB *db, cklist *ckl, int attr) { +int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr) { sqlite3_stmt *stmt; int rc; @@ -638,7 +659,14 @@ int list_get_path_program_tree(DB *db, cklist *ckl, int attr) { sqlite3_bind_text(stmt2, 1, (char *)sqlite3_column_text(stmt, 0), -1, 0); while (sqlite3_step(stmt2) == SQLITE_ROW) { char treePath[STR_L] = "|- "; - strcat(treePath, (char *)sqlite3_column_text(stmt2, 0)); + char *tmp = strdup((char *)sqlite3_column_text(stmt2, 0)); + if (bName) { + strcat(treePath, basename(tmp)); + } + else { + strcat(treePath, tmp); + } + free(tmp); if (attr) { /* secret */ if (sqlite3_column_int(stmt2, 1)) { diff --git a/src/dblayer.h b/src/dblayer.h index 13ac187..20ab4b2 100644 --- a/src/dblayer.h +++ b/src/dblayer.h @@ -43,7 +43,7 @@ extern DB open_DB(const UserOpt *opt); extern void close_DB(DB *db); extern int program_exists(DB *db, const char *pName); -extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int basename, int attr); +extern int get_program_paths(DB *db, cklist *ckl, const char* pName, int bName, int attr); /********/ /* init */ @@ -72,9 +72,9 @@ extern int edit_get_config(DB *db, const char *pName, char *ret, const char *cNa /* list */ /********/ -extern int list_get_paths(DB *db, cklist *ckl, int attr); +extern int list_get_paths(DB *db, cklist *ckl, int bName, int attr); extern int list_get_programs(DB *db, cklist *ckl); -extern int list_get_path_program_tree(DB *db, cklist *ckl, int attr); +extern int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr); /*******/ /* del */ -- cgit v1.2.3