From 0c9ca50be8c88a3b1de931506b6377f3a14e3e7e Mon Sep 17 00:00:00 2001 From: Fabian Kammel Date: Tue, 21 Jun 2022 15:12:27 +0200 Subject: [PATCH] Feat/more version info (#224) --- CMakeLists.txt | 2 +- cli/internal/cmd/version.go | 55 ++++++++++++++++++++++++++++++-- cli/internal/cmd/version_test.go | 43 +++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63d352428..18c75a20e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,7 @@ add_custom_target(coordinator ALL # add_custom_target(cli ALL - CGO_ENABLED=0 go build -o ${CMAKE_BINARY_DIR}/constellation -buildvcs=false -tags=gcp -ldflags "-buildid='' -X github.com/edgelesssys/constellation/internal/constants.VersionInfo=${PROJECT_VERSION}" + CGO_ENABLED=0 go build -o ${CMAKE_BINARY_DIR}/constellation -tags=gcp -ldflags "-buildid='' -X github.com/edgelesssys/constellation/internal/constants.VersionInfo=${PROJECT_VERSION}" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/cli BYPRODUCTS constellation ) diff --git a/cli/internal/cmd/version.go b/cli/internal/cmd/version.go index c38ae757c..3cf32b73b 100644 --- a/cli/internal/cmd/version.go +++ b/cli/internal/cmd/version.go @@ -1,6 +1,8 @@ package cmd import ( + "runtime/debug" + "github.com/edgelesssys/constellation/internal/constants" "github.com/spf13/cobra" ) @@ -12,9 +14,56 @@ func NewVersionCmd() *cobra.Command { Short: "Display version of this CLI", Long: "Display version of this CLI.", Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - cmd.Printf("CLI Version: v%s \n", constants.VersionInfo) - }, + Run: runVersion, } return cmd } + +func runVersion(cmd *cobra.Command, args []string) { + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + cmd.Printf("Unable to retrieve build info. Is buildvcs enabled?") + return + } + + commit, state, date, goVersion, compiler, platform := parseBuildInfo(buildInfo) + + cmd.Printf("Version:\t%s\n", constants.VersionInfo) + cmd.Printf("GitCommit:\t%s\n", commit) + cmd.Printf("GitTreeState:\t%s\n", state) + cmd.Printf("BuildDate:\t%s\n", date) + cmd.Printf("GoVersion:\t%s\n", goVersion) + cmd.Printf("Compiler:\t%s\n", compiler) + cmd.Printf("Platform:\t%s\n", platform) +} + +func parseBuildInfo(info *debug.BuildInfo) (commit, state, date, goVersion, compiler, platform string) { + var arch, os string + for idx := range info.Settings { + key := info.Settings[idx].Key + value := info.Settings[idx].Value + + switch key { + case "-compiler": + compiler = value + case "GOARCH": + arch = value + case "GOOS": + os = value + case "vcs.time": + date = value + case "vcs.modified": + if value == "true" { + state = "dirty" + } else { + state = "clean" + } + case "vcs.revision": + commit = value + } + } + + platform = os + "/" + arch + goVersion = info.GoVersion + return commit, state, date, goVersion, compiler, platform +} diff --git a/cli/internal/cmd/version_test.go b/cli/internal/cmd/version_test.go index 00a3def91..20c896905 100644 --- a/cli/internal/cmd/version_test.go +++ b/cli/internal/cmd/version_test.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "io" + "runtime/debug" "testing" "github.com/edgelesssys/constellation/internal/constants" @@ -23,3 +24,45 @@ func TestVersionCmd(t *testing.T) { assert.NoError(err) assert.Contains(string(s), constants.VersionInfo) } + +func TestParseBuildInfo(t *testing.T) { + assert := assert.New(t) + info := debug.BuildInfo{ + GoVersion: "go1.18.3", + Settings: []debug.BuildSetting{ + { + Key: "-compiler", + Value: "gc", + }, + { + Key: "GOARCH", + Value: "amd64", + }, + { + Key: "GOOS", + Value: "linux", + }, + { + Key: "vcs.time", + Value: "2022-06-20T11:57:25Z", + }, + { + Key: "vcs.modified", + Value: "true", + }, + { + Key: "vcs.revision", + Value: "abcdef", + }, + }, + } + + commit, state, date, goVersion, compiler, platform := parseBuildInfo(&info) + + assert.Equal("abcdef", commit) + assert.Equal("dirty", state) + assert.Equal("2022-06-20T11:57:25Z", date) + assert.Equal("go1.18.3", goVersion) + assert.Equal("gc", compiler) + assert.Equal("linux/amd64", platform) +}