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