Skip to content

Commit 34e9024

Browse files
authored
add -V/--version flag to go implementations (#295)
Uses goreleaser to pass ldflags. Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
1 parent 3504e66 commit 34e9024

File tree

6 files changed

+51
-3
lines changed

6 files changed

+51
-3
lines changed

cmd/kubectx/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func parseArgs(argv []string) Op {
5252
if v == "--help" || v == "-h" {
5353
return HelpOp{}
5454
}
55+
if v == "--version" || v == "-V" {
56+
return VersionOp{}
57+
}
5558
if v == "--current" || v == "-c" {
5659
return CurrentOp{}
5760
}

cmd/kubectx/help.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ func printUsage(out io.Writer) error {
3939
%PROG% -c, --current : show the current context name
4040
%PROG% <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
4141
%PROG% <NEW_NAME>=. : rename current-context to <NEW_NAME>
42-
%PROG% -u, --unset : unset the current context
42+
%PROG% -u, --unset : unset the current context
4343
%PROG% -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
4444
%SPAC% (this command won't delete the user/cluster entry
4545
%SPAC% referenced by the context entry)
46-
%PROG% -h,--help : show this message`
46+
%PROG% -h,--help : show this message
47+
%PROG% -V,--version : show version`
4748
help = strings.ReplaceAll(help, "%PROG%", selfName())
4849
help = strings.ReplaceAll(help, "%SPAC%", strings.Repeat(" ", len(selfName())))
4950

cmd/kubectx/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
var (
11+
version = "v0.0.0+unknown" // populated by goreleaser
12+
)
13+
14+
// VersionOps describes printing version string.
15+
type VersionOp struct{}
16+
17+
func (_ VersionOp) Run(stdout, _ io.Writer) error {
18+
_, err := fmt.Fprintf(stdout, "%s\n", version)
19+
return errors.Wrap(err, "write error")
20+
}

cmd/kubens/flags.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ func parseArgs(argv []string) Op {
4545
if v == "--help" || v == "-h" {
4646
return HelpOp{}
4747
}
48+
if v == "--version" || v == "-V" {
49+
return VersionOp{}
50+
}
4851
if v == "--current" || v == "-c" {
4952
return CurrentOp{}
5053
}

cmd/kubens/help.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func printUsage(out io.Writer) error {
3838
%PROG% - : switch to the previous namespace in this context
3939
%PROG% -c, --current : show the current namespace
4040
%PROG% -h,--help : show this message
41-
`
41+
%PROG% -V,--version : show version`
42+
4243
// TODO this replace logic is duplicated between this and kubectx
4344
help = strings.ReplaceAll(help, "%PROG%", selfName())
4445

cmd/kubens/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/pkg/errors"
8+
)
9+
10+
var (
11+
version = "v0.0.0+unknown" // populated by goreleaser
12+
)
13+
14+
// VersionOps describes printing version string.
15+
type VersionOp struct{}
16+
17+
func (_ VersionOp) Run(stdout, _ io.Writer) error {
18+
_, err := fmt.Fprintf(stdout, "%s\n", version)
19+
return errors.Wrap(err, "write error")
20+
}

0 commit comments

Comments
 (0)