Skip to content

Commit

Permalink
Add grep command
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Nov 30, 2016
1 parent 46f8626 commit 59e3fef
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
31 changes: 29 additions & 2 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ find_notes() {
if [[ $find_result == 0 && $formatted_output ]]; then
echo "$formatted_output"
return 0
else
else
return 2
fi
}

grep_notes() {
if [ ! "$#" -gt 0 ]; then
echo "Grep requires a pattern, but none was provided."
return 1
fi

grep_output=$(grep -r "$notes_dir" -e "$*" 2>&1)
grep_result=$?
formatted_output=$(echo "$grep_output" | without_notes_dir)

if [[ $grep_result == 0 && $formatted_output ]]; then
echo "$formatted_output"
return 0
else
return 2
fi
}
Expand All @@ -32,6 +50,7 @@ notes is a command line note taking tool.
Usage:
notes (new|n) <name> # Create a new note
notes find [pattern] # Search notes by filename and path
notes grep <pattern> # Search notes by content
notes open # Open your notes directory
notes --help # Print this usage information
Expand All @@ -43,7 +62,12 @@ EOF
main() {
ret=0
cmd=""
[ -z "$1" ] && echo "No command specified\n" && usage && exit 1

if [ -z "$1" ]; then
echo "No command specified\n"
usage
exit 1
fi

case "$1" in
"n" | "new" )
Expand All @@ -52,6 +76,9 @@ main() {
"find" )
cmd="find_notes"
;;
"grep" )
cmd="grep_notes"
;;
"open" )
cmd="open $notes_dir"
;;
Expand Down
10 changes: 5 additions & 5 deletions test/test-find.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ export NOTES_DIRECTORY="$TMP_DIRECTORY"

notes="./notes"

@test "Should output nothing and return non-zero if there are no notes" {
@test "Should output nothing and return non-zero if there are no notes to find" {
run $notes find

assert_failure
echo $output
assert_equal $(echo $output | wc -w) 0
}

@test "Should show all notes found if no pattern is provided" {
@test "Should show all notes found if no pattern is provided to find" {
touch $NOTES_DIRECTORY/note1.md
touch $NOTES_DIRECTORY/note2.md

Expand All @@ -27,7 +27,7 @@ notes="./notes"
assert_line "note2.md"
}

@test "Should show matching notes only if a pattern is provided" {
@test "Should show matching notes only if a pattern is provided to find" {
touch $NOTES_DIRECTORY/match-note1.md
touch $NOTES_DIRECTORY/hide-note2.md

Expand All @@ -38,7 +38,7 @@ notes="./notes"
refute_line "hide-note2.md"
}

@test "Should match notes case insensitively" {
@test "Should match notes case insensitively with find" {
touch $NOTES_DIRECTORY/MATCH-note1.md
touch $NOTES_DIRECTORY/hide-note2.md

Expand All @@ -49,7 +49,7 @@ notes="./notes"
refute_line "hide-note2.md"
}

@test "Should match subdirectory or file names" {
@test "Should match subdirectory or file names with find" {
touch "$NOTES_DIRECTORY/hide-note.md"
mkdir "$NOTES_DIRECTORY/match-directory"
touch "$NOTES_DIRECTORY/match-directory/note.md"
Expand Down
28 changes: 28 additions & 0 deletions test/test-grep.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!./libs/bats/bin/bats

load 'libs/bats-support/load'
load 'libs/bats-assert/load'

# Set up a directory for our notes
TMP_DIRECTORY=$(mktemp -d)
export NOTES_DIRECTORY="$TMP_DIRECTORY"

notes="./notes"

@test "Should complain and ask for a pattern if not is provided to grep" {
run $notes grep

assert_failure
assert_line "Grep requires a pattern, but none was provided."
}

@test "Should match only the files containing the given pattern when grepping" {
echo "my-pattern" > $NOTES_DIRECTORY/matching-node.md
echo "some-other-pattern" > $NOTES_DIRECTORY/non-matching-node.md

run $notes grep my-pattern

assert_success
assert_line "matching-node.md:my-pattern"
refute_line "non-matching-node.md"
}

0 comments on commit 59e3fef

Please sign in to comment.