Skip to content

Commit 32395e9

Browse files
authored
Merge pull request #11 from ecksun/rm
Add support for removing notes
2 parents 9639b3a + 4af1e63 commit 32395e9

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Opens your notes folder in your default configured file explorer. Shorthand alia
5151

5252
Opens a given note in your `$EDITOR`. Name can be an absolute path, or a relative path in your notes (.md suffix optional). Shorthand alias also available with `notes o`.
5353

54+
### `notes rm [-r | --recursive] <note-name>`
55+
56+
Remove the given note if it exists. If `-r` or `--recursive` is given, delete the folders/notes recursively.
57+
5458
### `notes grep/find <pattern> | notes open`
5559

5660
Combine these together! This opens each matching note in your `$EDITOR` in turn.

notes

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ new_note() {
5050
open_note "$note_name.md"
5151
}
5252

53+
remove_note() {
54+
local rm_args=()
55+
if [[ "$1" == "-r" || "$1" == "--recursive" ]]; then
56+
rm_args+=("--recursive")
57+
shift
58+
fi
59+
60+
local note_name="$*"
61+
local to_remove="$notes_dir/$note_name"
62+
63+
if [ -f "$notes_dir/$note_name.md" ]; then
64+
to_remove="$notes_dir/$note_name.md"
65+
fi
66+
rm "${rm_args[@]}" "$to_remove"
67+
}
68+
5369
open_something() {
5470
if [[ -p /dev/stdin ]]; then
5571
read -d'\n' note_names
@@ -90,6 +106,7 @@ Usage:
90106
notes grep|g <pattern> # Search notes by content
91107
notes open|o # Open your notes directory
92108
notes open|o <name> # Open a note for editing by full name
109+
notes rm [-r | --recursive] <name> # Remove note, or folder if -r or --recursive is given
93110
echo <name> | notes open|o # Open all note filenames piped in
94111
notes --help # Print this usage information
95112
@@ -123,6 +140,9 @@ main() {
123140
"open"|"o" )
124141
cmd="open_something"
125142
;;
143+
"rm" )
144+
cmd="remove_note"
145+
;;
126146
--help | -help | -h )
127147
cmd="usage"
128148
;;

test/helpers.bash

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
assert_exists() {
2-
assert [ -f "$1" ]
2+
assert [ -e "$1" ]
3+
}
4+
5+
refute_exists() {
6+
assert [ ! -e "$1" ]
37
}
48

59
setupNotesEnv() {
@@ -12,4 +16,4 @@ teardownNotesEnv() {
1216
else
1317
echo "** Did not delete $NOTES_DIRECTORY, as test failed **"
1418
fi
15-
}
19+
}

test/test-rm.bats

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!./libs/bats/bin/bats
2+
3+
load 'libs/bats-support/load'
4+
load 'libs/bats-assert/load'
5+
load 'helpers'
6+
7+
setup() {
8+
setupNotesEnv
9+
}
10+
11+
teardown() {
12+
teardownNotesEnv
13+
}
14+
15+
notes="./notes"
16+
17+
@test "Should remove created note" {
18+
touch "$NOTES_DIRECTORY/note.md"
19+
run $notes rm note
20+
21+
assert_success
22+
refute_exists "$NOTES_DIRECTORY/note.md"
23+
}
24+
25+
@test "Should fail to delete non-existent note" {
26+
run $notes rm note
27+
28+
assert_failure
29+
}
30+
31+
@test "Should remove note in folder" {
32+
mkdir "$NOTES_DIRECTORY/folder"
33+
touch "$NOTES_DIRECTORY/folder/note.md"
34+
run $notes rm folder/note
35+
36+
assert_success
37+
refute_exists "$NOTES_DIRECTORY/folder/note.md"
38+
}
39+
40+
@test "Should fail to remove folder" {
41+
mkdir "$NOTES_DIRECTORY/folder"
42+
run $notes rm folder
43+
44+
assert_failure
45+
assert_exists "$NOTES_DIRECTORY/folder"
46+
}
47+
48+
@test "-r Should remove folder recursively" {
49+
mkdir "$NOTES_DIRECTORY/folder"
50+
touch "$NOTES_DIRECTORY/folder/note.md"
51+
run $notes rm -r folder
52+
53+
assert_success
54+
refute_exists "$NOTES_DIRECTORY/folder"
55+
refute_exists "$NOTES_DIRECTORY/folder/notes.md"
56+
}
57+
58+
@test "--recursive Should remove folder recursively" {
59+
mkdir "$NOTES_DIRECTORY/folder"
60+
touch "$NOTES_DIRECTORY/folder/note.md"
61+
run $notes rm --recursive folder
62+
63+
assert_success
64+
refute_exists "$NOTES_DIRECTORY/folder"
65+
refute_exists "$NOTES_DIRECTORY/folder/notes.md"
66+
}
67+
68+
@test "should delete file if both folder and file exists" {
69+
mkdir "$NOTES_DIRECTORY/folder"
70+
touch "$NOTES_DIRECTORY/folder.md"
71+
run $notes rm --recursive folder
72+
73+
assert_success
74+
assert_exists "$NOTES_DIRECTORY/folder"
75+
refute_exists "$NOTES_DIRECTORY/folder.md"
76+
}

0 commit comments

Comments
 (0)