diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 4eb4847c..461ad9a8 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -13,7 +13,7 @@ builds: flags: - -trimpath ldflags: - - -s -w + - -s -w -X github.com/kyverno/kyverno-json/pkg/version.BuildVersion={{ .Version }} signs: - cmd: cosign diff --git a/Makefile b/Makefile index 3c5fd869..e15a22e2 100644 --- a/Makefile +++ b/Makefile @@ -53,7 +53,11 @@ CLI_DIR := $(CMD_DIR)/cli CLI_BIN := kyverno-json CGO_ENABLED ?= 0 GOOS ?= $(shell go env GOOS) -LD_FLAGS ?= "-s -w" +ifdef VERSION +LD_FLAGS := "-s -w -X $(PACKAGE)/pkg/version.BuildVersion=$(VERSION)" +else +LD_FLAGS := "-s -w" +endif .PHONY: fmt fmt: ## Run go fmt diff --git a/docs/user/commands/kyverno-json.md b/docs/user/commands/kyverno-json.md index a84a45e8..9f71b1ee 100644 --- a/docs/user/commands/kyverno-json.md +++ b/docs/user/commands/kyverno-json.md @@ -22,4 +22,5 @@ kyverno-json [flags] * [kyverno-json docs](kyverno-json_docs.md) - Generates reference documentation. * [kyverno-json jp](kyverno-json_jp.md) - Provides a command-line interface to JMESPath, enhanced with custom functions. * [kyverno-json scan](kyverno-json_scan.md) - scan +* [kyverno-json version](kyverno-json_version.md) - Prints the version informations. diff --git a/docs/user/commands/kyverno-json_version.md b/docs/user/commands/kyverno-json_version.md new file mode 100644 index 00000000..15e7c109 --- /dev/null +++ b/docs/user/commands/kyverno-json_version.md @@ -0,0 +1,31 @@ +## kyverno-json version + +Prints the version informations. + +### Synopsis + +Prints the version informations. + + +``` +kyverno-json version [flags] +``` + +### Examples + +``` + # Print version infos + kyverno-json version + +``` + +### Options + +``` + -h, --help help for version +``` + +### SEE ALSO + +* [kyverno-json](kyverno-json.md) - kyverno-json + diff --git a/pkg/commands/root_test.go b/pkg/commands/root_test.go index 1593147d..078cb3fc 100644 --- a/pkg/commands/root_test.go +++ b/pkg/commands/root_test.go @@ -12,7 +12,7 @@ import ( func TestRootCommand(t *testing.T) { cmd := RootCommand() assert.NotNil(t, cmd) - assert.Len(t, cmd.Commands(), 3) + assert.Len(t, cmd.Commands(), 4) err := cmd.Execute() assert.NoError(t, err) } diff --git a/pkg/commands/version/command.go b/pkg/commands/version/command.go index 4304131a..95325f86 100644 --- a/pkg/commands/version/command.go +++ b/pkg/commands/version/command.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/kyverno/kyverno-json/pkg/command" - "github.com/kyverno/kyverno/pkg/version" + "github.com/kyverno/kyverno-json/pkg/version" "github.com/spf13/cobra" ) diff --git a/pkg/commands/version/command_test.go b/pkg/commands/version/command_test.go index 25ec3f9c..0fadf041 100644 --- a/pkg/commands/version/command_test.go +++ b/pkg/commands/version/command_test.go @@ -6,7 +6,7 @@ import ( "strings" "testing" - "github.com/kyverno/kyverno/pkg/version" + "github.com/kyverno/kyverno-json/pkg/version" "github.com/stretchr/testify/assert" ) diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 00000000..5a6f5596 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,43 @@ +package version + +import ( + "runtime/debug" +) + +// BuildVersion is provided by govvv at compile-time +var BuildVersion string + +func Version() string { + if BuildVersion == "" { + bi, ok := debug.ReadBuildInfo() + if !ok { + return "---" + } + BuildVersion = bi.Main.Version + } + return BuildVersion +} + +func Time() string { + bi, ok := debug.ReadBuildInfo() + if ok { + for _, setting := range bi.Settings { + if setting.Key == "vcs.time" { + return setting.Value + } + } + } + return "---" +} + +func Hash() string { + bi, ok := debug.ReadBuildInfo() + if ok { + for _, setting := range bi.Settings { + if setting.Key == "vcs.revision" { + return setting.Value + } + } + } + return "---" +} diff --git a/website/docs/commands/kyverno-json.md b/website/docs/commands/kyverno-json.md index a84a45e8..9f71b1ee 100644 --- a/website/docs/commands/kyverno-json.md +++ b/website/docs/commands/kyverno-json.md @@ -22,4 +22,5 @@ kyverno-json [flags] * [kyverno-json docs](kyverno-json_docs.md) - Generates reference documentation. * [kyverno-json jp](kyverno-json_jp.md) - Provides a command-line interface to JMESPath, enhanced with custom functions. * [kyverno-json scan](kyverno-json_scan.md) - scan +* [kyverno-json version](kyverno-json_version.md) - Prints the version informations. diff --git a/website/docs/commands/kyverno-json_version.md b/website/docs/commands/kyverno-json_version.md new file mode 100644 index 00000000..15e7c109 --- /dev/null +++ b/website/docs/commands/kyverno-json_version.md @@ -0,0 +1,31 @@ +## kyverno-json version + +Prints the version informations. + +### Synopsis + +Prints the version informations. + + +``` +kyverno-json version [flags] +``` + +### Examples + +``` + # Print version infos + kyverno-json version + +``` + +### Options + +``` + -h, --help help for version +``` + +### SEE ALSO + +* [kyverno-json](kyverno-json.md) - kyverno-json +