From 2c8fce96e7073589d3cfa15af211ddf2c1a71530 Mon Sep 17 00:00:00 2001 From: primis Date: Wed, 7 Dec 2016 11:11:31 -0500 Subject: [PATCH 1/3] Added Config file support --- README.md | 4 ++++ config.example | 6 ++++++ notes | 5 +++++ 3 files changed, 15 insertions(+) create mode 100644 config.example diff --git a/README.md b/README.md index 021aedc..7e1bb62 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,10 @@ curl https://cdn.rawgit.com/pimterry/notes/v0.1.2/notes > /usr/local/bin/notes & By default your notes live in ~/notes, but you can change that to anywhere you like by setting the `$NOTES_DIRECTORY` environmental variable. +## How do I configure this? + +You can set the configuration by creating a file at "~/.config/notes/config". We've included an example for you (config.example) that you can copy. + ### Bash completion If you want bash autocompletion copy the completion script into the bash completion directory. The bash completion directory is `/usr/share/bash-completion/completions/` on a typical debian jessie install, you can you can get the bash completion directory by running the following command: diff --git a/config.example b/config.example new file mode 100644 index 0000000..0146ec8 --- /dev/null +++ b/config.example @@ -0,0 +1,6 @@ +# This is an example configuration file for notes +# To use it, copy to ~/.config/notes/config +# This config is case sensitive + +# Set the editor that notes are opened with +EDITOR=nano diff --git a/notes b/notes index ac5c24f..d2a5de9 100755 --- a/notes +++ b/notes @@ -1,5 +1,10 @@ #!/usr/bin/env bash +# Look for configuration file at ~/.config/notes/config and use it +if [ -f ~/.config/notes/config ]; then + . ~/.config/notes/config +fi + configured_dir=${NOTES_DIRECTORY%/} # Remove trailing slashes notes_dir="${configured_dir:-$HOME/notes}" escaped_notes_dir="$(printf "$notes_dir" | sed -e 's/[]\/$*.^|[]/\\&/g')" From ed671a28560c1f7524f39b36ae0b5d85e4ef84a6 Mon Sep 17 00:00:00 2001 From: primis Date: Wed, 7 Dec 2016 11:14:37 -0500 Subject: [PATCH 2/3] Staging for merge --- notes | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/notes b/notes index d2a5de9..8419151 100755 --- a/notes +++ b/notes @@ -31,9 +31,9 @@ ls_notes() { } find_notes() { - find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1) - find_result=$? - formatted_output=$(printf "$find_output" | without_notes_dir) + local find_output=$(find "$notes_dir" -ipath "$notes_dir/*$**" -type f 2>&1) + local find_result=$? + local formatted_output=$(printf "$find_output" | without_notes_dir) if [[ $find_result == 0 && "$formatted_output" ]]; then printf "$formatted_output\n" @@ -49,9 +49,9 @@ grep_notes() { return 1 fi - grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1) - grep_result=$? - formatted_output=$(printf "$grep_output" | without_notes_dir) + local grep_output=$(grep -r "$notes_dir" -li -e "$*" 2>&1) + local grep_result=$? + local formatted_output=$(printf "$grep_output" | without_notes_dir) if [[ $grep_result == 0 && "$formatted_output" ]]; then printf "$formatted_output\n" @@ -62,7 +62,7 @@ grep_notes() { } new_note() { - note_name="$*" + local note_name="$*" mkdir -p "$(dirname "$notes_dir/$note_name")" open_note "$note_name.md" } @@ -97,7 +97,7 @@ open_something() { } open_note() { - note_path=$1 + local note_path=$1 if [[ "$note_path" != *.md ]]; then note_path="$note_path.md" @@ -136,8 +136,8 @@ EOF } main() { - ret=0 - cmd="" + local ret=0 + local cmd="" if [ -z "$1" ]; then printf "No command specified\n\n" From 27c4902805f7f16a9a61661641b8e7271570d381 Mon Sep 17 00:00:00 2001 From: primis Date: Wed, 7 Dec 2016 11:50:54 -0500 Subject: [PATCH 3/3] Added Configuration Test --- test/helpers.bash | 3 +++ test/test-config.bats | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/test-config.bats diff --git a/test/helpers.bash b/test/helpers.bash index e374ae2..829edf6 100644 --- a/test/helpers.bash +++ b/test/helpers.bash @@ -8,11 +8,14 @@ refute_exists() { setupNotesEnv() { export NOTES_DIRECTORY="$(mktemp -d)" + export NOTES_HOME="$(mktemp -d)" + export HOME=$NOTES_HOME } teardownNotesEnv() { if [ $BATS_TEST_COMPLETED ]; then rm -rf $NOTES_DIRECTORY + rm -rf $NOTES_HOME else echo "** Did not delete $NOTES_DIRECTORY, as test failed **" fi diff --git a/test/test-config.bats b/test/test-config.bats new file mode 100644 index 0000000..3bbe889 --- /dev/null +++ b/test/test-config.bats @@ -0,0 +1,24 @@ +#!./libs/bats/bin/bats + +load 'libs/bats-support/load' +load 'libs/bats-assert/load' +load 'helpers' + +setup() { + setupNotesEnv +} + +teardown() { + teardownNotesEnv +} + +notes="./notes" + +@test "Configuration should override EDITOR" { + mkdir -p $HOME/.config/notes + echo "EDITOR=echo" > $HOME/.config/notes/config + run $notes new test + + assert_success + assert_line "$NOTES_DIRECTORY/test.md" +}