notedux 1.0.0 → 1.0.1

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 +310 -53
  2. package/package.json +1 -1
package/notedux CHANGED
@@ -1,79 +1,336 @@
1
1
  #!/bin/bash
2
2
 
3
3
  CONFIG_FILE="$HOME/.notedux.conf"
4
+ API="https://notedux.com/api/note"
4
5
 
5
- # load config if exist
6
- if [ -f "$CONFIG_FILE" ]; then
7
- source "$CONFIG_FILE"
8
- fi
6
+ show_help() {
7
+ cat <<EOF
9
8
 
10
- TOKEN=""
9
+ Notedux CLI
10
+ Fast terminal note taking.
11
11
 
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
12
+ Usage:
13
+ notedux <command> [arguments]
21
14
 
15
+ Commands:
22
16
 
23
- shift $((OPTIND -1))
17
+ show Show current note
18
+ add <text> Add new line
19
+ set <text> Replace note content
20
+ generate Generate new token
21
+ deltoken Delete saved token
22
+ delall Delete all note content
23
+ dellast Delete last line
24
+ dell <number> Delete line by number
24
25
 
25
- TEXT="$*"
26
+ Options:
26
27
 
27
- # use default if token is missing
28
- if [ -z "$TOKEN" ]; then
29
- TOKEN="$DEFAULT_TOKEN"
30
- fi
28
+ -t <token> Use custom token
29
+ -h, --help Show this help
30
+
31
+ Examples:
32
+
33
+ notedux generate
34
+ notedux add "Do one thing and do it well."
35
+ notedux show
36
+ notedux dell 3
31
37
 
32
- # no token - create it !
33
- if [ -z "$TOKEN" ]; then
34
- echo "No token found. Creating new one..."
38
+ EOF
39
+ }
35
40
 
36
- RESPONSE=$(curl -s -X POST https://notedux.com/api/note/create)
37
41
 
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/"//')
42
+ load_config() {
43
+ if [ -f "$CONFIG_FILE" ]; then
44
+ source "$CONFIG_FILE"
42
45
  fi
46
+ }
47
+
48
+
49
+ save_token() {
50
+ echo "DEFAULT_TOKEN=\"$1\"" > "$CONFIG_FILE"
51
+ }
52
+
53
+
54
+ delete_token() {
55
+ rm -f "$CONFIG_FILE"
56
+ echo "Token deleted."
57
+ }
58
+
59
+
60
+ confirm() {
61
+ read -p "$1 [y/N]: " answer
62
+
63
+ case "$answer" in
64
+ y|Y)
65
+ return 0
66
+ ;;
67
+ *)
68
+ return 1
69
+ ;;
70
+ esac
71
+ }
72
+
73
+
74
+ generate_token() {
75
+
76
+ echo "Generating token..."
77
+
78
+ RESPONSE=$(curl -s -X POST "$API/create")
79
+
80
+ TOKEN=$(echo "$RESPONSE" \
81
+ | grep -o '"token"[ ]*:[ ]*"[^"]*"' \
82
+ | sed 's/.*:\s*"//;s/"//')
83
+
43
84
 
44
85
  if [ -z "$TOKEN" ]; then
45
- echo "Error: could not create token."
86
+ echo "Error: cannot generate token."
87
+ echo "$RESPONSE"
46
88
  exit 1
47
89
  fi
48
90
 
49
- echo "DEFAULT_TOKEN=\"$TOKEN\"" > "$CONFIG_FILE"
50
- echo "Token created and saved to config."
51
- fi
52
91
 
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")
92
+ save_token "$TOKEN"
93
+
94
+ echo "Token generated."
95
+ echo "$TOKEN"
96
+ }
97
+
98
+
99
+ get_token() {
100
+
101
+ if [ -z "$TOKEN" ]; then
102
+ TOKEN="$DEFAULT_TOKEN"
103
+ fi
104
+
105
+
106
+ if [ -z "$TOKEN" ]; then
107
+ generate_token
108
+ TOKEN="$DEFAULT_TOKEN"
109
+ fi
110
+ }
111
+
112
+
113
+ show_note() {
114
+
115
+ HEADERS=$(mktemp)
116
+
117
+ RESPONSE=$(curl -s -D "$HEADERS" \
118
+ -X POST \
119
+ "$API/get" \
120
+ --data-urlencode "token=$TOKEN" )
121
+ echo ""
122
+
123
+ echo "$RESPONSE"| nl -ba
124
+ echo ""
125
+ LINK=$(grep -i '^x-note-link:' "$HEADERS" | cut -d' ' -f2- | tr -d '\r')
126
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
127
+ echo "$LINK" | pbcopy
128
+ }
129
+
130
+
131
+ add_note() {
132
+
133
+ TEXT="$*"
134
+
135
+ if [ -z "$TEXT" ]; then
136
+ echo "Error: text missing."
137
+ exit 1
138
+ fi
139
+
140
+
141
+
142
+ HEADERS=$(mktemp)
143
+
144
+ NOTE=$(curl -s -D "$HEADERS" \
145
+ -X POST \
146
+ "$API/add" \
147
+ --data-urlencode "token=$TOKEN" \
148
+ --data-urlencode "text=$TEXT")
149
+
150
+ LINK=$(grep -i '^x-note-link:' "$HEADERS" | cut -d' ' -f2- | tr -d '\r')
151
+ echo 'Saved.'
152
+ echo ""
153
+ echo -e "Link: \033[1;34m$LINK\033[0m (Copied to clipboard.)"
154
+ echo "$LINK" | pbcopy
155
+ }
156
+
157
+
158
+ set_note() {
159
+
160
+ TEXT="$*"
161
+
162
+ if [ -z "$TEXT" ]; then
163
+ echo "Error: text missing."
164
+ exit 1
165
+ fi
166
+
167
+
168
+ RESPONSE=$(curl -s -X POST "$API/set" \
169
+ --data-urlencode "token=$TOKEN" \
170
+ --data-urlencode "text=$TEXT")
59
171
 
60
172
  echo "$RESPONSE"
61
- exit 0
62
- fi
173
+ # get and show the link to the note
174
+ LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
175
+ echo "$LINK" | pbcopy
176
+ echo -e "\033[1;34m$LINK\033[0m"
177
+ echo "Copied to clipboard."
178
+ }
179
+
180
+
181
+ delete_all() {
182
+
183
+ if ! confirm "Delete complete note"; then
184
+ echo "Cancelled."
185
+ exit
186
+ fi
63
187
 
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
188
 
71
- # pokušaj da izvučeš link
72
- LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
189
+ RESPONSE=$(curl -s -X POST "$API/delall" \
190
+ --data-urlencode "token=$TOKEN")
73
191
 
74
- if [ -n "$LINK" ]; then
75
- echo "Note created:"
76
- echo "$LINK"
77
- else
78
192
  echo "$RESPONSE"
79
- fi
193
+ # get and show the link to the note
194
+ LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
195
+ echo "$LINK" | pbcopy
196
+ echo -e "\033[1;34m$LINK\033[0m"
197
+ echo "Copied to clipboard."
198
+ }
199
+
200
+
201
+ delete_last() {
202
+
203
+ if ! confirm "Delete last line"; then
204
+ echo "Cancelled."
205
+ exit
206
+ fi
207
+
208
+
209
+ RESPONSE=$(curl -s -X POST "$API/dellast" \
210
+ --data-urlencode "token=$TOKEN")
211
+
212
+ echo "$RESPONSE"
213
+ # get and show the link to the note
214
+ LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
215
+ echo "$LINK" | pbcopy
216
+ echo -e "\033[1;34m$LINK\033[0m"
217
+ echo "Copied to clipboard."
218
+ }
219
+
220
+
221
+ delete_line() {
222
+
223
+ NUMBER="$1"
224
+
225
+
226
+ if [ -z "$NUMBER" ]; then
227
+ echo "Error: line number required."
228
+ echo "Usage: notedux dell <number>"
229
+ exit 1
230
+ fi
231
+
232
+
233
+ RESPONSE=$(curl -s -X POST "$API/dell" \
234
+ --data-urlencode "token=$TOKEN" \
235
+ --data-urlencode "line=$NUMBER")
236
+
237
+ echo "$RESPONSE"
238
+ # get and show the link to the note
239
+ LINK=$(echo "$RESPONSE" | grep -o '"link"[ ]*:[ ]*"[^"]*"' | sed 's/.*:\s*"//;s/"//')
240
+ echo "$LINK" | pbcopy
241
+ echo -e "\033[1;34m$LINK\033[0m"
242
+ echo "Copied to clipboard."
243
+ }
244
+
245
+
246
+
247
+ # -------------------------
248
+ # MAIN
249
+ # -------------------------
250
+
251
+
252
+ load_config
253
+
254
+
255
+ if [ $# -eq 0 ]; then
256
+ show_help
257
+ exit
258
+ fi
259
+
260
+
261
+ case "$1" in
262
+
263
+ -h|--help)
264
+ show_help
265
+ exit
266
+ ;;
267
+
268
+
269
+ -t)
270
+ TOKEN="$2"
271
+ shift 2
272
+ ;;
273
+
274
+ esac
275
+
276
+
277
+ COMMAND="$1"
278
+ shift
279
+
280
+
281
+ case "$COMMAND" in
282
+
283
+ generate)
284
+ generate_token
285
+ ;;
286
+
287
+
288
+ show)
289
+ get_token
290
+ show_note
291
+ ;;
292
+
293
+
294
+ add)
295
+ get_token
296
+ add_note "$@"
297
+ ;;
298
+
299
+
300
+ set)
301
+ get_token
302
+ set_note "$@"
303
+ ;;
304
+
305
+
306
+ deltoken)
307
+ delete_token
308
+ ;;
309
+
310
+
311
+ delall)
312
+ get_token
313
+ delete_all
314
+ ;;
315
+
316
+
317
+ dellast)
318
+ get_token
319
+ delete_last
320
+ ;;
321
+
322
+
323
+ dell)
324
+ get_token
325
+ delete_line "$1"
326
+ ;;
327
+
328
+
329
+ *)
330
+ echo "Unknown command: $COMMAND"
331
+ echo
332
+ show_help
333
+ exit 1
334
+ ;;
335
+
336
+ esac
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "notedux",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Take notes from terminal",
5
5
  "bin": {
6
6
  "notedux": "notedux"