Skip to content

Commit 1f47404

Browse files
authored
internal/cmd: add version subcommand (#20)
1 parent df325e8 commit 1f47404

File tree

9 files changed

+141
-82
lines changed

9 files changed

+141
-82
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ jobs:
2121
run: make lint
2222

2323
- name: Run GoReleaser
24-
uses: goreleaser/goreleaser-action@v2
25-
with:
26-
version: latest
27-
args: release --snapshot --rm-dist
24+
run: make release
25+
env:
26+
RELEASE_ARGS: release --rm-dist --snapshot

.github/workflows/release.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ jobs:
1818
with:
1919
go-version: 1.14
2020

21-
- name: GoReleaser
22-
uses: goreleaser/goreleaser-action@v2
23-
with:
24-
version: latest
25-
args: release --rm-dist
21+
- name: Run GoReleaser
22+
run: make release
2623
env:
24+
RELEASE_ARGS: release --rm-dist
2725
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2826

2927
- name: Update new version in krew-index

.goreleaser.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ builds:
77
binary: kubectl-operator
88
env:
99
- CGO_ENABLED=0
10+
asmflags:
11+
- all=-trimpath={{ dir .Env.PWD }}
12+
gcflags:
13+
- all=-trimpath={{ dir .Env.PWD }}
14+
ldflags:
15+
- -s
16+
- -w
17+
- -X github.com/operator-framework/kubectl-operator/internal/version.GitVersion={{.Env.GIT_VERSION}}
18+
- -X github.com/operator-framework/kubectl-operator/internal/version.GitCommit={{.Env.GIT_COMMIT}}
19+
- -X github.com/operator-framework/kubectl-operator/internal/version.GitCommitTime={{.Env.GIT_COMMIT_TIME}}
20+
- -X github.com/operator-framework/kubectl-operator/internal/version.GitTreeState={{.Env.GIT_TREE_STATE}}
1021
goos:
1122
- linux
1223
- darwin

Makefile

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1-
.PHONY: all build gen-demo lint
2-
all: build
1+
SHELL:=/bin/bash
32

3+
export GIT_VERSION = $(shell git describe --tags --always)
4+
export GIT_COMMIT = $(shell git rev-parse HEAD)
5+
export GIT_COMMIT_TIME = $(shell TZ=UTC git show -s --format=%cd --date=format-local:%Y-%m-%dT%TZ)
6+
export GIT_TREE_STATE = $(shell sh -c '(test -n "$(shell git status -s)" && echo "dirty") || echo "clean"')
7+
8+
REPO = $(shell go list -m)
9+
GO_BUILD_ARGS = \
10+
-gcflags "all=-trimpath=$(shell dirname $(shell pwd))" \
11+
-asmflags "all=-trimpath=$(shell dirname $(shell pwd))" \
12+
-ldflags " \
13+
-s \
14+
-w \
15+
-X '$(REPO)/internal/version.GitVersion=$(GIT_VERSION)' \
16+
-X '$(REPO)/internal/version.GitCommit=$(GIT_COMMIT)' \
17+
-X '$(REPO)/internal/version.GitCommitTime=$(GIT_COMMIT_TIME)' \
18+
-X '$(REPO)/internal/version.GitTreeState=$(GIT_TREE_STATE)' \
19+
" \
20+
21+
.PHONY: all
22+
all: install
23+
24+
.PHONY: build
425
build:
5-
go build -o bin/kubectl-operator
26+
go build $(GO_BUILD_ARGS) -o bin/kubectl-operator
627

28+
.PHONY: install
729
install: build
830
install bin/kubectl-operator $(shell go env GOPATH)/bin
931

32+
.PHONY: gen-demo
1033
gen-demo:
1134
./assets/demo/gen-demo.sh
1235

13-
GOLANGCI_LINT_VER = "1.29.0"
36+
.PHONY: lint
1437
lint:
15-
# scripts/golangci-lint-check.sh
16-
ifneq (${GOLANGCI_LINT_VER}, "$(shell ./bin/golangci-lint --version 2>/dev/null | cut -b 27-32)")
17-
@echo "golangci-lint missing or not version '${GOLANGCI_LINT_VER}', downloading..."
18-
curl -sSfL "https://raw.githubusercontent.com/golangci/golangci-lint/v${GOLANGCI_LINT_VER}/install.sh" | sh -s -- -b ./bin "v${GOLANGCI_LINT_VER}"
19-
endif
20-
./bin/golangci-lint --timeout 3m run
38+
source ./scripts/fetch.sh; fetch golangci-lint 1.29.0 && ./bin/golangci-lint --timeout 3m run
39+
40+
.PHONY: release
41+
RELEASE_ARGS?=release --rm-dist --snapshot
42+
release:
43+
source ./scripts/fetch.sh; fetch goreleaser 0.141.0 && ./bin/goreleaser $(RELEASE_ARGS)

go.sum

Lines changed: 0 additions & 63 deletions
Large diffs are not rendered by default.

internal/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ operators from the installed catalogs.`,
4040
newOperatorUninstallCmd(&cfg),
4141
newOperatorListCmd(&cfg),
4242
newOperatorListAvailableCmd(&cfg),
43+
newVersionCmd(),
4344
)
4445

4546
return cmd

internal/cmd/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/operator-framework/kubectl-operator/internal/version"
9+
)
10+
11+
func newVersionCmd() *cobra.Command {
12+
cmd := &cobra.Command{
13+
Use: "version",
14+
Short: "Print version information",
15+
Run: func(_ *cobra.Command, _ []string) {
16+
fmt.Printf("%#v\n", version.Version)
17+
},
18+
}
19+
return cmd
20+
}

internal/version/version.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var (
9+
GitVersion = "unknown"
10+
GitCommit = "unknown"
11+
GitCommitTime = "unknown"
12+
GitTreeState = "unknown"
13+
GoVersion = runtime.Version()
14+
Compiler = runtime.Compiler
15+
Platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
16+
)
17+
18+
type Info struct {
19+
GitVersion string
20+
GitCommit string
21+
GitCommitTime string
22+
GitTreeState string
23+
GoVersion string
24+
Compiler string
25+
Platform string
26+
}
27+
28+
var Version Info
29+
30+
func init() {
31+
Version = Info{
32+
GitVersion: GitVersion,
33+
GitCommit: GitCommit,
34+
GitCommitTime: GitCommitTime,
35+
GitTreeState: GitTreeState,
36+
GoVersion: GoVersion,
37+
Compiler: Compiler,
38+
Platform: Platform,
39+
}
40+
}

scripts/fetch.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env bash
2+
3+
ROOT="$( git rev-parse --show-toplevel )"
4+
5+
fetch() {
6+
local tool=$1; shift
7+
local ver=$1; shift
8+
9+
local ver_cmd=""
10+
local tool_fetch_cmd=""
11+
case "$tool" in
12+
"golangci-lint")
13+
ver_cmd="${ROOT}/bin/golangci-lint --version 2>/dev/null | cut -d\" \" -f4"
14+
fetch_cmd="curl -sSfL \"https://raw.githubusercontent.com/golangci/golangci-lint/v${ver}/install.sh\" | sh -s -- -b \"${ROOT}/bin\" \"v${ver}\""
15+
;;
16+
"goreleaser")
17+
ver_cmd="${ROOT}/bin/goreleaser --version 2>/dev/null | grep version | cut -d' ' -f3"
18+
fetch_cmd="curl -sSfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | sh -s -- -b \"${ROOT}/bin\" -d \"v${ver}\""
19+
;;
20+
*)
21+
echo "unknown tool $tool"
22+
return 1
23+
;;
24+
esac
25+
26+
if [[ "${ver}" != "$(eval ${ver_cmd})" ]]; then
27+
echo "${tool} missing or not version '${ver}', downloading..."
28+
eval ${fetch_cmd}
29+
fi
30+
}

0 commit comments

Comments
 (0)