Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1612 | print installed version info (server version) in the output of everestctl version #932

Merged
merged 8 commits into from
Dec 25, 2024
56 changes: 50 additions & 6 deletions commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,78 @@ package commands

import (
"fmt"
"os"

"github.com/spf13/cobra"
"go.uber.org/zap"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/percona/everest/pkg/cli/utils"
"github.com/percona/everest/pkg/version"
)

func newVersionCmd(l *zap.SugaredLogger) *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "version",
Long: "Print version info",
Short: "Print version info",
Run: func(cmd *cobra.Command, args []string) { //nolint:revive
outputJSON, err := cmd.Flags().GetBool("json")
if err != nil {
l.Errorf("could not parse json global flag. Error: %s", err)
os.Exit(1)
return
}
if !outputJSON {
fmt.Println(version.FullVersionInfo()) //nolint:forbidigo
kubeConfigPath, err := cmd.Flags().GetString("kubeconfig")
if err != nil {
l.Errorf("could not parse kubeconfig global flag. Error: %s", err)
os.Exit(1)
return
}
version, err := version.FullVersionJSON()

clientOnly, err := cmd.Flags().GetBool("client-only")
if err != nil {
l.Errorf("could not print JSON. Error: %s", err)
l.Errorf("could not parse client-only flag. Error: %s", err)
os.Exit(1)
return
}
fmt.Println(version) //nolint:forbidigo

v := version.Info{
ProjectName: version.ProjectName,
Version: version.Version,
FullCommit: version.FullCommit,
}

if !clientOnly {
var err error
k, err := utils.NewKubeclient(l, kubeConfigPath)
if err != nil {
os.Exit(1)
return
}
ev, err := version.EverestVersionFromDeployment(cmd.Context(), k)
if client.IgnoreNotFound(err) != nil {
l.Error(err)
os.Exit(1)
}
sv := "[NOT INSTALLED]"
if ev != nil {
sv = fmt.Sprintf("v%s", ev.String())
}
v.ServerVersion = &sv
}

if !outputJSON {
fmt.Fprintln(os.Stdout, v)
return
}
fmt.Fprintln(os.Stdout, v.JSONString())
},
}
initVersionFlags(cmd)
return cmd
}

func initVersionFlags(cmd *cobra.Command) {
cmd.Flags().Bool("client-only", false, "Print client version only")
}
35 changes: 22 additions & 13 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,32 @@ func IsDev(v string) bool {
return ver.Core().Equal(devLatestVer)
}

// FullVersionInfo returns full version report.
func FullVersionInfo() string {
// Info represents the version information.
type Info struct {
ProjectName string `json:"projectName"`
Version string `json:"version"`
FullCommit string `json:"fullCommit"`
ServerVersion *string `json:"serverVersion,omitempty"`
}

// String returns the string representation of the version information.
func (v Info) String() string {
out := []string{
"ProjectName: " + ProjectName,
"Version: " + Version,
"FullCommit: " + FullCommit,
"ProjectName: " + v.ProjectName,
"Version: " + v.Version,
"FullCommit: " + v.FullCommit,
}
if v.ServerVersion != nil {
out = append(out, "ServerVersion: "+*v.ServerVersion)
}
return strings.Join(out, "\n")
}

// FullVersionJSON returns version info as JSON.
func FullVersionJSON() (string, error) {
res := map[string]string{
"projectName": ProjectName,
"version": Version,
"fullCommit": FullCommit,
// JSONString returns the JSON representation of the version information.
func (v Info) JSONString() string {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
data, err := json.Marshal(res)
return string(data), err
return string(data)
}
Loading