-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baste
executable file
·475 lines (412 loc) · 11.7 KB
/
baste
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env bash
# This script was generated by bashly (https://github.com/DannyBen/bashly)
# Modifying it manually is not recommended
# :script.bash3_bouncer
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
printf "bash version 4 or higher is required\n"
exit 1
fi
# :command.root_command
root_command() {
# :src/root_command.sh
requires curl
requires git
requires jq
if [[ "${OSTYPE}" == "darwin"* ]]; then
requires pbpaste
else
requires xclip
fi
: "${GITHUB_TOKEN:=}"
if [[ "${args[--token]}" ]]; then # First, --token flag takes precedence.
token="${args[--token]}"
elif [[ "${GITHUB_TOKEN}" ]]; then # Next, we try an environmental variable.
token="${GITHUB_TOKEN}"
elif git config github.token &> /dev/null; then # finally, we fall back to any possible git config.
token=$(git config github.token)
else
err 'you must specifiy a valid token before posting; exiting ...'
fi
content="${args[--content]}"
if [[ "${args[--file]}" ]]; then
if ! test -f "${args[--file]}"; then
err "file ${args[--file]} does not exist; exiting ..."
fi
content=$(<"${args[--file]}")
elif [[ "${args[--clipboard]}" ]]; then
[[ "${OSTYPE}" == "darwin"* ]] && content=$(pbpaste) || content=$(xclip -o -selection clipboard)
fi
if read -t 0; then
read -r content
fi
[[ ! "${content}" ]] && err 'you must provide some kind of content to post; exiting ...'
[[ "${args[--public]}" ]] && public="true" || public="false"
payload=$(
jq -n \
--arg description "${args[--description]}" \
--arg name "${args[--name]}" \
--arg extension "${args[--extension]}" \
--arg content "${content}" \
--argjson public ${public} \
'{
description: $description,
public: $public,
files: {
"\($name).\($extension)": {
content: $content
}
}
}'
)
response=$(
curl -sw "%{http_code}" https://api.github.com/gists \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${token}" \
-d "${payload}" \
)
code="${response##*$'\n'}"
body="${response%$'\n'*}"
if [[ "${code}" != "201" ]]; then
message=$(echo "${body}" | jq -r '.message')
err "error from gist.github.com: ${message}; exiting ..."
fi
echo "${body}" | jq -r '.html_url'
}
# :command.version_command
version_command() {
echo "$version"
}
# :command.usage
baste_usage() {
if [[ -n $long_usage ]]; then
printf "baste - A paste bin tool written in Bash for quickly creating gists.\n"
echo
else
printf "baste - A paste bin tool written in Bash for quickly creating gists.\n"
echo
fi
printf "Usage:\n"
printf " baste [options]\n"
printf " baste --help | -h\n"
printf " baste --version | -v\n"
echo
if [[ -n $long_usage ]]; then
printf "Options:\n"
# :command.usage_fixed_flags
echo " --help, -h"
printf " Show this help\n"
echo
echo " --version, -v"
printf " Show version number\n"
echo
# :command.usage_flags
# :flag.usage
echo " --description, -d DESCRIPTION"
printf " A brief description of your gist.\n"
echo
# :flag.usage
echo " --name, -n NAME"
printf " The file name of your gist.\n"
printf " Default: gistfile1\n"
echo
# :flag.usage
echo " --content, -c CONTENT"
printf " The content of your gist.\n"
echo
# :flag.usage
echo " --public, -p"
printf " Determines whether your gist will be public or private.\n"
echo
# :flag.usage
echo " --id ID"
printf " Specify the UUID of a previous gist you have ownership of and baste will\n append content to it.\n"
echo
# :flag.usage
echo " --clipboard, --clip"
printf " Attempts to use the latest entry in your local clipboard as content for your\n gist. If using MacOS, an attempt to use pbpaste will be made. Otherwise,\n xclip will be used.\n"
echo
# :flag.usage
echo " --file, -f FILE"
printf " A path to the file to upload to gist.\n"
echo
# :flag.usage
echo " --extension, -e EXTENSION"
printf " The file extension of the gist.\n"
printf " Default: txt\n"
echo
# :flag.usage
echo " --token, -t TOKEN"
printf " Explicitly specific a personal developer token as an alternative to using\n the GITHUB_TOKEN environmental variable.\n"
echo
# :command.usage_environment_variables
printf "Environment Variables:\n"
# :environment_variable.usage
echo " GITHUB_TOKEN"
printf " A personal developer token associated with your Github account. You can\n easily create a new one in the \"Developer Settings\" section under your\n account settings page. The only permission scope you will need is \"gist\" in\n order to create new gists.\n"
echo
# :command.usage_examples
printf "Examples:\n"
printf " base --file /path/to/file.txt\n"
printf " baste --content '... code ...'\n"
printf " baste --name 'README.md' --content '# My Readme' --public\n"
printf " cat /path/to/file | baste\n"
printf " baste --clipboard\n"
echo
fi
}
# :command.normalize_input
normalize_input() {
local arg flags
while [[ $# -gt 0 ]]; do
arg="$1"
if [[ $arg =~ ^(--[a-zA-Z0-9_\-]+)=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^(-[a-zA-Z0-9])=(.+)$ ]]; then
input+=("${BASH_REMATCH[1]}")
input+=("${BASH_REMATCH[2]}")
elif [[ $arg =~ ^-([a-zA-Z0-9][a-zA-Z0-9]+)$ ]]; then
flags="${BASH_REMATCH[1]}"
for (( i=0 ; i < ${#flags} ; i++ )); do
input+=("-${flags:i:1}")
done
else
input+=("$arg")
fi
shift
done
}
# :command.inspect_args
inspect_args() {
readarray -t sorted_keys < <(printf '%s\n' "${!args[@]}" | sort)
if (( ${#args[@]} )); then
echo args:
for k in "${sorted_keys[@]}"; do echo "- \${args[$k]} = ${args[$k]}"; done
else
echo args: none
fi
if (( ${#other_args[@]} )); then
echo
echo other_args:
echo "- \${other_args[*]} = ${other_args[*]}"
for i in "${!other_args[@]}"; do
echo "- \${other_args[$i]} = ${other_args[$i]}"
done
fi
}
# :command.user_lib
# :src/lib/colors.sh
# ---
# Color functions
# This file is a part of Bashly standard library
#
# Usage:
# Use any of the functions below to color or format a portion of a string.
#
# echo "before $(red this is red) after"
# echo "before $(green_bold this is green_bold) after"
#
# ---
red() { printf "\e[31m%b\e[0m\n" "$*"; }
green() { printf "\e[32m%b\e[0m\n" "$*"; }
yellow() { printf "\e[33m%b\e[0m\n" "$*"; }
blue() { printf "\e[34m%b\e[0m\n" "$*"; }
magenta() { printf "\e[35m%b\e[0m\n" "$*"; }
cyan() { printf "\e[36m%b\e[0m\n" "$*"; }
bold() { printf "\e[1m%b\e[0m\n" "$*"; }
underlined() { printf "\e[4m%b\e[0m\n" "$*"; }
red_bold() { printf "\e[1;31m%b\e[0m\n" "$*"; }
green_bold() { printf "\e[1;32m%b\e[0m\n" "$*"; }
yellow_bold() { printf "\e[1;33m%b\e[0m\n" "$*"; }
blue_bold() { printf "\e[1;34m%b\e[0m\n" "$*"; }
magenta_bold() { printf "\e[1;35m%b\e[0m\n" "$*"; }
cyan_bold() { printf "\e[1;36m%b\e[0m\n" "$*"; }
red_underlined() { printf "\e[4;31m%b\e[0m\n" "$*"; }
green_underlined() { printf "\e[4;32m%b\e[0m\n" "$*"; }
yellow_underlined() { printf "\e[4;33m%b\e[0m\n" "$*"; }
blue_underlined() { printf "\e[4;34m%b\e[0m\n" "$*"; }
magenta_underlined() { printf "\e[4;35m%b\e[0m\n" "$*"; }
cyan_underlined() { printf "\e[4;36m%b\e[0m\n" "$*"; }
# :src/lib/err_function.sh
err() {
echo -e "$(red_bold [ERR]) ${1}" >&2
exit 1
}
# :src/lib/inf_function.sh
inf() {
echo -e "$(green [INF]) ${1}" >&2
}
# :src/lib/requires_function.sh
requires() {
command -v "${1}" &> /dev/null || err "baste requires '${1}'; exiting ..."
}
# :src/lib/validations/validate_valid_uuid.sh
validate_valid_uuid() {
[[ "${1}" =~ ^[a-zA-Z0-9]{32}$ ]] || echo -e "$(red_bold [ERR]) UUID must be a 32-character long alphanumeric string; exiting ..."
}
# :command.command_functions
# :command.parse_requirements
parse_requirements() {
# :command.fixed_flag_filter
case "$1" in
--version | -v )
version_command
exit
;;
--help | -h )
long_usage=yes
baste_usage
exit
;;
esac
# :command.environment_variables_filter
# :command.dependencies_filter
# :command.command_filter
action="root"
# :command.parse_requirements_while
while [[ $# -gt 0 ]]; do
key="$1"
case "$key" in
# :flag.case
--description | -d )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--description]="$2"
shift
shift
else
printf "%s\n" "--description requires an argument: --description, -d DESCRIPTION"
exit 1
fi
;;
# :flag.case
--name | -n )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--name]="$2"
shift
shift
else
printf "%s\n" "--name requires an argument: --name, -n NAME"
exit 1
fi
;;
# :flag.case
--content | -c )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--content]="$2"
shift
shift
else
printf "%s\n" "--content requires an argument: --content, -c CONTENT"
exit 1
fi
;;
# :flag.case
--public | -p )
args[--public]=1
shift
;;
# :flag.case
--id )
if [[ -n ${2+x} ]]; then
# :flag.validations
if [[ -n $(validate_valid_uuid "$2") ]]; then
printf "validation error in %s:\n%s\n" "--id ID" "$(validate_valid_uuid "$2")"
exit 1
fi
args[--id]="$2"
shift
shift
else
printf "%s\n" "--id requires an argument: --id ID"
exit 1
fi
;;
# :flag.case
--clipboard | --clip )
args[--clipboard]=1
shift
;;
# :flag.case
--file | -f )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--file]="$2"
shift
shift
else
printf "%s\n" "--file requires an argument: --file, -f FILE"
exit 1
fi
;;
# :flag.case
--extension | -e )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--extension]="$2"
shift
shift
else
printf "%s\n" "--extension requires an argument: --extension, -e EXTENSION"
exit 1
fi
;;
# :flag.case
--token | -t )
if [[ -n ${2+x} ]]; then
# :flag.validations
args[--token]="$2"
shift
shift
else
printf "%s\n" "--token requires an argument: --token, -t TOKEN"
exit 1
fi
;;
-* )
printf "invalid option: %s\n" "$key"
exit 1
;;
* )
# :command.parse_requirements_case
printf "invalid argument: %s\n" "$key"
exit 1
;;
esac
done
# :command.required_args_filter
# :command.required_flags_filter
# :command.catch_all_filter
# :command.default_assignments
[[ -n ${args[--name]} ]] || args[--name]="gistfile1"
[[ -n ${args[--extension]} ]] || args[--extension]="txt"
# :command.whitelist_filter
}
# :command.initialize
initialize() {
version="0.1.0"
long_usage=''
set -e
# :src/initialize.sh
# Code here runs inside the initialize() function
# Use it for anything that you need to run before any other function, like
# setting environment vairables:
# CONFIG_FILE=settings.ini
#
# Feel free to empty (but not delete) this file.
}
# :command.run
run() {
declare -A args
declare -a other_args
declare -a input
normalize_input "$@"
parse_requirements "${input[@]}"
if [[ $action == "root" ]]; then
root_command
fi
}
initialize
run "$@"