generated from samkenxstream/SAMkenxcircleci-ex-service-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
do
executable file
·323 lines (279 loc) · 8.24 KB
/
do
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
#!/usr/bin/env bash
set -eu -o pipefail
_version=1.0.${CIRCLE_BUILD_NUM-0}-$(git rev-parse --short HEAD 2>/dev/null || echo latest)
reportDir="test-reports"
serviceName="ex-service-template"
make-target() {
mkdir -p "target"
target="target/bin/$(go env GOOS)/$(go env GOARCH)"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_binary="Build the service binaries."
binary() {
local ldflags
ldflags="${1:-}" # Optionally accept build flags
make-target
local suffix=""
if [ "$(go env GOOS)" = "windows" ]; then
suffix=".exe"
fi
export CGO_ENABLED=0
set -x
go build -ldflags "$ldflags" -o "$target/api${suffix}" ./cmd/api
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_build="Build the binaries for production"
build() {
local date ldflags
date=$(date " %FT%T%z")
ldflags="-s -w -X github.com/circleci/${serviceName}/cmd.Version=$_version -X github.com/circleci/${serviceName}/cmd.Date=$date"
GOOS=linux GOARCH=amd64 binary "$ldflags" &
local fail="0"
for job in $(jobs -p); do
wait "${job}" || (( fail =1 ))
done
if [ "$fail" == "0" ]; then
echo "Compilation succeeded"
else
echo "Compile failed ($fail)"
exit 1
fi
mkdir -p target
echo "$_version" | tee target/version.txt
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_lint="Run golanci-lint to lint go files."
lint() {
exec ./bin/golangci-lint run "${@:-./...}"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_lint_report="Run golanci-lint to lint go files and generate an xml report."
lint-report() {
output="${reportDir}/lint.xml"
echo "Storing results as Junit XML in ${output}" >&2
mkdir -p "${reportDir}"
lint ./... --out-format junit-xml | tee "${output}"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_test="Run normal unit tests"
test() {
mkdir -p "${reportDir}"
# -count=1 is used to forcibly disable test result caching
./bin/gotestsum --junitfile="${reportDir}/junit.xml" -- -race -count=1 "${@:-./...}"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_godoc="Run godoc to read documentation."
godoc() {
install-go-bin "golang.org/x/tools/cmd/[email protected]"
local url
url="http://localhost:6060/pkg/github.com/circleci/${serviceName}/"
command -v xdg-open && xdg-open $url &
command -v open && open $url &
./bin/godoc -http=127.0.0.1:6060
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_go_mod_tidy="Run 'go mod tidy' to clean up module files."
go-mod-tidy() {
go mod tidy -v
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_install_devtools="Install tools required for doing dev stuff"
install-devtools() {
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.45.2
install-go-bin \
"gotest.tools/[email protected]"
}
install-go-bin() {
for pkg in "${@}"; do
GOBIN="${PWD}/bin" go install "${pkg}" &
done
wait
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_create_stub_test_files="Create an empty pkg_test in all directories with no tests.
Creating this blank test file will ensure that coverage considers all
packages, not just those with tests.
"
create-stub-test-files() {
# Variable expansion is not intended within the single quoted command
# shellcheck disable=SC2016
go list -f '{{if not .TestGoFiles}}{{.Name}} {{.Dir}}{{end}}' ./... | \
xargs -r --max-args=2 bash -c 'echo "package $0" > "$1/pkg_test.go"'
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_run_goimports="Run goimports for package"
run-goimports () {
command -v ./bin/goimports || install-go-bin "golang.org/x/tools/cmd/[email protected]"
./bin/goimports -local "github.com/circleci/${serviceName}" -w "${@:-.}"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_version="Print version"
version() {
echo "$_version"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_generate="generate any generated code"
generate() {
go generate -x ./...
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_schema="Generate the migrations/schema.sql file from the current DB"
schema() {
if docker-compose ps | grep -q postgres; then
echo "Looks like postgres is still running, \
please stop it with 'docker-compose down postgres' \
before running ./do schema"
exit 1
fi
local filename
filename="${1:-migrations/schema.sql}"
# Ensure the DB schema is up-to-date
docker-compose run db-schema
# Them dump it to the file
# pg_dump uses \r\n line endings, which we tidy up
printf -- "-- This file is generated by ./do schema\n\n" > "${filename}"
docker-compose exec -T postgres pg_dump \
--dbname=ex-service-template \
--no-tablespaces \
--schema-only \
--no-privileges \
--no-owner \
| tr -d "\r" \
>> "${filename}"
}
help-text-intro() {
echo "
DO
A set of simple repetitive tasks that adds minimally
to standard tools used to build and test the service.
(e.g. go and docker)
"
}
help_start_api="Start the local API server on port 8000"
start-api() {
source .env
go run "${@:-./cmd/api}"
}
### START FRAMEWORK ###
# Do Version 0.0.4
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_self_update="Update the framework from a file.
Usage: $0 self-update FILENAME
"
self-update() {
local source selfpath pattern
source="$1"
selfpath="${BASH_SOURCE[0]}"
cp "$selfpath" "$selfpath.bak"
pattern='/### START FRAMEWORK/,/END FRAMEWORK ###$/'
(sed "${pattern}d" "$selfpath"; sed -n "${pattern}p" "$source") \
> "$selfpath.new"
mv "$selfpath.new" "$selfpath"
chmod --reference="$selfpath.bak" "$selfpath"
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_completion="Print shell completion function for this script.
Usage: $0 completion SHELL"
completion() {
local shell
shell="${1-}"
if [ -z "$shell" ]; then
echo "Usage: $0 completion SHELL" 1>&2
exit 1
fi
case "$shell" in
bash)
(echo
echo '_dotslashdo_completions() { '
# shellcheck disable=SC2016
echo ' COMPREPLY=($(compgen -W "$('"$0"' list)" "${COMP_WORDS[1]}"))'
echo '}'
echo 'complete -F _dotslashdo_completions '"$0"
);;
zsh)
cat <<EOF
_dotslashdo_completions() {
local -a subcmds
subcmds=()
DO_HELP_SKIP_INTRO=1 $0 help | while read line; do
EOF
cat <<'EOF'
cmd=$(cut -f1 <<< $line)
cmd=$(awk '{$1=$1};1' <<< $cmd)
desc=$(cut -f2- <<< $line)
desc=$(awk '{$1=$1};1' <<< $desc)
subcmds =("$cmd:$desc")
done
_describe 'do' subcmds
}
compdef _dotslashdo_completions do
EOF
;;
fish)
cat <<EOF
complete -e -c do
complete -f -c do
for line in (string split \n (DO_HELP_SKIP_INTRO=1 $0 help))
EOF
cat <<'EOF'
set cmd (string split \t $line)
complete -c do -a $cmd[1] -d $cmd[2]
end
EOF
;;
esac
}
list() {
declare -F | awk '{print $3}'
}
# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_help="Print help text, or detailed help for a task."
help() {
local item
item="${1-}"
if [ -n "${item}" ]; then
local help_name
help_name="help_${item//-/_}"
echo "${!help_name-}"
return
fi
if [ -z "${DO_HELP_SKIP_INTRO-}" ]; then
type -t help-text-intro > /dev/null && help-text-intro
fi
for item in $(list); do
local help_name text
help_name="help_${item//-/_}"
text="${!help_name-}"
[ -n "$text" ] && printf "%-30s\t%s\n" "$item" "$(echo "$text" | head -1)"
done
}
case "${1-}" in
list) list;;
""|"help") help "${2-}";;
*)
if ! declare -F "${1}" > /dev/null; then
printf "Unknown target: %s\n\n" "${1}"
help
exit 1
else
"$@"
fi
;;
esac
### END FRAMEWORK ###