Skip to content

Commit

Permalink
Add -v to output version number
Browse files Browse the repository at this point in the history
  • Loading branch information
z0mbix committed Nov 16, 2021
1 parent d536a65 commit d63de52
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ builds:
- -a
ldflags:
- -extldflags "-static"
- -X github.com/yannh/kubeconform/pkg/config.version={{.Tag}}

archives:
- format: tar.gz
Expand Down Expand Up @@ -58,4 +59,4 @@ changelog:
sort: asc
filters:
exclude:
- '^test:'
- '^test:'
2 changes: 2 additions & 0 deletions cmd/kubeconform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func realMain() int {
cfg, out, err := config.FromFlags(os.Args[0], os.Args[1:])
if cfg.Help {
return 0
} else if cfg.Version {
return 0
} else if out != "" {
fmt.Println(out)
return 1
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
)

var version = "development"

type Config struct {
Cache string
CPUProfileFile string
Expand All @@ -26,6 +28,7 @@ type Config struct {
IgnoreMissingSchemas bool
IgnoreFilenamePatterns []string
Help bool
Version bool
}

type arrayParam []string
Expand Down Expand Up @@ -79,6 +82,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
flags.StringVar(&c.Cache, "cache", "", "cache schemas downloaded via HTTP to this folder")
flags.StringVar(&c.CPUProfileFile, "cpu-prof", "", "debug - log CPU profiling to file")
flags.BoolVar(&c.Help, "h", false, "show help information")
flags.BoolVar(&c.Version, "v", false, "show version information")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [OPTION]... [FILE OR FOLDER]...\n", progName)

Expand All @@ -98,5 +102,9 @@ func FromFlags(progName string, args []string) (Config, string, error) {
flags.Usage()
}

if c.Version {
fmt.Println(version)
}

return c, buf.String(), err
}
13 changes: 13 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func TestFromFlags(t *testing.T) {
RejectKinds: map[string]struct{}{},
},
},
{
[]string{"-v"},
Config{
Files: []string{},
Version: true,
KubernetesVersion: "master",
NumberOfWorkers: 4,
OutputFormat: "text",
SchemaLocations: nil,
SkipKinds: map[string]struct{}{},
RejectKinds: map[string]struct{}{},
},
},
{
[]string{"-skip", "a,b,c"},
Config{
Expand Down

0 comments on commit d63de52

Please sign in to comment.