-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·470 lines (382 loc) · 11.8 KB
/
build.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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env bash
build_core () {
build_one $*
}
build_win () {
build_one amd64 windows win $*
}
build_linux () {
build_one amd64 linux bin $*
}
build_darwin () {
build_one amd64 darwin dar $*
}
build_ci() {
go mod download
build_all
ls -la ./bin/
for f in bin/*; do gzip $f; done
ls -la ./bin/
}
build_all () {
for ARCH in amd64; do
for OS in darwin linux windows; do
build_one $ARCH $OS $OS-$ARCH $*
done
done
}
build_one () {
local ARCH=${1:-amd64}
local OS=${2:-darwin}
local suffix=${3:-dar}
local S=''
case $suffix in
dar) S="";;
bin) S="-$OS-$ARCH";;
win) S="-$OS-$ARCH.exe";;
*) S="-$suffix";;
esac
shift;shift;shift;
echo "PWD=$(pwd)"
headline "---- Building the binary, for $PROJ_DIR | S='$S', OS='$OS' | suffix=$suffix"
cat <<-EOF
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=1 go build -tags="gocql_debug" -ldflags "$LDFLAGS" -o "$PROJ_DIR/bin/${APPNAME}$S" $* $PKG_SRC
EOF
GOARCH=$ARCH GOOS=$OS CGO_ENABLED=1 go build -tags="gocql_debug" -ldflags "$LDFLAGS" -o "$PROJ_DIR/bin/${APPNAME}$S" $* $PKG_SRC && \
chmod +x "$PROJ_DIR/bin/${APPNAME}$S" && \
echo && ls -l "$PROJ_DIR/bin/${APPNAME}$S" # && echo $?
}
app_name () {
local temp=$(grep -E "APP_NAME[ \t]+=[ \t]+" doc.go|grep -Eo "\\\".+\\\"")
temp="${temp%\"}"
temp="${temp#\"}"
echo $temp
}
app_version () {
grep -E "Version[ \t]+=[ \t]+" doc.go|grep -Eo "[0-9.]+"
}
app_version_int () {
grep -E "VersionInt[ \t]+=[ \t]+" doc.go|grep -Eo "[0-9x]+"
}
build_gofmt () {
gofmt -l -s -w .
}
build_fmt () {
gofmt -l -s -w .
}
build_golint() {
golint ./...
}
build_lint() {
golint ./...
}
build_gotest() {
go test ./...
}
build_test() {
go test ./...
}
build_gocov() {
go test -race -covermode=atomic -coverprofile cover.out && \
go tool cover -html=cover.out -o cover.html && \
open cover.html
}
build_gocov_codecov() {
# https://codecov.io/gh/hedzr/cmdr
go test -race -coverprofile=coverage.txt -covermode=atomic
bash <(curl -s https://codecov.io/bash) -t $CODECOV_TOKEN
}
build_gocov_codecov_open() {
open https://codecov.io/gh/hedzr/cmdr
}
build_consul() { commander 'build_consul' "$@"; }
build_consul_usage () {
cat <<-EOF
'consul' Usages:
SUB-COMMANDS:
run, start run deps runtime instances
stop stop deps runtime instances
EOF
}
build_consul_start () {
build_consul_run "$@" &
}
build_consul_run () {
pushd ci >/dev/null
local cmd=$1; [[ "$cmd" == "" ]] && cmd=up || shift
local tmpdir=/tmp/consul
[ -d $tmpdir ] && rm -rf $tmpdir || mkdir $tmpdir
# consul agent -data-dir /tmp/consul -server -bootstrap &
PROJ_DIR=$CD
echo "USING PROJ_DIR = $PROJ_DIR"
echo "USING CONSUL CONFIG DIR: $PROJ_DIR/ci/consul.d"
echo ""
consul agent -config-dir $PROJ_DIR/ci/consul.d -data-dir $tmpdir -server
popd >/dev/null
}
build_consul_stop () {
kill -9 $(lsof -Pni :8500|grep LISTEN|awk '{print $2}')
}
build_mod() { commander 'build_mod' "$@"; }
build_mod_usage () {
cat <<-EOF
'mod' Usages:
SUB-COMMANDS:
run run
test,dump dump the information
Help:
# go mod helpers:
export GO111MODULE=on
export GOPROXY=https://athens.azurefd.net
export GOPATH=$(dirname $(dirname $(dirname $(dirname $CD))))
EOF
}
build_mod_run () {
export GO111MODULE=on
export GOPROXY=https://athens.azurefd.net
export GOPATH=$(dirname $(dirname $CD))
}
build_mod_test () {
cat <<-EOF
GO111MODULE=$GO111MODULE
GOPROXY=$GOPROXY
GOPATH=$GOPATH
EOF
}
build_mod_dump () {
build_mod_test
}
build_deps() { commander 'build_deps' "$@"; }
build_deps_usage () {
cat <<-EOF
'deps' Usages:
SUB-COMMANDS:
run run deps runtime instances
start start deps runtime instances
stop stop deps runtime instances
reset remove the runtime instances of containers completely
consul run small consul for testing.
EOF
}
build_deps_run () {
# run deps docker containers for debugging and developing
pushd ci/deps >/dev/null
local cmd=$1; [[ "$cmd" == "" ]] && cmd=up || shift
docker-compose $cmd "$@"
popd >/dev/null
}
build_deps_start () {
pushd ci/deps >/dev/null
docker-compose start
popd >/dev/null
}
build_deps_stop () {
pushd ci/deps >/dev/null
docker-compose stop
popd >/dev/null
}
build_deps_reset () {
pushd ci/deps >/dev/null
docker-compose rm -f
popd >/dev/null
}
build_deps_consul () {
pushd ci >/dev/null
PROJ_DIR=$CD
[ -d /tmp/consul ] && rm -rf /tmp/consul || mkdir /tmp/consul
# consul agent -data-dir /tmp/consul -server -bootstrap &
echo "USING PROJ_DIR = $PROJ_DIR"
echo "USING CONSUL CONFIG DIR: $PROJ_DIR/ci/consul.d"
echo ""
consul agent -config-dir $PROJ_DIR/ci/consul.d -data-dir /tmp/consul -server
popd >/dev/null
}
build_auto() {
is_zsh && build_auto_zsh
}
build_auto_zsh() {
# [[ $fpath == *$HOME/.oh-my-zsh/completions* ]] && [[ -d $HOME/.oh-my-zsh/completions ]] && DONE=1 && build_auto_zsh_i $HOME/.oh-my-zsh/completions
[[ $DONE -ne 1 ]] && {
local dir=$HOME/.zsh.autocomp
[ -d $dir ] || mkdir $dir
grep -qE "^fpath=\(.*$dir.*\)" $HOME/.zshrc && : || {
cat >>$HOME/.zshrc<<-EOF
fpath=(\$fpath $dir)
EOF
}
build_auto_zsh_i $dir
}
}
build_auto_zsh_i() {
local ash=$1/consul-tags
cat >$ash<<-EOF
#compdef consul-tags consul-tags-userside
#autoload
EOF
}
build_docker() {
# VERSION=$(app_version)
local TS="$(date -u '+%Y-%m-%d_%I:%M:%S%p')"
local GH="$(git rev-parse HEAD)"
docker-compose rm -f
[ -d vendor ] && mv vendor vendor.1
build_linux
headline "docker build -t hedzr/$APPNAME:$VERSION -t hedzr/$APPNAME:latest . --build-arg \"VERSION=$VERSION\" --build-arg \"APPNAME=$APPNAME\" --build-arg \"PKGNAME=$PROJ\" --build-arg \"GITHASH=$GH\" --build-arg \"TS=$TS\""
docker build -t hedzr/$APPNAME:$VERSION -t hedzr/$APPNAME:latest . --build-arg "VERSION=$VERSION" --build-arg "APPNAME=$APPNAME" --build-arg "PKGNAME=$PROJ" --build-arg "GITHASH=$GH" --build-arg "TS=$TS"
[ -d vendor.1 ] && mv vendor.1 vendor
}
build_full_docker() {
#VERSION=1.1.0
#APPNAME=im-core
#PROJ=core
TS="$(date -u '+%Y-%m-%d_%I:%M:%S%p')"
GH="$(git rev-parse HEAD)"
[ -d $HOME/hzw/golang-dev/$APPNAME/vendor ] && {
# [ -L vendor ] && rm vendor
# ln -s $HOME/hzw/golang-dev/$APPNAME/vendor vendor
[ -d vendor ] || {
#echo copy vendor
#cp -R $HOME/hzw/golang-dev/$APPNAME/vendor ./
glide i -v --skip-test
}
}
docker build -t im-core \
--build-arg "VERSION=$VERSION" --build-arg "APPNAME=$APPNAME" \
--build-arg "PKGNAME=$PROJ" --build-arg "GITHASH=$GH" --build-arg "TS=$TS" \
-f ./Dockerfile.full-docker .
}
build_etcd () {
etcdctl get root --prefix
}
build_help(){
cat <<-EOF
Usages: $0 [command]
Commands:
(nothing) go build "consul-tags" to executable
etcd Print the service registry entries
consul (( consul sub-commands set ))
deps (( deps sub-commands set ))
help Show this screen
EOF
}
build_usage(){ build_help; }
build_usages(){ build_help; }
build_h(){ build_help; }
build_-h(){ build_help; }
build_--help(){ build_help; }
#### write your functions here, and invoke them by: `./bash.sh <your-func-name>`
cool(){ echo cool; }
sleeping(){ echo sleeping; }
_my_main_do_sth(){
local cmd=${1:-core} && { [ $# -ge 1 ] && shift; } || :
# for linux only:
# local cmd=${1:-sleeping} && shift || :
is_vagrant && CD=/vagrant && SCRIPT=$CD/bootstrap.sh
headline "CD = $CD, SCRIPT = $SCRIPT, PWD=$(pwd)"
# [ -d $CD/.boot.d ] || mkdir -p $CD/.boot.d
# [ -z "$(ls -A $CD/.boot.d/)" ] && for f in $CD/.boot.d/*; do source $f; done
local GOSRC=$GOPATH/src/
local APPNAME=$(app_name)
local PWD=$(pwd)
local PKG=${PWD/$GOSRC/}
local PROJ=$(cat doc.go|grep -o 'package .*'|awk '{print $2}')
local PROJ_DIR=$PWD
local VERSION=$(app_version)
local versionInt=$(app_version_int)
echo "appName=$APPNAME | pkg=$PKG, $PROJ | version = $VERSION, $versionInt"
# local W_PKG="github.com/hedzr/common/cli-tpl/cli_common"
local W_PKG="github.com/hedzr/cmdr"
local LDFLAGS="-s -w -X ${W_PKG}.Buildstamp=`date -u '+%Y-%m-%d_%I:%M:%S%p'` -X ${W_PKG}.Githash=`git rev-parse HEAD` -X ${W_PKG}.Version=$VERSION -X ${W_PKG}.AppName=$APPNAME"
PKG_SRC=cli/main.go
#echo "LDFLAGS=$LDFLAGS"
debug "build_$cmd - $@"
eval "build_$cmd $@" || { echo 'not true'; :; }
}
is_vagrant() { [[ -d /vagrant ]]; }
#### HZ Tail BEGIN ####
in_debug() { [[ $DEBUG -eq 1 ]]; }
is_root() { [ "$(id -u)" = "0" ]; }
is_bash() { is_bash_t1 && is_bush_t2; }
is_bash_t1() { [ -n "$BASH_VERSION" ]; }
is_bash_t2() { [ ! -n "$BASH" ]; }
is_zsh() { [[ $SHELL == */zsh ]]; }
is_zsh_t2() { [ -n "$ZSH_NAME" ]; }
is_darwin() { [[ $OSTYPE == *darwin* ]]; }
is_linux() { [[ $OSTYPE == *linux* ]]; }
in_sourcing() { is_zsh && [[ "$ZSH_EVAL_CONTEXT" == toplevel* ]] || [[ $(basename -- "$0") != $(basename -- "${BASH_SOURCE[0]}") ]]; }
is_interactive_shell () { [[ $- == *i* ]]; }
is_not_interactive_shell () { [[ $- != *i* ]]; }
is_ps1 () { [ -z "$PS1" ]; }
is_not_ps1 () { [ ! -z "$PS1" ]; }
is_stdin () { [ -t 0 ]; }
is_not_stdin () { [ ! -t 0 ]; }
headline() { printf "\e[0;1m$@\e[0m:\n"; }
headline_begin() { printf "\e[0;1m"; } # for more color, see: shttps://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
headline_end() { printf "\e[0m:\n"; } # https://misc.flogisoft.com/bash/tip_colors_and_formatting
printf_black() { printf "\e[0;30m$@\e[0m:\n"; }
printf_red() { printf "\e[0;31m$@\e[0m:\n"; }
printf_green() { printf "\e[0;32m$@\e[0m:\n"; }
printf_yellow() { printf "\e[0;33m$@\e[0m:\n"; }
printf_blue() { printf "\e[0;34m$@\e[0m:\n"; }
printf_purple() { printf "\e[0;35m$@\e[0m:\n"; }
printf_cyan() { printf "\e[0;36m$@\e[0m:\n"; }
printf_white() { printf "\e[0;37m$@\e[0m:\n"; }
debug() { in_debug && printf "\e[0;38;2;133;133;133m$@\e[0m\n" || :; }
debug_begin() { printf "\e[0;38;2;133;133;133m"; }
debug_end() { printf "\e[0m\n"; }
dbg() { ((DEBUG)) && printf ">>> \e[0;38;2;133;133;133m$@\e[0m\n" || :; }
debug_info() {
debug_begin
cat <<-EOF
in_debug: $(in_debug && echo Y || echo '-')
is_root: $(is_root && echo Y || echo '-')
is_bash: $(is_bash && echo Y || echo '-')
is_zsh: $(is_zsh && echo Y || echo '-')
in_sourcing: $(in_sourcing && echo Y || echo '-') # ZSH_EVAL_CONTEXT = $ZSH_EVAL_CONTEXT
is_interactive_shell: $(is_interactive_shell && echo Y || echo '-')
EOF
debug_end
}
commander () {
local self=$1; shift;
local cmd=${1:-usage}; [ $# -eq 0 ] || shift;
#local self=${FUNCNAME[0]}
case $cmd in
help|usage|--help|-h|-H) "${self}_usage" "$@"; ;;
funcs|--funcs|--functions|--fn|-fn) script_functions "^$self"; ;;
*)
if [ "$(type -t ${self}_${cmd}_entry)" == "function" ]; then
"${self}_${cmd}_entry" "$@"
else
"${self}_${cmd}" "$@"
fi
;;
esac
}
script_functions () {
# shellcheck disable=SC2155
local fncs=$(declare -F -p | cut -d " " -f 3|grep -vP "^[_-]"|grep -vP "\\."|grep -vP "^[A-Z]"); # Get function list
if [ $# -eq 0 ]; then
echo "$fncs"; # not quoted here to create shell "argument list" of funcs.
else
echo "$fncs"|grep -P "$@"
fi
#declare MyFuncs=($(script.functions));
}
main_do_sth() {
set -e
trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
trap '[ $? -ne 0 ] && echo FAILED COMMAND: $previous_command with exit code $?' EXIT
MAIN_DEV=${MAIN_DEV:-eth0}
MAIN_ENTRY=${MAIN_ENTRY:-_my_main_do_sth}
# echo $MAIN_ENTRY - "$@"
in_debug && { debug_info; echo "$SHELL : $ZSH_NAME - $ZSH_VERSION | BASH_VERSION = $BASH_VERSION"; [ -n "$ZSH_NAME" ] && echo "x!"; }
$MAIN_ENTRY "$@"
trap - EXIT
${HAS_END:-$(false)} && { debug_begin;echo -n 'Success!';debug_end; } || :
}
DEBUG=${DEBUG:-0}
is_darwin && realpathx(){ [[ $1 == /* ]] && echo "$1" || echo "$PWD/${1#./}"; } || realpathx () { readlink -f $*; }
in_sourcing && { CD=${CD}; debug ">> IN SOURCING, \$0=$0, \$_=$_"; } || { SCRIPT=$(realpathx $0) && CD=$(dirname $SCRIPT) && debug ">> '$SCRIPT' in '$CD', \$0='$0','$1'."; }
main_do_sth "$@"
#### HZ Tail END ####