Skip to content

Commit 3b10a6e

Browse files
authored
feat: plugin version info (#10)
* add simple version info using ldflags * add showVersion flag for version pkg call * update ldflags for goreleaser * update docstring for Version struct * update flag description * add simple docstring for ToString receiver of Version struct
1 parent b76132c commit 3b10a6e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

.goreleaser.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ builds:
1717
- CGO_ENABLED=0
1818
- GO111MODULE=on
1919
main: cmd/plugin/main.go
20-
ldflags: -s -w
21-
-X github.com/jdockerty/kubectl-oomd/pkg/version.version=
20+
ldflags:
21+
- -s -w -X github.com/jdockerty/kubectl-oomd/pkg/version.version={{ .Version }}
22+
- -X github.com/jdockerty/kubectl-oomd/pkg/version.commit={{ .Commit }}
2223
archives:
2324
- id: oomd
2425
builds:

cmd/plugin/cli/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"text/tabwriter"
99

1010
"github.com/jdockerty/kubectl-oomd/pkg/plugin"
11+
"github.com/jdockerty/kubectl-oomd/pkg/version"
1112
"github.com/spf13/cobra"
1213
"github.com/spf13/viper"
1314
"k8s.io/cli-runtime/pkg/genericclioptions"
@@ -17,6 +18,7 @@ var (
1718
KubernetesConfigFlags *genericclioptions.ConfigFlags
1819
noHeaders bool
1920
allNamespaces bool
21+
showVersion bool
2022

2123
// When using the namespace provided by the `--namespace/-n` flag or current context.
2224
// This represents: Pod, Container, Request, Limit, and Termination Time
@@ -39,6 +41,12 @@ func RootCmd() *cobra.Command {
3941
},
4042
RunE: func(cmd *cobra.Command, args []string) error {
4143

44+
if showVersion {
45+
versionInfo := version.GetVersion()
46+
fmt.Printf("%s", versionInfo.ToString())
47+
return nil
48+
}
49+
4250
namespace := *KubernetesConfigFlags.Namespace
4351
t := tabwriter.NewWriter(os.Stdout, 10, 1, 5, ' ', 0) // Formatting for table output, similar to other kubectl commands.
4452

@@ -79,6 +87,7 @@ func RootCmd() *cobra.Command {
7987

8088
cmd.Flags().BoolVar(&noHeaders, "no-headers", false, "Don't print headers")
8189
cmd.Flags().BoolVarP(&allNamespaces, "all-namespaces", "A", false, "Show OOMKilled containers across all namespaces")
90+
cmd.Flags().BoolVarP(&showVersion, "version", "v", false, "Display version and build information")
8291
KubernetesConfigFlags = genericclioptions.NewConfigFlags(true)
8392
KubernetesConfigFlags.AddFlags(cmd.Flags())
8493

pkg/version/version.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
)
7+
8+
var (
9+
commit string
10+
version = "dev"
11+
platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
12+
13+
outputTemplate = `
14+
Version: %s
15+
Commit: %s
16+
Platform: %s
17+
Go: %s
18+
`
19+
)
20+
21+
// Version contains all of the information about the build of the current release.
22+
// It is expected that this is ran on tagged releases and populated by `-ldflags` of the `go build` commmand.
23+
// See the .gorelease.yml ldflags section for details.
24+
type Version struct {
25+
Commit string
26+
GoVersion string
27+
Platform string
28+
Version string
29+
}
30+
31+
// ToString populates the version/build information into the output template and returns it for use.
32+
func (v *Version) ToString() string {
33+
return fmt.Sprintf(outputTemplate, v.Version, v.Commit, v.Platform, v.GoVersion)
34+
}
35+
36+
// GetVersion is used to retrieve the current version information of the application.
37+
func GetVersion() *Version {
38+
return &Version{
39+
Commit: commit,
40+
GoVersion: runtime.Version(),
41+
Platform: platform,
42+
Version: version,
43+
}
44+
}

0 commit comments

Comments
 (0)