From 93d8e16d5039a44bbc847b457ab2a09c53625877 Mon Sep 17 00:00:00 2001 From: "Manuel J. Garrido" Date: Mon, 24 Jun 2013 09:47:48 +0200 Subject: [PATCH 1/2] Install using 'install' instead of 'cp'. Install 'todo.sh' instead of 'todo' --- Makefile | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 6e98a05..3ef42e0 100644 --- a/Makefile +++ b/Makefile @@ -31,9 +31,8 @@ clean: rm -f $(DISTNAME).tar.gz $(DISTNAME).zip install: - cp todo.sh $(INSTALL_DIR)/todo - chmod a+x $(INSTALL_DIR)/todo - cp todo_completion /etc/bash_completion.d/todo + install --mode=755 todo.sh $(INSTALL_DIR) + install --mode=644 todo_completion /etc/bash_completion.d/todo mkdir -p ~/.todo cp -n todo.cfg ~/.todo/config From ce365df67ce84cd23abfd214238168b5041036c8 Mon Sep 17 00:00:00 2001 From: "Manuel J. Garrido" Date: Mon, 24 Jun 2013 10:32:42 +0200 Subject: [PATCH 2/2] Allow the use of global config file If not found elsewhere, look for the config file in /etc/todo/config. `make install` installs a global config file, setting TODO_DIR=~/.todo. todo.sh creates TODO_DIR if it doesn't exist. --- Makefile | 6 +++--- tests/t0000-config.sh | 12 ++++++++++++ tests/t2120-shorthelp.sh | 3 +++ todo.sh | 12 +++++++++++- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 3ef42e0..512a54c 100644 --- a/Makefile +++ b/Makefile @@ -33,9 +33,9 @@ clean: install: install --mode=755 todo.sh $(INSTALL_DIR) install --mode=644 todo_completion /etc/bash_completion.d/todo - mkdir -p ~/.todo - cp -n todo.cfg ~/.todo/config - + mkdir -p /etc/todo + [ -e /etc/todo/config ] || \ + sed "s/^\(export[ \t]*TODO_DIR=\).*/\1~\/.todo/" todo.cfg > /etc/todo/config # # Testing # diff --git a/tests/t0000-config.sh b/tests/t0000-config.sh index 88cb345..ac1af9f 100755 --- a/tests/t0000-config.sh +++ b/tests/t0000-config.sh @@ -8,6 +8,10 @@ to find it somewhere else. ' . ./test-lib.sh + +# Override default global config file +export TODOTXT_GLOBAL_CFG_FILE=global.cfg + # Remove the pre-created todo.cfg to test behavior in its absence rm -f todo.cfg echo "Fatal Error: Cannot read configuration file $HOME/.todo/config" > expect @@ -55,6 +59,14 @@ test_expect_success 'config file (default location 3)' ' rm -f .todo.cfg ' +rm -f used_config +test_expect_success 'config file (global config file)' ' + cp test.cfg "$TODOTXT_GLOBAL_CFG_FILE" + todo.sh > output; + test_cmp expect output && test -f used_config && + rm -f "$TODOTXT_GLOBAL_CFG_FILE" +' + rm -f used_config test_expect_success 'config file (command line)' ' todo.sh -d test.cfg > output; diff --git a/tests/t2120-shorthelp.sh b/tests/t2120-shorthelp.sh index 1724dee..a31f930 100755 --- a/tests/t2120-shorthelp.sh +++ b/tests/t2120-shorthelp.sh @@ -45,6 +45,9 @@ mv todo.cfg custom.cfg mv .todo.actions.d custom.actions echo 'export TODO_ACTIONS_DIR=$HOME/custom.actions' >> custom.cfg +# Avoid the use of global config file, if it exists +export TODOTXT_GLOBAL_CFG_FILE=global.cfg + test_todo_session '-h and fatal error without config' <>> todo.sh -h | sed '/^ \\{0,2\\}[A-Z]/!d' Usage: todo.sh [-fhpantvV] [-d todo_config] action [task_number] [task_description] diff --git a/todo.sh b/todo.sh index 25078fb..ff90cfd 100755 --- a/todo.sh +++ b/todo.sh @@ -584,6 +584,7 @@ TODOTXT_DEFAULT_ACTION=${TODOTXT_DEFAULT_ACTION:-} TODOTXT_SORT_COMMAND=${TODOTXT_SORT_COMMAND:-env LC_COLLATE=C sort -f -k2} TODOTXT_DISABLE_FILTER=${TODOTXT_DISABLE_FILTER:-} TODOTXT_FINAL_FILTER=${TODOTXT_FINAL_FILTER:-cat} +TODOTXT_GLOBAL_CFG_FILE=${TODOTXT_GLOBAL_CFG_FILE:-/etc/todo/config} # Export all TODOTXT_* variables export ${!TODOTXT_@} @@ -650,6 +651,15 @@ export SENTENCE_DELIMITERS=',.:;' fi } +[ -e "$TODOTXT_CFG_FILE" ] || { + CFG_FILE_ALT="$TODOTXT_GLOBAL_CFG_FILE" + + if [ -e "$CFG_FILE_ALT" ] + then + TODOTXT_CFG_FILE="$CFG_FILE_ALT" + fi +} + if [ -z "$TODO_ACTIONS_DIR" -o ! -d "$TODO_ACTIONS_DIR" ] then @@ -706,7 +716,7 @@ fi ACTION=${1:-$TODOTXT_DEFAULT_ACTION} [ -z "$ACTION" ] && usage -[ -d "$TODO_DIR" ] || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory" +[ -d "$TODO_DIR" ] || mkdir -p $TODO_DIR 2> /dev/null || dieWithHelp "$1" "Fatal Error: $TODO_DIR is not a directory" ( cd "$TODO_DIR" ) || dieWithHelp "$1" "Fatal Error: Unable to cd to $TODO_DIR" [ -f "$TODO_FILE" ] || cp /dev/null "$TODO_FILE"