-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstrapped
executable file
·171 lines (152 loc) · 4.74 KB
/
strapped
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
#!/bin/bash
# shellcheck source=/dev/null
VERSION="0.3.0"
C_GREEN="\\033[32m"
C_BLUE="\\033[94m"
C_REG="\\033[0;39m"
STRAPPED_DEBUG=""
custom_straps=""
auto_approve=""
base_repo="https://repo.strapped.sh"
yml_location="https://raw.githubusercontent.com/azohra/strapped/master/yml/first_run.yml"
url_regex='^(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]$'
# shellcheck disable=SC2034
YSH_LIB=1;
# shellcheck disable=SC1091
source /dev/stdin <<< "$(curl -s https://raw.githubusercontent.com/azohra/yaml.sh/v0.2.0/ysh)"
pretty_print () {
local msg_type=${1}
local message=${2}
case $msg_type in
":log:" )
echo -e "\\n🔫 ${C_BLUE} ${message} ${C_REG}"
;;
":announce:" )
echo -e "${C_GREEN}${message%::*}:${C_BLUE} ${message#*::} ${C_REG}"
;;
":info:" )
echo -e "${C_REG}${message}"
;;
esac
}
run_command() {
# write your test however you want; this just tests if SILENT is non-empty
if [ "$STRAPPED_DEBUG" ]; then
eval "${1}"
else
eval "${1}" > /dev/null
fi
}
parse_config() {
# Check for YML
if [[ "${yml_location}" =~ ${url_regex} ]]; then config=$(curl -s -L "${yml_location}" | ysh ); else config=$(ysh -f "${yml_location}"); fi
if [ ! "${config}" ]; then pretty_print ":announce:" "Strapped::Config not found" && exit 2;else pretty_print ":announce:" "Config::${yml_location}"; fi
}
parse_strapped_repo() {
# Check for Repo
if [ "$(ysh -T "${config}" -Q "strapped.repo")" != "null" ]; then base_repo="$(ysh -T "${config}" -Q "strapped.repo")"; fi
if [ ! "${base_repo}" ]; then pretty_print ":announce:" "Strapped::Repo not found" && exit 2;else pretty_print ":announce:" "Base Repo::${base_repo}"; fi
}
create_strap_array() {
# Create Strap Array
if [[ "${custom_straps}" ]]; then straps="${custom_straps//,/ }"; else straps=$(ysh -T "${config}" -t | uniq); fi
straps=${straps/strapped /}
if [ ! "${straps}" ]; then pretty_print ":announce:" "Strapped::Straps not found" && exit 2;else pretty_print ":announce:" "Straps::${straps//$'\n'/, }"; fi
}
ask_permission () {
local message=${1}
pretty_print ":log:" "${message}"
printf "(Y/N): "
while true
do
if [ "${auto_approve}" ]; then ans="y" && printf "Y (auto)"; else read -r ans; fi
case ${ans} in
[yY]* ) break;;
[nN]* ) exit;;
* ) printf "Y/N: ";;
esac
done
}
stay_strapped () {
local version
local strap_repo
for strap in ${straps}; do
strap_config=$(ysh -T "${config}" -s "${strap}")
# Get strapped repo
strap_repo=$(ysh -T "${strap_config}" -Q "repo")
strap_repo=${strap_repo:=${base_repo}}
# Get strapped version
version=$(ysh -T "${strap_config}" -Q "version")
version=${version:="latest"}
if [[ ${strap_repo} =~ ${url_regex} ]]; then
source /dev/stdin <<< "$(curl -s -L "${strap_repo}/${strap}/${version}/${strap}.sh")"
else
source "${strap_repo}/${strap}/${version}/${strap}.sh"
fi
pretty_print ":announce:" "\\n${strap}::${version} from ${strap_repo}"
strapped_"${strap}" "${strap_config}"
done
}
function usage {
echo -e "\\nUsage: strapped [flags]\\n"
echo "flags:"
echo " -u, --upgrade upgrade strapped to the latest version"
echo " -v, --version print the current strapped version"
echo " -a, --auto do not prompt for confirmation"
echo " -y, --yml file/url path to a valid strapped yml config"
echo " -l, --lint exits after reading config file"
echo " -s, --straps string run a subset of your config. Comma seperated."
echo " -h, --help prints this message"
exit 1
}
function upgrade {
rm /usr/local/bin/strapped
curl -s https://stay.strapped.sh | sh
pretty_print ":announce:" "Strapped::Upgraded Successfully!"
exit 0
}
while [ $# -gt 0 ] ; do
case "$1" in
-d|--debug)
STRAPPED_DEBUG="true"
;;
-u|--upgrade)
upgrade
;;
-y|--yml)
yml_location="$2"
shift # extra value
;;
-l|--lint)
# init_parser
ysh "${2}" > /dev/null
exit $?
;;
-r|--repo)
base_repo="$2"
shift # extra value
;;
-s|--straps)
custom_straps="$2"
shift # extra value
;;
-a|--auto)
auto_approve="true"
;;
-v|--version)
echo "v$VERSION" && exit 0
;;
-h|--help)
usage
;;
-*)
"Unknown option: '$1'"
;;
esac
shift
done
parse_config
parse_strapped_repo
create_strap_array
ask_permission "Are you ready to get strapped?"
stay_strapped