Skip to content

Commit d637e07

Browse files
authored
adding version info to plugin (#89)
1 parent 3b74378 commit d637e07

File tree

5 files changed

+53
-94
lines changed

5 files changed

+53
-94
lines changed

hack/README.md

Lines changed: 0 additions & 25 deletions
This file was deleted.

hack/build.sh

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
set -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

94109
codegen() {
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

110142
source_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

128149
go_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

133154
go_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

184205
update_deps() {
185206
echo "🚒 Update"
186-
go mod tidy
187-
go mod vendor
207+
$(basedir)/hack/update-deps.sh
188208
}
189209

190210
watch() {
@@ -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
335350
fi
336351

337-
# Global variables
338-
source $(basedir)/hack/global_vars.sh
339-
340352
# Shared funcs with CI
341353
source $(basedir)/hack/build-flags.sh
342354

hack/global_vars.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

hack/release.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
# Documentation about this script and how to use it can be found
18+
# at https://github.com/knative/hack
1719

18-
# Documentation about how the functions here are hooked into the release process
19-
# and how to use it can be found at https://github.com/knative/hack
20+
source $(dirname $0)/../vendor/knative.dev/hack/release.sh
2021

21-
source $(dirname "$0")/global_vars.sh
22-
source $(dirname "$0")/../vendor/knative.dev/hack/release.sh
23-
source $(dirname $0)/build-flags.sh
22+
PLUGIN="kn-quickstart"
2423

2524
function build_release() {
2625
export GO111MODULE=on

hack/tools.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
//go:build tools
12
// +build tools
23

34
/*
45
Copyright 2021 The Knative Authors
5-
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
88
You may obtain a copy of the License at
9-
109
http://www.apache.org/licenses/LICENSE-2.0
11-
1210
Unless required by applicable law or agreed to in writing, software
1311
distributed under the License is distributed on an "AS IS" BASIS,
1412
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,4 +16,7 @@ limitations under the License.
1816

1917
package tools
2018

21-
import _ "knative.dev/hack"
19+
// This package imports things required by this repository, to force `go mod` to see them as dependencies
20+
import (
21+
_ "knative.dev/hack"
22+
)

0 commit comments

Comments
 (0)