From 49f0189274ef26d251b148d0d062ae4d397f1439 Mon Sep 17 00:00:00 2001 From: mgoerens Date: Mon, 10 Jul 2023 14:24:39 +0200 Subject: [PATCH] Add commit ID to version output - use ldflags to injects the git commit ID at build time - add the commit ID to the version output - add the possibility to get the version and commit information in a JSON format close #362 Signed-off-by: mgoerens --- Makefile | 9 +++++++-- cmd/version.go | 30 +++++++++++++++++++++++++++- tests/tests/functional/chart_test.py | 9 +++++---- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 2d4f8a9a..0d785ea3 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ IMAGE_BUILDER?=podman IMAGE_REPO?=quay.io/redhat-certification COMMIT_ID=$(shell git rev-parse --short HEAD) +COMMIT_ID_LONG=$(shell git rev-parse HEAD) default: bin @@ -33,7 +34,9 @@ fmt: install.gofumpt .PHONY: bin bin: - CGO_ENABLED=0 go build -o ./out/chart-verifier main.go + CGO_ENABLED=0 go build \ + -ldflags "-X 'github.com/redhat-certification/chart-verifier/cmd.CommitIDLong=$(COMMIT_ID_LONG)'" \ + -o ./out/chart-verifier main.go .PHONY: lint lint: install.golangci-lint @@ -41,7 +44,9 @@ lint: install.golangci-lint .PHONY: bin_win bin_win: - env GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -o .\out\chart-verifier.exe main.go + env GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build \ + -ldflags "-X 'github.com/redhat-certification/chart-verifier/cmd.CommitIDLong=$(COMMIT_ID_LONG)'" \ + -o .\out\chart-verifier.exe main.go .PHONY: test test: diff --git a/cmd/version.go b/cmd/version.go index 5368c3f0..c4495c22 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -2,6 +2,7 @@ package cmd import ( _ "embed" + "encoding/json" "fmt" "io" "os" @@ -11,10 +12,27 @@ import ( apiversion "github.com/redhat-certification/chart-verifier/pkg/chartverifier/version" ) +// Print version and commit ID as json blob +var asData bool + func init() { rootCmd.AddCommand(NewVersionCmd()) } +// CommitIDLong contains the commit ID the binary was build on. It is populated at build time by ldflags. +// If you're running from a local debugger it will show an empty commit ID. +var CommitIDLong string = "unknown" + +type VersionContext struct { + Version string `json:"version"` + Commit string `json:"commit"` +} + +var Version = VersionContext{ + Version: apiversion.GetVersion(), + Commit: CommitIDLong, +} + func NewVersionCmd() *cobra.Command { cmd := &cobra.Command{ Use: "version", @@ -24,10 +42,20 @@ func NewVersionCmd() *cobra.Command { return runVersion(os.Stdout) }, } + cmd.Flags().BoolVar(&asData, "as-data", false, "output the version and commit ID information in JSON format") return cmd } func runVersion(out io.Writer) error { - fmt.Fprintf(out, "v%s\n", apiversion.GetVersion()) + if asData { + marshalledVersion, err := json.Marshal(Version) + if err != nil { + return err + } + fmt.Fprint(out, string(marshalledVersion)) + return nil + } + + fmt.Fprintf(out, "chart-verifier v%s \n", apiversion.GetVersion(), CommitIDLong) return nil } diff --git a/tests/tests/functional/chart_test.py b/tests/tests/functional/chart_test.py index 4e0e6d9d..3542b8ff 100644 --- a/tests/tests/functional/chart_test.py +++ b/tests/tests/functional/chart_test.py @@ -226,20 +226,21 @@ def run_report_docker_image(verifier_image_name,verifier_image_tag,profile_type, def run_version_tarball_image(tarball_name): tar = tarfile.open(tarball_name, "r:gz") tar.extractall(path="./test_verifier") - out = subprocess.run(["./test_verifier/chart-verifier","version"],capture_output=True) + out = subprocess.run(["./test_verifier/chart-verifier","version","--as-data"],capture_output=True) return normalize_version(out.stdout.decode("utf-8")) def normalize_version(version): - """Trim trailing newlines and leading v from semantic versions. + """Extract normalized version from JSON data Parameters: - version (string): a semver string like v0.0.0\n + version (string): the version and commit ID in JSON Returns: string: a normalized semver like 0.0.0. """ print(f'version input to normalize_version function is: {version}') - return version.rstrip().lstrip('v') + version_dict = json.loads(version) + return version_dict["version"] def run_version_docker_image(verifier_image_name,verifier_image_tag): """Run chart verifier's version command using the Docker image."""