2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-21 09:12:27 -04:00
|
|
|
"runtime/debug"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-07-08 04:59:59 -04:00
|
|
|
// NewVersionCmd returns a new cobra.Command for the verify command.
|
2022-06-08 02:14:28 -04:00
|
|
|
func NewVersionCmd() *cobra.Command {
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Display version of this CLI",
|
2022-05-06 11:51:41 -04:00
|
|
|
Long: "Display version of this CLI.",
|
2022-03-22 11:03:15 -04:00
|
|
|
Args: cobra.NoArgs,
|
2022-06-21 09:12:27 -04:00
|
|
|
Run: runVersion,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
2022-06-21 09:12:27 -04:00
|
|
|
|
|
|
|
func runVersion(cmd *cobra.Command, args []string) {
|
|
|
|
buildInfo, ok := debug.ReadBuildInfo()
|
|
|
|
if !ok {
|
2022-11-10 04:27:24 -05:00
|
|
|
cmd.PrintErrf("Unable to retrieve build info. Is buildvcs enabled?")
|
2022-06-21 09:12:27 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, state, date, goVersion, compiler, platform := parseBuildInfo(buildInfo)
|
|
|
|
|
2023-02-14 08:30:10 -05:00
|
|
|
cmd.Printf("Version:\t%s (%s)\n", constants.VersionInfo, constants.VersionBuild)
|
2022-06-21 09:12:27 -04:00
|
|
|
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
|
|
|
|
}
|