-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.sh
executable file
·206 lines (176 loc) · 4.75 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
#!/usr/bin/env sh
set -xe
# system functions
basename() {
# Usage: basename "path" ["suffix"]
local tmp
tmp=${1%"${1##*[!/]}"}
tmp=${tmp##*/}
tmp=${tmp%'${2/"$tmp"}'}
printf '%s\n' "${tmp:-/}"
}
lstrip() {
# Usage: lstrip "string" "pattern"
printf '%s\n' "${1##$2}"
}
WORK_DIR=$(pwd)
# if company name not set - try to get it from the path$
if [ -z "${COMPANY_NAME}" ]; then
COMPANY_NAME=$(lstrip $(basename `cd ..; pwd`) "pro")
else
COMPANY_NAME="${COMPANY_NAME}"
fi
SERVICE_NAME=$(lstrip $(basename $(pwd)) "i-")
REPOSITORY=$COMPANY_NAME/$SERVICE_NAME
# FIX SED for macos
SED=`which gsed` || true
if [ -z "$SED" ]; then
SED=`which sed`
fi
init() {
GO_FILES=$(find . -name '*.go' | grep -v _test.go)
PKG_LIST=$(go list ./... | grep -v /lib/)
}
build() {
GIT_COMMIT=$(git rev-parse --short HEAD)
BUILD_DATE=$(date "+%Y%m%d")
for file in cmd/*; do
if [ -d "$file" ]; then
name=${file##*/}
go build -ldflags "-s -w -X main.repository=${REPOSITORY} -X main.revisionID=${GIT_COMMIT} -X main.version=${BUILD_DATE}:${GIT_COMMIT} -X main.service=${SERVICE_NAME}" -o ./$name ./cmd/$name/*.go && chmod +x ./$name
fi
done
}
self_update() {
[ ! -d "etc/" ] && mkdir etc;
docker pull cr.webdevelop.us/webdevelop-pro/go-common:latest-dev;
docker rm -f makesh;
docker run --name=makesh cr.webdevelop.us/webdevelop-pro/go-common:latest-dev sh &&
# docker cp makesh:/app/etc/make.sh make.sh;
docker cp makesh:/app/etc/golangci.yml .golangci.yml;
docker cp makesh:/app/etc/air.toml .air.toml
docker cp makesh:/app/etc/pre-commit etc/pre-commit;
docker stop makesh;
}
case $1 in
install)
echo "golang global dependencies"
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2
go install github.com/go-swagger/go-swagger/cmd/swagger@latest
go install github.com/securego/gosec/v2/cmd/gosec@latest
go install github.com/cosmtrek/air@latest
echo "set up pre-commit hook and make.sh file"
self_update;
if [ -d ".git" -a -d ".git/hooks" ]
then
rm .git/hooks/pre-commit 2>/dev/null || echo 'ok' # ignore error to let sh continue
ln -s ../../etc/pre-commit .git/hooks/pre-commit
fi
;;
lint)
dirlist=`ls`
for ddir in $dirlist[@]
do
if [ -d $ddir ]
then
if [ -f "$ddir/go.mod" ]; then
cd $ddir
golangci-lint -c ../.golangci.yml run --fix $2 $3 || echo 'not ok'
cd ../
fi
fi
done
;;
test)
dirlist=`ls`
for ddir in $dirlist[@]
do
if [ -d $ddir ]
then
if [ -f "$ddir/go.mod" ]; then
cd $ddir
go test -count=1 -p 1 ./... $2 $3
cd ../
fi
fi
done
;;
race)
init
go test -race -short ${PKG_LIST}
;;
self-update)
docker rm -f makesh;
self_update;
;;
run-dev)
# make sure you have proper .air.toml
air
;;
memory)
init
CC=clang go test -msan -short ${PKG_LIST}
;;
coverage)
init
mkdir /tmp/coverage >/dev/null
rm /tmp/coverage/*.cov
for package in ${PKG_LIST}; do
go test -covermode=count -coverprofile "/tmp/coverage/${package##*/}.cov" "$package"
done
tail -q -n +2 /tmp/coverage/*.cov >>/tmp/coverage/coverage.cov
go tool cover -func=/tmp/coverage/coverage.cov
;;
run)
if [ -z "$2" ]
then
(build || echo Failed build http) && $(dirname $0)/http
else
(build || echo Failed build $2) && $(dirname $0)/$2
fi
;;
audit)
echo "running gosec"
gosec ./...
;;
build)
build
;;
swag-doc)
# docs - https://goswagger.io/use/spec/route.html
swagger generate spec --scan-models -o swagger.json
;;
deploy-dev)
BRANCH_NAME=`git rev-parse --abbrev-ref HEAD`
GIT_COMMIT=`git rev-parse --short HEAD`
echo $BRANCH_NAME, $GIT_COMMIT
docker build -t cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:$GIT_COMMIT -t cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:latest-dev --platform=linux/amd64 .
# snyk container test cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:$GIT_COMMIT
if [ $? -ne 0 ]; then
echo "===================="
echo "snyk has found a vulnerabilities, please consider choosing alternative image from snyk"
echo "===================="
fi
docker push cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:$GIT_COMMIT
docker push cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:latest-dev
kubectl -n $COMPANY_NAME-dev set image deployment/$SERVICE_NAME $SERVICE_NAME=cr.webdevelop.us/$COMPANY_NAME/$SERVICE_NAME:$GIT_COMMIT
;;
update-version)
find ./ -name "go.mod" -exec $SED -i "s/$2/$3/g" {} \;
dirlist=`ls`
for ddir in $dirlist[@]
do
if [ -d $ddir ]; then
if [ -f "$ddir/go.mod" ]; then
cd $ddir; rm go.sum; go mod tidy; cd ..;
fi
fi
done
;;
help)
cat make.sh | grep "^[a-z-]*)"
;;
*)
echo "unknown $1, try help"
;;
esac