-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote
More file actions
103 lines (82 loc) · 1.88 KB
/
Copy pathnote
File metadata and controls
103 lines (82 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
#
# note-cli – einfaches Notiz-Tool
#
# Speichert Notizen unter .notes/db/<hash>, wobei <hash> der SHA1-Hash
# des Inhalts ist (analog zu Git-Blobs).
set -euo pipefail
NOTES_DIR=".notes/db"
usage() {
echo "Usage: note add <text>"
echo " note list"
echo " note delete <hash>"
exit 1
}
cmd_add() {
if [[ $# -eq 0 ]]; then
echo "Fehler: Kein Text angegeben." >&2
usage
fi
local content="$*"
mkdir -p "$NOTES_DIR"
# Hash über den Inhalt berechnen (SHA1, wie git hash-object)
local hash
hash=$(printf '%s' "$content" | sha1sum | awk '{print $1}')
local filepath="$NOTES_DIR/$hash"
if [[ -f "$filepath" ]]; then
echo "Notiz existiert bereits: $hash"
return 0
fi
printf '%s' "$content" > "$filepath"
echo "Notiz gespeichert: $hash"
}
cmd_list() {
if [[ ! -d "$NOTES_DIR" ]] || [[ -z "$(ls -A "$NOTES_DIR" 2>/dev/null)" ]]; then
echo "Keine Notizen vorhanden."
return 0
fi
local file
local hash
local content
for file in "$NOTES_DIR"/*; do
hash=$(basename "$file")
content=$(cat "$file")
echo "$hash $content"
done
}
cmd_delete() {
if [[ $# -eq 0 ]]; then
echo "Fehler: Kein Hash angegeben." >&2
usage
fi
local hash="$1"
local filepath="$NOTES_DIR/$hash"
if [[ ! -f "$filepath" ]]; then
echo "Fehler: Keine Notiz mit Hash '$hash' gefunden." >&2
exit 1
fi
rm "$filepath"
echo "Notiz gelöscht: $hash"
}
main() {
if [[ $# -eq 0 ]]; then
usage
fi
local command="$1"
shift
case "$command" in
add)
cmd_add "$@"
;;
list)
cmd_list "$@"
;;
delete)
cmd_delete "$@"
;;
*)
usage
;;
esac
}
main "$@"