This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
before-commit.sh
executable file
·128 lines (113 loc) · 2.94 KB
/
before-commit.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
#!/usr/bin/env bash
readonly CI_FLAG=ci
readonly TEST_ACC_FLAG=testacc
RED='\033[0;31m'
GREEN='\033[0;32m'
INVERTED='\033[7m'
NC='\033[0m' # No Color
echo -e "${INVERTED}"
echo "USER: " + $USER
echo "PATH: " + $PATH
echo "GOPATH:" + $GOPATH
echo -e "${NC}"
##
# Tidy dependencies
##
go mod tidy
tidyResult=$?
if [ ${tidyResult} != 0 ]; then
echo -e "${RED}✗ go mod tidy${NC}\n$tidyResult${NC}"
exit 1
else echo -e "${GREEN}√ go mod tidy${NC}"
fi
##
# GO BUILD
##
if [ "$1" == "$CI_FLAG" ] || [ "$2" == "$CI_FLAG" ]; then
# build all binaries
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/terraform-provider-kind-windows-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (windows)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (windows)${NC}"
fi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/terraform-provider-kind-linux-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (linux)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (linux)${NC}"
fi
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/terraform-provider-kind-darwin-amd64
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (mac)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (mac)${NC}"
fi
else
# build just current arch
CGO_ENABLED=0 go build -o bin/terraform-provider-kind
goBuildResult=$?
if [ ${goBuildResult} != 0 ]; then
echo -e "${RED}✗ go build (dev)${NC}\n$goBuildResult${NC}"
exit 1
else echo -e "${GREEN}√ go build (dev)${NC}"
fi
fi
##
# Verify dependencies
##
echo "? go mod verify"
depResult=$(go mod verify)
if [ $? != 0 ]; then
echo -e "${RED}✗ go mod verify\n$depResult${NC}"
exit 1
else echo -e "${GREEN}√ go mod verify${NC}"
fi
##
# GO TEST
##
echo "? go test"
go test ./...
# Check if tests passed
if [ $? != 0 ]; then
echo -e "${RED}✗ go test\n${NC}"
exit 1
else echo -e "${GREEN}√ go test${NC}"
fi
goFilesToCheck=$(find . -type f -name "*.go" | egrep -v "\/vendor\/|_*/automock/|_*/testdata/|_*export_test.go")
##
# TF ACCEPTANCE TESTS
##
if [ "$1" == "$TEST_ACC_FLAG" ] || [ "$2" == "$TEST_ACC_FLAG" ]; then
# run terraform acceptance tests
if [ "$1" == "$CI_FLAG" ] || [ "$2" == "$CI_FLAG" ]; then
TF_ACC=1 go test ./kind -v -count 1 -parallel 20 -timeout 120m
else
TF_ACC=1 go test ./kind -v -count 1 -parallel 1 -timeout 120m
fi
fi
#
# GO FMT
#
goFmtResult=$(echo "${goFilesToCheck}" | xargs -L1 go fmt)
if [ $(echo ${#goFmtResult}) != 0 ]
then
echo -e "${RED}✗ go fmt${NC}\n$goFmtResult${NC}"
exit 1;
else echo -e "${GREEN}√ go fmt${NC}"
fi
##
# GO VET
##
packagesToVet=("./kind/...")
for vPackage in "${packagesToVet[@]}"; do
vetResult=$(go vet ${vPackage})
if [ $(echo ${#vetResult}) != 0 ]; then
echo -e "${RED}✗ go vet ${vPackage} ${NC}\n$vetResult${NC}"
exit 1
else echo -e "${GREEN}√ go vet ${vPackage} ${NC}"
fi
done