-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell-command.sh
executable file
·101 lines (79 loc) · 2.02 KB
/
shell-command.sh
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
#!/usr/bin/env bash
##
# Shell script template.
#
# @usage:
# Interactive prompt:
# ./shell-command.sh
#
# ./shell-command.sh "topic"
#
# shellcheck disable=SC2162
set -euo pipefail
[ "${SCRIPT_DEBUG-}" = "1" ] && set -x
# URL endpoint to fetch the data from.
URL_ENDPOINT="${JOKE_URL_ENDPOINT:-https://official-joke-api.appspot.com/jokes/__TOPIC__/random}"
# Topic.
topic="${1-}"
# Flag to bypass the prompt to proceed.
SHOULD_PROCEED="${SHOULD_PROCEED-}"
#-------------------------------------------------------------------------------
ask() {
local prompt="$1"
local default="${2-}"
local result=""
if [[ -n ${default} ]]; then
prompt="${prompt} [${default}]: "
else
# LCOV_EXCL_START
prompt="${prompt}: "
# LCOV_EXCL_END
fi
while [[ -z ${result} ]]; do
read -p "${prompt}" result
# LCOV_EXCL_START
if [[ -z ${result} && -n ${default} ]]; then
result="${default}"
fi
# LCOV_EXCL_END
done
echo "${result}"
}
ask_yesno() {
local prompt="${1}"
local default="${2:-Y}"
local result
read -p "${prompt} [$([ "${default}" = "Y" ] && echo "Y/n" || echo "y/N")]: " result
result="$(echo "${result:-${default}}" | tr '[:upper:]' '[:lower:]')"
echo "${result}"
}
#-------------------------------------------------------------------------------
main() {
echo "Follow the prompts"
echo
[ -z "${topic}" ] && topic="$(ask "Joke topic" "general")"
[ -z "${SHOULD_PROCEED}" ] && SHOULD_PROCEED="$(ask_yesno "Proceed?")"
if [ "${SHOULD_PROCEED}" != "y" ]; then
echo
echo "Aborting."
exit 0
fi
echo
echo "Fetching joke for topic: ${topic}..."
echo
local url
url="${URL_ENDPOINT//__TOPIC__/${topic}}"
echo
echo "URL: ${url}"
echo
response="$(curl -sL "${url}")"
# Extract 'setup'
setup=$(echo "${response}" | sed -E 's/.*"setup":"([^"]+)".*/\1/')
# Extract 'punchline'
punchline=$(echo "${response}" | sed -E 's/.*"punchline":"([^"]+)".*/\1/')
echo "$setup"
echo "$punchline"
}
if [ "$0" = "${BASH_SOURCE[0]}" ]; then
main "$@"
fi