Feat/more version info (#224)

This commit is contained in:
Fabian Kammel 2022-06-21 15:12:27 +02:00 committed by GitHub
parent 3b92b52611
commit 0c9ca50be8
3 changed files with 96 additions and 4 deletions

View File

@ -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
)

View File

@ -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
}

View File

@ -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)
}