-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
executable file
·198 lines (174 loc) · 6.49 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
#!/usr/bin/env bash
# Copyright © 2018 Joel Baranick <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
BUILD_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"
BINARY_DIR="$BUILD_DIR/.bin"
VERSION=$(cat $BUILD_DIR/.version)
function verbose() { echo -e "$*"; }
function error() { echo -e "ERROR: $*" 1>&2; }
function fatal() { echo -e "ERROR: $*" 1>&2; exit 1; }
function pushd () { command pushd "$@" > /dev/null; }
function popd () { command popd > /dev/null; }
function trap_add() {
localtrap_add_cmd=$1; shift || fatal "${FUNCNAME} usage error"
for trap_add_name in "$@"; do
trap -- "$(
extract_trap_cmd() { printf '%s\n' "$3"; }
eval "extract_trap_cmd $(trap -p "${trap_add_name}")"
printf '%s\n' "${trap_add_cmd}"
)" "${trap_add_name}" || fatal "unable to add to trap ${trap_add_name}"
done
}
declare -f -t trap_add
function get_platform() {
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*)
echo "linux"
;;
Darwin*)
echo "darwin"
;;
*)
echo "Unsupported machine type :${unameOut}"
exit 1
;;
esac
}
PLATFORM=$(get_platform)
GOX="gox"
GOMETALINTER=$BINARY_DIR/gometalinter
GOMETALINTER_URL="https://github.com/alecthomas/gometalinter/releases/download/v2.0.4/gometalinter-2.0.4-$PLATFORM-amd64.tar.gz"
CONSUL=$BINARY_DIR/consul
CONSUL_URL="https://releases.hashicorp.com/consul/1.7.9/consul_1.7.9_${PLATFORM}_amd64.zip"
function download_consul() {
if [ ! -f "$CONSUL" ]; then
verbose " --> $CONSUL"
local tmpdir=`mktemp -d`
trap_add "rm -rf $tmpdir" EXIT
pushd $tmpdir
curl -L -s -O $CONSUL_URL || fatal "failed to download '$CONSUL_URL': $?"
for i in *.zip; do
[ "$i" = "*.zip" ] && continue
unzip -q "$i" && rm -r "$i"
done
popd
mkdir -p $BINARY_DIR
cp $tmpdir/* $BINARY_DIR/
fi
}
function download_gometalinter() {
if [ ! -f "$GOMETALINTER" ]; then
verbose " --> $GOMETALINTER"
local tmpdir=`mktemp -d`
trap_add "rm -rf $tmpdir" EXIT
pushd $tmpdir
curl -L -s -O $GOMETALINTER_URL || fatal "failed to download '$GOMETALINTER_URL': $?"
for i in *.tar.gz; do
[ "$i" = "*.tar.gz" ] && continue
tar xzf "$i" -C $tmpdir --strip-components 1 && rm -r "$i"
done
popd
mkdir -p $BINARY_DIR
cp $tmpdir/* $BINARY_DIR/
fi
}
function download_gox() {
if [ ! -x "$(command -v $GOX)" ]; then
echo " --> $GOX"
go get github.com/mitchellh/gox || fatal "go get 'github.com/mitchellh/gox' failed: $?"
fi
}
function download_binaries() {
download_gox || fatal "failed to download 'gox': $?"
download_gometalinter || fatal "failed to download 'gometalinter': $?"
download_consul || fatal "failed to download 'consul': $?"
export PATH=$PATH:$BINARY_DIR
}
function run() {
local revision=`git rev-parse HEAD`
local branch=`git rev-parse --abbrev-ref HEAD`
local host=`hostname`
local buildDate=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
go version | grep -q 'go version go1.15.2 ' || fatal "go version is not 1.15.2"
if [ -z "$TRAVIS" ]; then
verbose "Cleanup dist..."
rm -rf dist/*
fi
verbose "Fetching binaries..."
download_binaries
verbose "Getting dependencies..."
verbose "Installing dependencies..."
go install ./... || fatal "go install failed: $?"
go test -i ./... || fatal "go test install failed: $?"
verbose "Formatting source..."
local unformattedFileCount="$(gofmt -l -s . | wc -l | awk '{ print $1 }')"
if [[ "$unformattedFileCount" -gt 0 ]]; then
error "Source not formatted"
gofmt -d -s .
exit 1
fi
verbose "Linting source..."
$GOMETALINTER --disable-all --enable=vet --enable=gocyclo --cyclo-over=15 --enable=golint --min-confidence=.85 --enable=ineffassign --skip=Godeps --skip=vendor --skip=third_party --skip=testdata --vendor ./... || fatal "gometalinter failed: $?"
verbose "Checking licenses..."
licRes=$(
for file in $(find . -type f -iname '*.go'); do
head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || error " Missing license in: ${file}"
done;)
if [ -n "${licRes}" ]; then
fatal "license header checking failed:\n${licRes}"
fi
verbose "Running tests..."
if [ -n "$TRAVIS" ]; then
if [ ! -x "$(command -v goveralls)" ]; then
echo "Getting goveralls..."
go get golang.org/x/tools/cmd/cover || fatal "go get 'golang.org/x/tools/cmd/cover' failed: $?"
go get github.com/mattn/goveralls || fatal "go get 'github.com/mattn/goveralls' failed: $?"
fi
go test -v -covermode=count -coverprofile=coverage.out ./... || fatal "$gopackage tests failed: $?"
goveralls -v -service=travis-ci -coverprofile=coverage.out -ignore=main.go,testutil/server.go,testutil/golden.go -service=travis-ci || fatal "goveralls: $?"
else
go test -v ./... || fatal "$gopackage tests failed: $?"
fi
XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"}
XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"}
if [ -z "$TRAVIS" ]; then
XC_OS=$(go env GOOS)
XC_ARCH=$(go env GOARCH)
fi
verbose "Building binaries..."
$GOX -os="${XC_OS}" -arch="${XC_ARCH}" -osarch="!darwin/arm !darwin/arm64 !darwin/386" -ldflags "-X github.com/kadaan/consulate/version.Version=$VERSION -X github.com/kadaan/consulate/version.Revision=$revision -X github.com/kadaan/consulate/version.Branch=$branch -X github.com/kadaan/consulate/version.BuildUser=$USER@$host -X github.com/kadaan/consulate/version.BuildDate=$buildDate" -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" || fatal "gox failed: $?"
if [ -n "$TRAVIS" ]; then
verbose "Creating archives..."
cd dist
set -x
for f in *; do
filename=$(basename "$f")
extension="${filename##*.}"
filename="${filename%.*}"
if [[ "$filename" != "$extension" ]] && [[ -n "$extension" ]]; then
extension=".$extension"
else
extension=""
fi
archivename="$filename.tar.gz"
verbose " --> $archivename"
genericname="consulate$extension"
mv -f "$f" "$genericname"
tar -czf $archivename "$genericname"
rm -rf "$genericname"
done
fi
}
run "$@"