-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusion.sh
executable file
·101 lines (86 loc) · 2.3 KB
/
busion.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/local/bin/bash
# This script is used to copy content from a link into a file based on a comment section
# This can help to share the same function cross multiple files and generate a final file instead of having multiple files
SELF="${BASH_SOURCE[0]##*/}"
# shellcheck disable=SC2034
NAME="${SELF%.sh}"
OPTS="i:o:xh"
USAGE="Usage: $SELF [$OPTS]"
HELP="
$USAGE
Options:
-i Inputfile
-o Outputfile
-h Help
Example:
Inputfile contains a comment:
# Busion source url URL
The comment will be replaced by the content of the downloaded URL
"
_quit(){
local retCode="$1" msg="${*:2}"
printf '%s \n' "$msg"
exit "$retCode"
}
_clean(){ rm $tmpFile; }
while getopts "${OPTS}" arg; do
case "${arg}" in
i)
inputFile="$OPTARG"
;;
o)
outputFile="$OPTARG"
;;
h)
_quit 0 "$HELP"
;;
x)
set -x
;;
?)
_quit 1 "Invalid Argument: $USAGE"
;;
*)
_quit 1 "$USAGE"
;;
esac
done
shift $((OPTIND - 1))
[[ -z "$inputFile" || ! -f "$inputFile" ]] && _quit 2 "$HELP"
[[ -z "$outputFile" ]] && _quit 2 "$HELP"
declare -A config
tmpFile="$(mktemp)"
trap '_clean' EXIT
while IFS= read -r line; do
case "$line" in
*"Busion source"*)
read _ _ _ include content <<<"$line"
for key in "${!config[@]}"; do
content="${content//\$$key/${config[$key]}}"
done
case "$include" in
url)
curl -s "$curlOpts" "${content}" || _quit 2 "Could not download $content"
;;
file)
[[ -f "$content" ]] && \
printf '%s\n' "$(<$content)"
;;
esac
continue
;;
*"Busion var"*)
read _ _ _ var <<<"$line"
IFS="=" read key value <<<"$var"
config[$key]="$value"
continue
;;
*"Busion curlopt"*)
[[ "$line" =~ .*curlopt[[:space:]](.*) ]] && \
curlOpts="${BASH_REMATCH[1]}"
continue
;;
esac
printf '%s\n' "$line"
done < $inputFile > $tmpFile
cp "$tmpFile" "$outputFile"