Skip to content

Commit

Permalink
Merge pull request pimterry#28 from primis/master
Browse files Browse the repository at this point in the history
Add Configuration File Support
  • Loading branch information
pimterry authored Dec 7, 2016
2 parents 3e40b1c + 27c4902 commit fb1b621
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions config.example
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions notes
Original file line number Diff line number Diff line change
@@ -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')"
Expand Down
3 changes: 3 additions & 0 deletions test/helpers.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/test-config.bats
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit fb1b621

Please sign in to comment.