From ea1fdd60f52370fb5dfd0b189d64ae4061945ab0 Mon Sep 17 00:00:00 2001 From: Mahe Tardy Date: Thu, 10 Aug 2023 14:58:49 +0000 Subject: [PATCH] tetra: version cmd default to CLI and add build info Version commands are usually expected to return quickly and can be used to see if the CLI is well installed and works, I think it's best to put the server version behind a flag. We could also adjust the timeout for this command in order to return quickly. Signed-off-by: Mahe Tardy --- cmd/tetra/version/version.go | 66 ++++++++++++++++++++---------------- pkg/version/version.go | 57 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 30 deletions(-) diff --git a/cmd/tetra/version/version.go b/cmd/tetra/version/version.go index fcdd26f80fe..d90520396ac 100644 --- a/cmd/tetra/version/version.go +++ b/cmd/tetra/version/version.go @@ -20,49 +20,55 @@ import ( "github.com/cilium/tetragon/api/v1/tetragon" "github.com/cilium/tetragon/cmd/tetra/common" + "github.com/cilium/tetragon/pkg/logger" "github.com/cilium/tetragon/pkg/version" "github.com/spf13/cobra" - "github.com/spf13/viper" ) -func printClientersion() { - fmt.Printf("cli version: %s\n", version.Version) -} +const examples = ` # Retrieve version from server + tetra version --server -func printVersion(res *tetragon.GetVersionResponse, err error) { - if err == nil { - fmt.Printf("server version: %s\n", res.Version) - printClientersion() - } else { - fmt.Printf("error getting server version: %s\n", err) - printClientersion() - } -} + # Get build info for the CLI + tetra version --build` + +var ( + server bool + build bool +) func New() *cobra.Command { cmd := &cobra.Command{ - Use: "version", - Short: "Print version", - Args: cobra.NoArgs, + Use: "version", + Short: "Print version from CLI and server", + Example: examples, + Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - if viper.GetBool("client") { - printClientersion() - return + fmt.Printf("CLI version: %s\n", version.Version) + + if server { + common.CliRunErr( + func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) { + res, err := cli.GetVersion(ctx, &tetragon.GetVersionRequest{}) + if err != nil { + logger.GetLogger().WithError(err).Error("error retrieving server version") + } + fmt.Printf("Server version: %s\n", res.Version) + }, + func(err error) { + logger.GetLogger().WithError(err).Error("error retrieving server version") + }, + ) + } + + if build { + info := version.ReadBuildInfo() + info.Print() } - common.CliRunErr( - func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) { - res, err := cli.GetVersion(ctx, &tetragon.GetVersionRequest{}) - printVersion(res, err) - }, - func(err error) { - printVersion(nil, err) - }, - ) }, } flags := cmd.Flags() - flags.Bool("client", false, "Only print client version without attempting to connect to server") - viper.BindPFlags(flags) + flags.BoolVarP(&server, "server", "s", false, "Connect and retrieve version from the server") + flags.BoolVarP(&build, "build", "b", false, "Show CLI build information") return cmd } diff --git a/pkg/version/version.go b/pkg/version/version.go index f7873546307..bc537de249e 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -3,4 +3,61 @@ package version +import ( + "fmt" + "runtime/debug" +) + var Version string + +type BuildInfo struct { + GoVersion string + Commit string + Time string + Modified string +} + +func ReadBuildInfo() *BuildInfo { + info := &BuildInfo{} + buildInfo, ok := debug.ReadBuildInfo() + if ok { + info.GoVersion = buildInfo.GoVersion + // unfortunately, it's not a Go map + for _, s := range buildInfo.Settings { + if s.Key == "vcs.revision" { + info.Commit = s.Value + continue + } + if s.Key == "vcs.time" { + info.Time = s.Value + continue + } + if s.Key == "vcs.modified" { + info.Modified = s.Value + continue + } + } + } + return info +} + +func (info BuildInfo) Print() { + if info.GoVersion != "" { + fmt.Printf("GoVersion: %s\n", info.GoVersion) + } + if info.Time != "" { + fmt.Printf("Date: %s\n", info.Time) + } + if info.Commit != "" { + fmt.Printf("GitCommit: %s\n", info.Commit) + } + if info.Modified != "" { + var state string + if info.Modified == "true" { + state = "dirty" + } else { + state = "clean" + } + fmt.Printf("GitTreeState: %s\n", state) + } +}