1616
1717set -o pipefail
1818
19+ # =================================================
20+ # CUSTOMIZE ME:
21+
22+ # Name of the plugin
23+ PLUGIN=" kn-quickstart"
24+
25+ # Directories containing go code which needs to be formatted
26+ SOURCE_DIRS=" cmd pkg internal"
27+
28+ # Directory which should be compiled
29+ MAIN_SOURCE_DIR=" cmd"
30+
31+ # Package which holds the version variables
32+ VERSION_PACKAGE=" knative.dev/kn-plugin-quickstart/internal/command"
33+
1934# =================================================
2035
2136# Store for later
@@ -92,6 +107,12 @@ run() {
92107
93108
94109codegen () {
110+ # TODO: Workaround to update go-licenses until prow-tests image is updated
111+ local temp_dir=" $( mktemp -d) "
112+ pushd " ${temp_dir} " > /dev/null 2>&1
113+ GO111MODULE=on go get github.com/google/go-licenses@latest || return 1
114+ popd > /dev/null 2>&1
115+
95116 # Update dependencies
96117 update_deps
97118
@@ -102,32 +123,32 @@ codegen() {
102123 check_license
103124}
104125
105- go_fmt () {
106- echo " 🧹 ${S} Format"
107- find $( echo $SOURCE_DIRS ) -name " *.go" -print0 | xargs -0 gofmt -s -w
126+ # Run a go tool, get it first if necessary.
127+ run_go_tool () {
128+ local tool=$2
129+ local install_failed=0
130+ if [ -z " $( which ${tool} ) " ]; then
131+ local temp_dir=" $( mktemp -d) "
132+ pushd " ${temp_dir} " > /dev/null 2>&1
133+ GOFLAGS=" " go get " $1 " || install_failed=1
134+ popd > /dev/null 2>&1
135+ rm -rf " ${temp_dir} "
136+ fi
137+ (( install_failed )) && return ${install_failed}
138+ shift 2
139+ ${tool} " $@ "
108140}
109141
110142source_format () {
111143 set +e
112- which goimports > /dev/null 2>&1
113- if [ $? -ne 0 ]; then
114- echo " ✋ No 'goimports' found. Please use"
115- echo " ✋ go install golang.org/x/tools/cmd/goimports"
116- echo " ✋ to enable import cleanup. Import cleanup skipped."
117-
118- # Run go fmt instead
119- go_fmt
120- else
121- echo " 🧽 ${X} Format"
122- goimports -w $( echo $SOURCE_DIRS )
123- find $( echo $SOURCE_DIRS ) -name " *.go" -print0 | xargs -0 gofmt -s -w
124- fi
144+ run_go_tool golang.org/x/tools/cmd/goimports goimports -w $( echo $SOURCE_DIRS )
145+ find $( echo $SOURCE_DIRS ) -name " *.go" -print0 | xargs -0 gofmt -s -w
125146 set -e
126147}
127148
128149go_build () {
129150 echo " 🚧 Compile"
130- go build -ldflags " $( build_flags $( basedir) ) " -o $PLUGIN " ./$MAIN_SOURCE_DIR /..."
151+ go build -mod=vendor - ldflags " $( build_flags $( basedir) ) " -o $PLUGIN " ./$MAIN_SOURCE_DIR /..."
131152}
132153
133154go_test () {
@@ -143,7 +164,7 @@ go_test() {
143164
144165 echo " 🧪 ${X} Test"
145166 set +e
146- go test -v ./pkg/... ./internal/... > $test_output 2>&1
167+ go test -v ./pkg/... > $test_output 2>&1
147168 local err=$?
148169 if [ $err -ne 0 ]; then
149170 echo " 🔥 ${red} Failure${reset} "
@@ -183,8 +204,7 @@ check_license() {
183204
184205update_deps () {
185206 echo " 🚒 Update"
186- go mod tidy
187- go mod vendor
207+ $( basedir) /hack/update-deps.sh
188208}
189209
190210watch () {
@@ -259,17 +279,12 @@ cross_build() {
259279
260280 export CGO_ENABLED=0
261281 echo " 🐧 ${PLUGIN} -linux-amd64"
262- GOOS=linux GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./${PLUGIN} -linux-amd64 " ./$MAIN_SOURCE_DIR /..." || failed=1
263- echo " 💪 ${PLUGIN} -linux-arm64"
264- GOOS=linux GOARCH=arm64 go build -mod=vendor -ldflags " ${ld_flags} " -o ./${PLUGIN} -linux-arm64 ./cmd/... || failed=1
282+ GOOS=linux GOARCH=amd64 go build -mod=vendor -ldflags " ${ld_flags} " -o ./${PLUGIN} -linux-amd64 " ./$MAIN_SOURCE_DIR /..." || failed=1
265283 echo " 🍏 ${PLUGIN} -darwin-amd64"
266- GOOS=darwin GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./${PLUGIN} -darwin-amd64 " ./$MAIN_SOURCE_DIR /..." || failed=1
284+ GOOS=darwin GOARCH=amd64 go build -mod=vendor - ldflags " ${ld_flags} " -o ./${PLUGIN} -darwin-amd64 " ./$MAIN_SOURCE_DIR /..." || failed=1
267285 echo " 🎠 ${PLUGIN} -windows-amd64.exe"
268- GOOS=windows GOARCH=amd64 go build -ldflags " ${ld_flags} " -o ./${PLUGIN} -windows-amd64.exe " ./$MAIN_SOURCE_DIR /..." || failed=1
269- echo " Z ${PLUGIN} -linux-s390x"
270- GOOS=linux GOARCH=s390x go build -mod=vendor -ldflags " ${ld_flags} " -o ./${PLUGIN} -linux-s390x ./cmd/... || failed=1
271- echo " P ${PLUGIN} -linux-ppc64le"
272- GOOS=linux GOARCH=ppc64le go build -mod=vendor -ldflags " ${ld_flags} " -o ./${PLUGIN} -linux-ppc64le ./cmd/... || failed=1
286+ GOOS=windows GOARCH=amd64 go build -mod=vendor -ldflags " ${ld_flags} " -o ./${PLUGIN} -windows-amd64.exe " ./$MAIN_SOURCE_DIR /..." || failed=1
287+
273288 return ${failed}
274289}
275290
@@ -334,9 +349,6 @@ if $(has_flag --debug); then
334349 set -x
335350fi
336351
337- # Global variables
338- source $( basedir) /hack/global_vars.sh
339-
340352# Shared funcs with CI
341353source $( basedir) /hack/build-flags.sh
342354
0 commit comments