-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlint.sh
173 lines (153 loc) · 3.47 KB
/
lint.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
#!/bin/bash
function init() {
# initialize environment variables
export is_print_help=0
export exit_code=0
export exit_on_error=0
}
function main() {
# process print help information
is_print_help "$1"
if [ $is_print_help == 1 ]; then
print_help
return 0
fi
# process arguments
if [ "$1" == "-e" ] || [ "$2" == "-e" ]; then
export exit_on_error=1
fi
# start check code
check_all "$1"
# check exit code
if [ $exit_code == 0 ]; then
echo all check passed
else
echo exit code: $exit_code
fi
return $exit_code
}
function is_print_help() {
if [ "$1" == "-help" ]; then
export is_print_help=1
return
fi
if [ "$1" == "--help" ]; then
export is_print_help=1
return
fi
if [ "$1" == "-h" ]; then
export is_print_help=1
return
fi
}
function print_help() {
echo "Usage of lint:"
echo " -golint only use golint to check code"
echo " -gocyclo only use gocyclo to check code"
echo " -cilint only use golangci-lint to check code"
echo " -gosec only use gosec to check code"
echo " -e interrupt script when detect error"
echo
echo "example:"
echo " \"./lint.sh -golint\" only use golint to check code"
echo " \"./lint.sh -gosec -e\" only use gosec and exit on error"
}
function check_all() {
# linux
export GOOS=linux
export GOARCH=amd64
check "$1"
export GOARCH=386
check "$1"
export GOARCH=arm64
check "$1"
# windows
export GOOS=windows
export GOARCH=amd64
check "$1"
export GOARCH=386
check "$1"
export GOARCH=arm64
check "$1"
# darwin
export GOOS=darwin
export GOARCH=amd64
check "$1"
export GOARCH=arm64
check "$1"
}
function check() {
echo "================================================"
echo check $GOOS $GOARCH
echo ------------------------------------------------
case "$1" in
"-golint")
golint -set_exit_status -min_confidence 0.3 ./...
set_exit_code
echo_line
;;
"-gocyclo")
gocyclo -avg -over 15 .
set_exit_code
echo_line
;;
"-cilint")
golangci-lint run ./...
set_exit_code
echo_line
;;
"-gosec")
gosec -quiet ./...
set_exit_code
echo_line
;;
*)
echo ------------------------------------------------
echo golint
echo ------------------------------------------------
golint -set_exit_status -min_confidence 0.3 ./...
set_exit_code
echo ------------------------------------------------
echo
echo ------------------------------------------------
echo gocyclo
echo ------------------------------------------------
gocyclo -avg -over 15 .
set_exit_code
echo ------------------------------------------------
echo
echo ------------------------------------------------
echo golangci-lint
echo ------------------------------------------------
golangci-lint run ./...
set_exit_code
echo ------------------------------------------------
echo
echo ------------------------------------------------
echo gosec
echo ------------------------------------------------
gosec -quiet ./...
set_exit_code
echo ------------------------------------------------
echo_line
;;
esac
}
function set_exit_code() {
if [ $? == 0 ]; then
echo pass
return
else
export exit_code=1
fi
if [ $exit_on_error != 1 ]; then
return
fi
exit $exit_code
}
function echo_line() {
echo "================================================"
echo
}
init
main "$1" "$2"