-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.sh
executable file
·289 lines (258 loc) · 6.71 KB
/
make.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
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
#!/usr/bin/env bash
set -eu # exit on error or undefined variable
bash -c 'set -o pipefail' # return code of first cmd to fail in a pipeline
EXPECTED_ARGS=1
if [[ $# -lt ${EXPECTED_ARGS} ]]; then
echo "Usage:"
echo " $(basename "$0") FUNC [ARGS...]"
echo ""
echo "Execute the specified func"
echo ""
echo "Examples:"
echo " $(basename "$0") depends go"
exit 1
fi
FUNC=${1}
# depends checks for programs this script depends on
depends() {
if [[ ${1} == "go" ]]; then
go version >/dev/null 2>&1 ||
{
echo "Install https://golang.org"
exit 1
}
elif [[ ${1} == "configu" ]]; then
configu --help >/dev/null 2>&1 ||
{
echo "Install https://github.com/mozey/config"
exit 1
}
elif [[ ${1} == "watcher" ]]; then
watcher -version >/dev/null 2>&1 ||
{
echo "Install https://github.com/mozey/watcher"
exit 1
}
elif [[ ${1} == "gojq" ]]; then
gojq --version >/dev/null 2>&1 ||
{
echo "Install https://github.com/itchyny/gojq"
exit 1
}
elif [[ ${1} == "gotest" ]]; then
# TODO How to make gotest print version?
TYPE=$(type -t gotest || echo "undefined")
if [[ ${TYPE} != "file" ]]; then
{
echo "Install https://github.com/rakyll/gotest"
exit 1
}
fi
elif [[ ${1} == "APP_EXE_PATH" ]]; then
export APP_EXE_PATH=${APP_EXE_PATH:-undefined}
if [[ ${APP_EXE_PATH} == "undefined" ]]; then
# Error if APP_EXE is not set
export APP_EXE=${APP_EXE}
# Use full path to avoid conflicts
# shellcheck disable=2155
export APP_EXE_PATH="$(pwd)/${APP_EXE}"
fi
else
echo "unknown dependency ${1}"
exit 1
fi
}
# detect_os is useful when writing cross platform functions.
# Return value must corrrespond to GOOS listed here
# https://go.dev/doc/install/source#environment
# shellcheck disable=2120
detect_os() {
# Check if func arg is set
# https://stackoverflow.com/a/13864829/639133
if [ -z ${1+x} ]; then
OUTPUT=$(uname -s)
else
# This is useful when parsing output from a remote host
OUTPUT="$1"
fi
case "$OUTPUT" in
Darwin)
echo 'darwin'
;;
Linux)
echo 'linux'
;;
CYGWIN* | MINGW32* | MSYS* | MINGW*)
echo 'windows'
;;
# Detect additional OS's here...
# See correspondence table at the bottom of this link
# https://stackoverflow.com/a/27776822/639133
*)
echo 'other'
;;
esac
}
# detect_arch is useful when writing cross platform functions
# Return value must corrrespond to GOARCH listed here
# https://go.dev/doc/install/source#environment
# shellcheck disable=2120
detect_arch() {
# Check if func arg is set
# https://stackoverflow.com/a/13864829/639133
if [ -z ${1+x} ]; then
OUTPUT=$(uname -m)
else
# This is useful when parsing output from a remote host
OUTPUT="$1"
fi
case "$OUTPUT" in
amd64)
echo 'amd64'
;;
x86_64)
echo 'amd64'
;;
arm64)
echo 'arm64'
;;
*)
echo 'other'
;;
esac
}
# Kill process by matching full path to bin
kill_path() {
OS=$(detect_os)
if [[ ${OS} == "darwin" ]] || [[ ${OS} == "linux" ]]; then
PID=$(pgrep -fx "${1}" || echo "")
if [[ -n "${PID}" ]]; then
kill "${PID}"
fi
else
echo "OS ${OS} not implemented"
exit 1
fi
}
app_build_dev() {
scripts/build-dev.sh
}
app_kill() {
depends APP_EXE_PATH
kill_path "${APP_EXE_PATH}"
}
# Run the binary, no live reload.
# Use full path to avoid conflicts
app_run() {
depends go
depends APP_EXE_PATH
app_kill
app_build_dev
# shellcheck disable=2181
(if [[ "${?}" -eq 0 ]]; then (${APP_EXE_PATH}); fi)
}
# Restart the binary.
# Use full path to avoid conflicts
app_restart() {
depends go
depends APP_EXE_PATH
app_kill
app_build_dev
# shellcheck disable=2181
(if [[ "${?}" -eq 0 ]]; then (${APP_EXE_PATH} &) fi)
}
# Run app bin with live reload
# Watch .go files for changes then recompile & try to start bin
# will also kill bin on ctrl+c
app() {
depends watcher
app_restart
APP_DIR=${APP_DIR}
watcher -d 1500 -r -dir "" \
--include ".*.go$" \
--excludeDir "${APP_DIR}/cmd.*" \
--excludeDir "${APP_DIR}/dist.*" \
--excludeDir "${APP_DIR}/scripts.*" \
--excludeDir "${APP_DIR}/vendor.*" \
--excludeDir "${APP_DIR}/www.*" |
xargs -n1 bash -c "./make.sh app_restart" || bash -c "./make.sh app_kill"
}
# env_sh generates ENV.sh from config.ENV.json
env_sh() {
depends gojq
ENV=${1}
PROMPT=${2:-undefined}
if [[ ! -d "$APP_DIR" ]]; then
echo "Dir $APP_DIR does not exist"
exit 1
fi
if [[ ${PROMPT} != "PROMPT_DISABLED" ]]; then
read -r -p "Generate new ${ENV}.sh? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
:
else
echo "abort"
exit 1
fi
fi
SAMPLE_CONFIG="$APP_DIR/sample.config.${ENV}.json"
CONFIG="$APP_DIR/config.${ENV}.json"
ENV_SH="$APP_DIR/${ENV}.sh"
if [[ -f "${CONFIG}" ]]; then
echo "Using existing ${CONFIG}"
else
echo "Creating new ${CONFIG}"
cp "${SAMPLE_CONFIG}" "${CONFIG}"
fi
echo "#!/usr/bin/env bash" >"${ENV_SH}"
{
echo "# ------------------------ DO NOT EDIT!"
echo "# Generated from $CONFIG"
echo "# Edit the JSON file to change your local config,"
echo "# then refresh $APP_DIR/$ENV.sh like this"
echo "# ./make.sh env_sh $APP_DIR $ENV"
echo ""
echo "if [ -z \${APP_DIR+x} ]; then"
echo " # Caller can override \$APP_DIR"
echo " export APP_DIR=$APP_DIR"
echo "fi"
echo ""
} >>"${ENV_SH}"
# Create array of key values
# https://stackoverflow.com/a/23118607/639133
KEY_VALUES=()
while IFS='' read -r line; do
KEY_VALUES+=("$line")
done < <(gojq -r 'to_entries|map("\(.key)\n\(.value|tostring)")|.[]' "${CONFIG}")
for ((i = 0; i < ${#KEY_VALUES[@]}; i = i + 2)); do
KEY="${KEY_VALUES[$i]}"
VALUE="${KEY_VALUES[$i + 1]}"
echo "export ${KEY}=\"${VALUE}\"" >>"${ENV_SH}"
done
echo "" >>"${ENV_SH}"
echo 'printenv | sort | grep --color -E "APP_|AWS_"' >>"${ENV_SH}"
chmod u+x "${ENV_SH}"
}
# gen_pkg_config generates pkg/config
gen_pkg_config() {
depends configu
cd "$APP_DIR"
configu -generate ./pkg/config
go fmt ./pkg/config/config.go || :
go fmt ./pkg/config/fn.go || :
go fmt ./pkg/config/template.go || :
}
# ..............................................................................
# Required env vars
APP_DIR=${APP_DIR}
GOPATH=${GOPATH}
# Execute FUNC if it's a func defined in this script
TYPE=$(type -t "${FUNC}" || echo "undefined")
if [[ ${TYPE} == "function" ]]; then
# Additional arguments, after the func, are passed through.
# For example, `./make.sh FUNC ARG1`
${FUNC} "${@:2}"
else
echo "FUNC ${FUNC} not implemented"
exit 1
fi