Skip to content

Commit 1c208ef

Browse files
committed
feat: add 'version' subcommand printing ldflags (version, commit, build time)\n\nci: pin goreleaser-action to '~> v2' and migrate GoReleaser config to v2 to remove deprecations
1 parent ce1ab34 commit 1c208ef

File tree

4 files changed

+70
-36
lines changed

4 files changed

+70
-36
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ jobs:
2727
- name: Run GoReleaser
2828
uses: goreleaser/goreleaser-action@v6
2929
with:
30-
version: latest
30+
# Pin to v2 line to avoid 'latest will lock to ~> v2' warning
31+
version: "~> v2"
3132
args: release --clean --config deploy/.goreleaser.yaml
3233
env:
3334
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

deploy/.goreleaser.yaml

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
# Sources:
2-
# https://github.com/replicatedhq/outdated/blob/v0.3.3/deploy/.goreleaser.yaml
3-
project_name: kubectl-df-pv
1+
version: 2
2+
3+
project: kubectl-df-pv
4+
45
release:
56
github:
67
owner: yashbhutwala
78
name: kubectl-df-pv
9+
810
builds:
911
- id: df-pv
10-
goos:
11-
- linux
12-
- windows
13-
- darwin
14-
goarch:
15-
- arm64
16-
- amd64
17-
- "386"
18-
env:
19-
- CGO_ENABLED=0
2012
main: cmd/df-pv/main.go
21-
ldflags: -s -w
22-
-X github.com/yashbhutwala/kubectl-df-pv/pkg/version.version={{.Version}}
23-
-X github.com/yashbhutwala/kubectl-df-pv/pkg/version.gitSHA={{.Commit}}
24-
-X github.com/yashbhutwala/kubectl-df-pv/pkg/version.buildTime={{.Date}}
25-
-extldflags "-static"
26-
flags: -tags netgo -installsuffix netgo
2713
binary: df-pv
28-
hooks: {}
14+
env:
15+
- CGO_ENABLED=0
16+
goos:
17+
- linux
18+
- windows
19+
- darwin
20+
goarch:
21+
- amd64
22+
- arm64
23+
- "386"
24+
flags:
25+
- -tags=netgo
26+
- -installsuffix
27+
- netgo
28+
ldflags:
29+
- -s -w
30+
- -X github.com/yashbhutwala/kubectl-df-pv/pkg/version.version={{.Version}}
31+
- -X github.com/yashbhutwala/kubectl-df-pv/pkg/version.gitSHA={{.Commit}}
32+
- -X github.com/yashbhutwala/kubectl-df-pv/pkg/version.buildTime={{.Date}}
33+
- -extldflags "-static"
34+
35+
# Let GoReleaser choose defaults per-OS (tar.gz for unix, zip for windows),
36+
# while ensuring common docs are embedded in the archives.
2937
archives:
30-
- id: df-pv
31-
builds:
32-
- df-pv
33-
format: tar.gz
34-
format_overrides:
35-
- goos: windows
36-
format: zip
37-
# name_template: '{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{.Arm }}{{ end }}-{{ .Major }}.{{ .Minor }}.{{ .Patch }}'
38-
name_template: '{{ .ProjectName }}_{{ .Tag }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
38+
- name_template: '{{ .ProjectName }}_{{ .Tag }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
3939
files:
4040
- licence*
4141
- LICENCE*

pkg/df-pv/root.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/pkg/errors"
2020
log "github.com/sirupsen/logrus"
2121
"github.com/spf13/cobra"
22+
"github.com/yashbhutwala/kubectl-df-pv/pkg/version"
2223
corev1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/resource"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -74,6 +75,16 @@ It colors the values based on "severity" [red: > 75% (too high); yellow: < 25% (
7475
},
7576
}
7677

78+
// version subcommand
79+
var versionCmd = &cobra.Command{
80+
Use: "version",
81+
Short: "Print version information",
82+
Run: func(cmd *cobra.Command, args []string) {
83+
fmt.Println(version.Info())
84+
},
85+
}
86+
rootCmd.AddCommand(versionCmd)
87+
7788
rootCmd.PersistentFlags().StringVarP(&flags.logLevel, "verbosity", "v", "info", "log level; one of [info, debug, trace, warn, error, fatal, panic]")
7889
rootCmd.Flags().BoolVarP(&flags.disableColor, "disable-color", "d", false, "boolean flag for disabling colored output")
7990

pkg/version/version.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,34 @@ var (
88
buildTime = ""
99
)
1010

11-
// String returns a human-readable version string.
12-
func String() string {
13-
v := version
14-
if v == "" {
15-
v = "dev"
11+
// Version returns the version string (e.g. v0.4.0 or dev).
12+
func Version() string {
13+
if version == "" {
14+
return "dev"
1615
}
17-
return v
16+
return version
1817
}
1918

19+
// GitSHA returns the git commit SHA, if available.
20+
func GitSHA() string { return gitSHA }
21+
22+
// BuildTime returns the build time, if available.
23+
func BuildTime() string { return buildTime }
24+
25+
// String returns a human-readable version string.
26+
func String() string { return Version() }
27+
28+
// Info returns a multi-field version line suitable for CLI output.
29+
func Info() string {
30+
v := Version()
31+
if gitSHA == "" && buildTime == "" {
32+
return v
33+
}
34+
if gitSHA != "" && buildTime != "" {
35+
return v + " (" + gitSHA + ", " + buildTime + ")"
36+
}
37+
if gitSHA != "" {
38+
return v + " (" + gitSHA + ")"
39+
}
40+
return v + " (" + buildTime + ")"
41+
}

0 commit comments

Comments
 (0)