notedux 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/notedux +399 -53
  2. package/package.json +1 -1
package/notedux CHANGED
@@ -1,79 +1,425 @@
1
1
  #!/bin/bash
2
2
 
3
3
  CONFIG_FILE="$HOME/.notedux.conf"
4
+ API="https://notedux.com/api/note"
5
+ show_help() {
6
+ cat <<EOF
4
7
 
5
- # load config if exist
6
- if [ -f "$CONFIG_FILE" ]; then
7
- source "$CONFIG_FILE"
8
- fi
8
+ Notedux CLI
9
+ Fast terminal note taking.
9
10
 
10
- TOKEN=""
11
+ Usage:
12
+ notedux <command> [arguments]
11
13
 
12
- # parsign arguments
13
- while getopts "t:" opt; do
14
- case $opt in
15
- t)
16
- TOKEN_SET=1
17
- TOKEN="$OPTARG"
18
- ;;
19
- esac
20
- done
14
+ Commands:
21
15
 
16
+ show Show current note
17
+ add <text> Add new line
18
+ set <text> Replace note content
19
+ generate Generate new token
20
+ deltoken Delete saved token
21
+ delall Delete all note content
22
+ dellast Delete last line
23
+ dell <number> Delete line by number
22
24
 
23
- shift $((OPTIND -1))
25
+ Options:
24
26
 
25
- TEXT="$*"
27
+ -t <token> Use custom token
28
+ -h, --help Show this help
26
29
 
27
- # use default if token is missing
28
- if [ -z "$TOKEN" ]; then
29
- TOKEN="$DEFAULT_TOKEN"
30
- fi
30
+ Examples:
31
31
 
32
- # no token - create it !
33
- if [ -z "$TOKEN" ]; then
34
- echo "No token found. Creating new one..."
32
+ notedux generate
33
+ notedux add "Do one thing and do it well."
34
+ notedux show
35
+ notedux dell 3
35
36
 
36
- RESPONSE=$(curl -s -X POST https://notedux.com/api/note/create)
37
+ EOF
38
+ }
37
39
 
38
- if command -v jq >/dev/null 2>&1; then
39
- TOKEN=$(echo "$RESPONSE" | jq -r '.token')
40
- else
41
- TOKEN=$(echo "$RESPONSE" | grep -o '"token"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
40
+
41
+ load_config() {
42
+ if [ -f "$CONFIG_FILE" ]; then
43
+ source "$CONFIG_FILE"
44
+ fi
45
+ }
46
+
47
+
48
+ save_token() {
49
+ echo "DEFAULT_TOKEN=\"$1\"" > "$CONFIG_FILE"
50
+ }
51
+
52
+
53
+ delete_token() {
54
+ read -p "Are you sure you want to delete the token? [y/N] " answer
55
+
56
+ case "$answer" in
57
+ [Yy]|[Yy][Ee][Ss])
58
+ rm -f "$CONFIG_FILE"
59
+ echo "Token deleted."
60
+ ;;
61
+ *)
62
+ echo "Operation cancelled."
63
+ ;;
64
+ esac
65
+ }
66
+
67
+
68
+ confirm() {
69
+ read -p "$1 [y/N]: " answer
70
+
71
+ case "$answer" in
72
+ y|Y)
73
+ return 0
74
+ ;;
75
+ *)
76
+ return 1
77
+ ;;
78
+ esac
79
+ }
80
+
81
+
82
+ generate_token() {
83
+
84
+ if grep -q '^DEFAULT_TOKEN=' "$CONFIG_FILE" 2>/dev/null; then
85
+ echo "Default token is already set."
86
+ return 1
42
87
  fi
88
+ echo "Generating token..."
89
+
90
+ RESPONSE=$(curl -s -X POST "$API/create")
91
+
92
+ TOKEN=$(echo "$RESPONSE" \
93
+ | grep -o '"token"[ ]*:[ ]*"[^"]*"' \
94
+ | sed 's/.*:\s*"//;s/"//')
95
+
43
96
 
44
97
  if [ -z "$TOKEN" ]; then
45
- echo "Error: could not create token."
98
+ echo "Error: cannot generate token."
99
+ echo "$RESPONSE"
46
100
  exit 1
47
101
  fi
48
102
 
49
- echo "DEFAULT_TOKEN=\"$TOKEN\"" > "$CONFIG_FILE"
50
- echo "Token created and saved to config."
51
- fi
52
103
 
53
- # =========================
54
- # GET if there is no TEXT
55
- # =========================
56
- if [ -z "$TEXT" ]; then
57
- RESPONSE=$(curl -s -X POST https://notedux.com/api/note/get \
58
- --data-urlencode "token=$TOKEN")
104
+ save_token "$TOKEN"
105
+
106
+ echo "Token generated."
107
+ echo "$TOKEN"
108
+ }
109
+
110
+
111
+ get_token() {
112
+
113
+ if [ -z "$TOKEN" ]; then
114
+ TOKEN="$DEFAULT_TOKEN"
115
+ fi
116
+
117
+
118
+ if [ -z "$TOKEN" ]; then
119
+ generate_token
120
+ TOKEN="$DEFAULT_TOKEN"
121
+ fi
122
+ }
123
+
124
+
125
+ show_note() {
126
+
127
+ HEADERS=$(mktemp)
128
+
129
+ RESPONSE=$(curl -s -D "$HEADERS" \
130
+ -w "\n%{http_code}" \
131
+ -X POST \
132
+ "$API/get" \
133
+ --data-urlencode "token=$TOKEN")
134
+ echo ""
135
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
136
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
137
+
138
+ LINK=$(grep -i '^x-note-link:' "$HEADERS" | cut -d' ' -f2- | tr -d '\r')
139
+ if [ "$HTTP_CODE" == "200" ]; then
140
+ echo "$RESPONSE"| nl -ba
141
+ echo ""
142
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
143
+ echo "$LINK" | pbcopy
144
+ else
145
+ echo "$RESPONSE"
146
+ echo ""
147
+ fi
148
+
149
+ }
150
+
151
+
152
+ add_note() {
59
153
 
154
+ TEXT="$*"
155
+
156
+ if [ -z "$TEXT" ]; then
157
+ echo "Error: text missing."
158
+ exit 1
159
+ fi
160
+
161
+
162
+
163
+ HEADERS=$(mktemp)
164
+
165
+ RESPONSE=$(curl -s -D "$HEADERS" \
166
+ -w "\n%{http_code}" \
167
+ -X POST \
168
+ "$API/add" \
169
+ --data-urlencode "token=$TOKEN" \
170
+ --data-urlencode "text=$TEXT")
171
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
172
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
173
+ echo ""
60
174
  echo "$RESPONSE"
61
- exit 0
62
- fi
175
+ echo ""
176
+ LINK="https://notedux.com/n/$TOKEN"
177
+ if [ "$HTTP_CODE" = "200" ]; then
178
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
179
+ echo "$LINK" | pbcopy
180
+ fi
181
+ }
182
+
183
+
184
+ set_note() {
185
+
186
+ read -p "The content of the note will be overwritten. Are you sure ? [y/N] " answer
187
+
188
+ case "$answer" in
189
+ [Yy]|[Yy][Ee][Ss])
190
+ TEXT="$*"
191
+ if [ -z "$TEXT" ]; then
192
+ echo "Error: text missing."
193
+ exit 1
194
+ fi
195
+ HEADERS=$(mktemp)
196
+ RESPONSE=$(curl -s -D "$HEADERS" \
197
+ -w "\n%{http_code}" \
198
+ -X POST \
199
+ "$API/set" \
200
+ --data-urlencode "token=$TOKEN" \
201
+ --data-urlencode "text=$TEXT")
202
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
203
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
204
+ echo ""
205
+ echo "$RESPONSE"
206
+ echo ""
207
+ LINK="https://notedux.com/n/$TOKEN"
208
+ if [ "$HTTP_CODE" = "200" ]; then
209
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
210
+ echo "$LINK" | pbcopy
211
+ fi
212
+ ;;
213
+ *)
214
+ echo "Operation cancelled."
215
+ ;;
216
+ esac
217
+ }
63
218
 
64
- # =========================
65
- # ADD NOTE
66
- # =========================
67
- RESPONSE=$(curl -s -X POST https://notedux.com/api/note/add \
68
- --data-urlencode "token=$TOKEN" \
69
- --data-urlencode "text=$TEXT")
70
219
 
71
- # pokušaj da izvučeš link
72
- LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
73
220
 
74
- if [ -n "$LINK" ]; then
75
- echo "Note created:"
76
- echo "$LINK"
77
- else
221
+ delete_all() {
222
+
223
+ read -p "Delete complete note. Are you sure ? [y/N] " answer
224
+
225
+ case "$answer" in
226
+ [Yy]|[Yy][Ee][Ss])
227
+ HEADERS=$(mktemp)
228
+ RESPONSE=$(curl -s -D "$HEADERS" \
229
+ -w "\n%{http_code}" \
230
+ -X POST \
231
+ "$API/deleteall" \
232
+ --data-urlencode "token=$TOKEN")
233
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
234
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
235
+ echo ""
236
+ echo "$RESPONSE"
237
+ echo ""
238
+ LINK="https://notedux.com/n/$TOKEN"
239
+ if [ "$HTTP_CODE" = "200" ]; then
240
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
241
+ echo "$LINK" | pbcopy
242
+ fi
243
+ ;;
244
+ *)
245
+ echo "Operation cancelled."
246
+ ;;
247
+ esac
248
+ }
249
+
250
+
251
+ delete_last() {
252
+
253
+ if ! confirm "Delete last line"; then
254
+ echo "Cancelled."
255
+ exit
256
+ fi
257
+
258
+
259
+ RESPONSE=$(curl -s -X POST "$API/dellast" \
260
+ --data-urlencode "token=$TOKEN")
261
+
78
262
  echo "$RESPONSE"
79
- fi
263
+ # get and show the link to the note
264
+ LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
265
+ echo "$LINK" | pbcopy
266
+ echo -e "\033[1;34m$LINK\033[0m"
267
+ echo "Copied to clipboard."
268
+ }
269
+ delete_last() {
270
+
271
+ read -p "Delete last line. Are you sure ? [y/N] " answer
272
+
273
+ case "$answer" in
274
+ [Yy]|[Yy][Ee][Ss])
275
+ HEADERS=$(mktemp)
276
+ RESPONSE=$(curl -s -D "$HEADERS" \
277
+ -w "\n%{http_code}" \
278
+ -X POST \
279
+ "$API/deletelast" \
280
+ --data-urlencode "token=$TOKEN")
281
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
282
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
283
+ echo ""
284
+ echo "$RESPONSE"
285
+ echo ""
286
+ LINK="https://notedux.com/n/$TOKEN"
287
+ if [ "$HTTP_CODE" = "200" ]; then
288
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
289
+ echo "$LINK" | pbcopy
290
+ fi
291
+ ;;
292
+ *)
293
+ echo "Operation cancelled."
294
+ ;;
295
+ esac
296
+ }
297
+
298
+ delete_line() {
299
+
300
+ NUMBER="$1"
301
+ if [ -z "$NUMBER" ]; then
302
+ echo "Error: line number required."
303
+ echo "Usage: notedux dell <number>"
304
+ exit 1
305
+ fi
306
+ read -p "Delete line $NUMBER. Are you sure ? [y/N] " answer
307
+
308
+ case "$answer" in
309
+ [Yy]|[Yy][Ee][Ss])
310
+ HEADERS=$(mktemp)
311
+ RESPONSE=$(curl -s -D "$HEADERS" \
312
+ -w "\n%{http_code}" \
313
+ -X POST \
314
+ "$API/deleten" \
315
+ --data-urlencode "token=$TOKEN" \
316
+ --data-urlencode "line=$NUMBER")
317
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
318
+ RESPONSE=$(echo "$RESPONSE" | sed '$d')
319
+ echo ""
320
+ echo "$RESPONSE"
321
+ echo ""
322
+ LINK="https://notedux.com/n/$TOKEN"
323
+ if [ "$HTTP_CODE" = "200" ]; then
324
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
325
+ echo "$LINK" | pbcopy
326
+ fi
327
+ ;;
328
+ *)
329
+ echo "Operation cancelled."
330
+ ;;
331
+ esac
332
+ }
333
+
334
+
335
+
336
+ # -------------------------
337
+ # MAIN
338
+ # -------------------------
339
+
340
+
341
+ load_config
342
+
343
+
344
+ if [ $# -eq 0 ]; then
345
+ show_help
346
+ exit
347
+ fi
348
+
349
+
350
+ case "$1" in
351
+
352
+ -h|--help)
353
+ show_help
354
+ exit
355
+ ;;
356
+
357
+
358
+ -t)
359
+ TOKEN="$2"
360
+ shift 2
361
+ ;;
362
+
363
+ esac
364
+
365
+
366
+ COMMAND="$1"
367
+ shift
368
+
369
+
370
+ case "$COMMAND" in
371
+
372
+ generate)
373
+ generate_token
374
+ ;;
375
+
376
+
377
+ show)
378
+ get_token
379
+ show_note
380
+ ;;
381
+
382
+
383
+ add)
384
+ get_token
385
+ add_note "$@"
386
+ ;;
387
+
388
+
389
+ set)
390
+ get_token
391
+ set_note "$@"
392
+ ;;
393
+
394
+
395
+ deltoken)
396
+ delete_token
397
+ ;;
398
+
399
+
400
+ delall)
401
+ get_token
402
+ delete_all
403
+ ;;
404
+
405
+
406
+ dellast)
407
+ get_token
408
+ delete_last
409
+ ;;
410
+
411
+
412
+ dell)
413
+ get_token
414
+ delete_line "$1"
415
+ ;;
416
+
417
+
418
+ *)
419
+ echo "Unknown command: $COMMAND"
420
+ echo
421
+ show_help
422
+ exit 1
423
+ ;;
424
+
425
+ esac
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notedux",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Take notes from terminal",
5
5
  "bin": {
6
6
  "notedux": "notedux"