fixed error when adding first note on new install. Fixed error when listing empty db.
[bash-notes.git] / notes.sh
... / ...
CommitLineData
1#! /bin/bash
2
3# set -ex
4
5PID=$$
6VERSION="0.1"
7
8set_defaults() {
9# Binaries to use
10EDITOR=${EDITOR:-/usr/bin/vim}
11TERMINAL=${TERMINAL:-/usr/bin/alacritty}
12TERM_OPTS="--class notes --title notes -e "
13JQ=${JQ:-/usr/bin/jq}
14
15# base directory for program files
16BASEDIR=${BASEDIR:-~/.local/share/bash-notes}
17# notes database in json format
18DB=${BASEDIR}/db.json
19# directory containing the actual notes
20NOTESDIR=${BASEDIR}/notes
21
22} # end set_defaults, do not change this line.
23
24set_defaults
25
26# Do not edit below this point
27RCFILE=${RCFILE:-~/.config/bash-notes.rc}
28TMPDB=/tmp/db.json
29BASENAME=$( basename $0 )
30NOW=$(date +%s)
31
32if [ ! -x $JQ ]; then
33 echo "jq not found in your PATH"
34 echo "install jq to continue"
35 exit 1
36fi
37
38# IMPORT USER DEFINED OPTIONS IF ANY
39if [[ -f $RCFILE ]]; then
40 source $RCFILE
41fi
42
43# We prevent the program from running more than one instance:
44PIDFILE=/var/tmp/$(basename $0 .sh).pid
45
46# Make sure the PID file is removed when we kill the process
47trap 'rm -f $PIDFILE; exit 1' TERM INT
48
49if [[ -r $PIDFILE ]]; then
50 # PIDFILE exists, so I guess there's already an instance running
51 # let's kill it and run again
52 kill -s 15 $(cat $PIDFILE) > /dev/null 2>&1
53 # should already be deleted by trap, but just to be sure
54 rm $PIDFILE
55fi
56
57# create PIDFILE
58echo $PID > $PIDFILE
59
60# check if input is a number, returns false or the number itself
61function check_noteID() {
62 IN=$1
63 case $IN in
64 ''|*[!0-9]*)
65 return 1
66 ;;
67 *)
68 echo $IN
69 ;;
70 esac
71}
72
73function helptext() {
74 echo "Usage:"
75 echo " $0 [PARAMS] ..."
76 echo ""
77 cat << __NOWCONF__
78${BASENAME} configuration is:
79
80base directory: ${BASEDIR}/
81notes archive: ${NOTESDIR}/
82notes database: ${DB}
83rc file: $RCFILE
84
85text editor: ${EDITOR}
86terminal: ${TERMINAL}
87jq executable: ${JQ}
88__NOWCONF__
89
90 echo ""
91 echo "${BASENAME} parameters are:"
92 echo " -h | --help : This help text"
93 echo " -p | --plain : Output is in plain text"
94 echo " (without this option the output is colored)"
95 echo " -l | --list : List existing notes"
96 echo " -a | --add <title> : Add new note"
97 echo " -m | --modify <note> : Modify note"
98 echo " -d | --date <note> : Modify date for note"
99 echo " -r | --remove <note> : Remove note"
100 echo " -v | --version : Print version"
101 echo " --userconf : Export User config file"
102 echo ""
103}
104
105function addnote() {
106 NOTETITLE="$1"
107 echo "adding new note - \"$NOTETITLE\""
108 LASTID=$($JQ '.notes[-1].id // 0 | tonumber' $DB)
109 # [ "" == $LASTID ] && LASTID=0
110 NOTEID=$(( $LASTID + 1 ))
111 touch ${NOTESDIR}/${NOW}
112 $JQ --arg i "$NOTEID" --arg t "$NOTETITLE" --arg f "$NOW" '.notes += [{"id": $i, "title": $t, "file": $f}]' "$DB" > $TMPDB
113 mv $TMPDB $DB
114 $(${TERMINAL} ${TERM_OPTS} ${EDITOR} ${NOTESDIR}/${NOW})
115}
116
117function listnotes() {
118 # [ $PLAIN == true ] && echo "output is plain text" || echo "output is colored"
119 if [[ $(ls -A $NOTESDIR) ]]; then
120 echo "listing all notes"
121 echo ""
122 echo "[ID] [TITLE] [SIZE]"
123 for i in ${NOTESDIR}/*; do
124 SIZE=$(du -k $i |cut -f 1)
125 TITLE=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .title' $DB)
126 ID=$($JQ -r --arg z $(basename $i) '.notes[] | select(.file == $z) | .id' $DB)
127
128 echo "[${ID}] ${TITLE} ${SIZE}kb"
129 done
130 else
131 echo "no notes yet. You can add your first one with: ${BASENAME} -a \"your note title\""
132 fi
133}
134
135function editnote() {
136 NOTE=$1
137 local OK=$(check_noteID $NOTE)
138 if [ ! $OK ]; then
139 echo "invalid note \"$NOTE\""
140 exit 1
141 fi
142
143 TITLE=$($JQ --arg i $OK '.notes[] | select(.id == $i) | .title' $DB)
144 FILE=$($JQ -r --arg i $OK '.notes[] | select(.id == $i) | .file' $DB)
145 if [ "$TITLE" ]; then
146 echo "editing note $TITLE"
147 $(${TERMINAL} --class notes --title notes -e ${EDITOR} ${NOTESDIR}/${FILE})
148 else
149 echo "note not found"
150 exit 1
151 fi
152}
153
154function datenote() {
155 NOTE=$1
156 local OK=$(check_noteID $NOTE)
157 [ $OK ] && echo "editing date for note $OK" || echo "invalid note \"$NOTE\""
158 # FILEDATE=$(date -d @$NOW +%d/%m/%Y_%T)
159
160}
161
162function rmnote() {
163 NOTE=$1
164 local OK=$(check_noteID $NOTE)
165 [ $OK ] && echo "removing note $OK" || echo "invalid note \"$NOTE\""
166}
167
168function export_config() {
169 if [ -r ${RCFILE} ]; then
170 echo "Backing up current '${RCFILE}'...."
171 mv -f ${RCFILE} ${RCFILE}.$(date +%Y%m%d_%H%M)
172 fi
173 echo "Writing '${RCFILE}'...."
174 sed -n '/^set_defaults() {/,/^} # end set_defaults, do not change this line./p' $0 \
175 | grep -v set_defaults \
176 | sed -e 's/^\([^=]*\)=\${\1:-\([^}]*\)}/\1=\2/' \
177 > ${RCFILE}
178 if [ -r ${RCFILE} ]; then
179 echo "Taking no further action."
180 exit 0
181 else
182 echo "Could not write '${RCFILE}'...!"
183 exit 1
184 fi
185}
186
187# we should expand on this function to add a sample note and explain a little bit
188# how the program works.
189function firstrun() {
190 [ -f $RCFILE ] && RC=$RCFILE || RC="none"
191
192 clear
193 echo "${BASENAME} configuration:
194
195base directory: ${BASEDIR}/
196notes archive: ${NOTESDIR}/
197notes database: ${DB}
198rc file: $RC
199text editor: ${EDITOR}
200terminal: ${TERMINAL}
201jq executable: ${JQ}
202"
203
204 read -r -p "Do you wish to continue? (y/N) " ANSWER
205 case $ANSWER in
206 y|Y )
207 mkdir -p $NOTESDIR
208 cat << __EOL__ > ${DB}
209{
210 "params": {
211 "version": "${VERSION}",
212 "dbversion": "${NOW}"
213 },
214 "notes": []
215}
216__EOL__
217 echo; echo "All done, you can now write your first note."
218 ;;
219 * )
220 echo "No changes made. Exiting"
221 exit
222 ;;
223 esac
224}
225
226# check for notes dir existance and create it in case it doesn't exists
227if [[ ! -d $NOTESDIR ]]; then
228 # we don't have a directory. FIRST RUN?
229 firstrun
230fi
231
232# NOTE: This requires GNU getopt. On Mac OS X and FreeBSD, you have to install this
233# separately; see below.
234GOPT=`getopt -o hvpla:m:d:r: --long help,version,list,plain,userconf,add:,modify:,date:,remove:,editor:,storage: \
235 -n 'bash-notes' -- "$@"`
236
237if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
238
239# Note the quotes around `$GOPT': they are essential!
240eval set -- "$GOPT"
241
242PLAIN=false
243
244while true; do
245 case "$1" in
246 -h | --help )
247 helptext
248 exit
249 ;;
250 -v | --version )
251 echo $BASENAME v${VERSION}
252 exit
253 ;;
254 -p | --plain )
255 PLAIN=true
256 shift
257 ;;
258 -l | --list )
259 listnotes
260 exit
261 ;;
262 -a | --add )
263 TITLE="$2"
264 shift 2
265 addnote "$TITLE"
266 ;;
267 -m | --modify )
268 NOTE="$2"
269 shift 2
270 editnote "$NOTE"
271 ;;
272 -d | --date )
273 NOTE="$2"
274 shift 2
275 datenote "$NOTE"
276 ;;
277 -r | --remove )
278 NOTE="$2"
279 shift 2
280 rmnote "$NOTE"
281 ;;
282 --userconf )
283 export_config
284 echo "config exported to \"$RCFILE\""
285 exit
286 ;;
287 -- )
288 shift; break
289 ;;
290 * )
291 break
292 ;;
293 esac
294done
295