diff --git a/cmd/argocd/commands/root.go b/cmd/argocd/commands/root.go index 8c397e8ee6cfa..91ffde5997b3a 100644 --- a/cmd/argocd/commands/root.go +++ b/cmd/argocd/commands/root.go @@ -41,7 +41,7 @@ func NewCommand() *cobra.Command { } command.AddCommand(NewCompletionCommand()) - command.AddCommand(initialize.InitCommand(NewVersionCmd(&clientOpts))) + command.AddCommand(initialize.InitCommand(NewVersionCmd(&clientOpts, nil))) command.AddCommand(initialize.InitCommand(NewClusterCommand(&clientOpts, pathOpts))) command.AddCommand(initialize.InitCommand(NewApplicationCommand(&clientOpts))) command.AddCommand(initialize.InitCommand(NewAppSetCommand(&clientOpts))) diff --git a/cmd/argocd/commands/version.go b/cmd/argocd/commands/version.go index 595599933622d..8f3d5b1abfe11 100644 --- a/cmd/argocd/commands/version.go +++ b/cmd/argocd/commands/version.go @@ -17,7 +17,7 @@ import ( ) // NewVersionCmd returns a new `version` command to be used as a sub-command to root -func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { +func NewVersionCmd(clientOpts *argocdclient.ClientOptions, serverVersion *version.VersionMessage) *cobra.Command { var ( short bool client bool @@ -54,7 +54,12 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { } if !client { - sv := getServerVersion(ctx, clientOpts, cmd) + var sv *version.VersionMessage + if serverVersion == nil { + sv = getServerVersion(ctx, clientOpts, cmd) + } else { + sv = serverVersion + } if short { v["server"] = map[string]string{"argocd-server": sv.Version} @@ -68,8 +73,13 @@ func NewVersionCmd(clientOpts *argocdclient.ClientOptions) *cobra.Command { case "wide", "short", "": fmt.Fprint(cmd.OutOrStdout(), printClientVersion(&cv, short || (output == "short"))) if !client { - sv := getServerVersion(ctx, clientOpts, cmd) - printServerVersion(sv, short || (output == "short")) + var sv *version.VersionMessage + if serverVersion == nil { + sv = getServerVersion(ctx, clientOpts, cmd) + } else { + sv = serverVersion + } + fmt.Fprint(cmd.OutOrStdout(), printServerVersion(sv, short || (output == "short"))) } default: log.Fatalf("unknown output format: %s", output) @@ -109,44 +119,45 @@ func printClientVersion(version *common.Version, short bool) string { return output } -func printServerVersion(version *version.VersionMessage, short bool) { - fmt.Printf("%s: %s\n", "argocd-server", version.Version) +func printServerVersion(version *version.VersionMessage, short bool) string { + output := fmt.Sprintf("%s: %s\n", "argocd-server", version.Version) if short { - return + return output } if version.BuildDate != "" { - fmt.Printf(" BuildDate: %s\n", version.BuildDate) + output += fmt.Sprintf(" BuildDate: %s\n", version.BuildDate) } if version.GitCommit != "" { - fmt.Printf(" GitCommit: %s\n", version.GitCommit) + output += fmt.Sprintf(" GitCommit: %s\n", version.GitCommit) } if version.GitTreeState != "" { - fmt.Printf(" GitTreeState: %s\n", version.GitTreeState) + output += fmt.Sprintf(" GitTreeState: %s\n", version.GitTreeState) } if version.GitTag != "" { - fmt.Printf(" GitTag: %s\n", version.GitTag) + output += fmt.Sprintf(" GitTag: %s\n", version.GitTag) } if version.GoVersion != "" { - fmt.Printf(" GoVersion: %s\n", version.GoVersion) + output += fmt.Sprintf(" GoVersion: %s\n", version.GoVersion) } if version.Compiler != "" { - fmt.Printf(" Compiler: %s\n", version.Compiler) + output += fmt.Sprintf(" Compiler: %s\n", version.Compiler) } if version.Platform != "" { - fmt.Printf(" Platform: %s\n", version.Platform) + output += fmt.Sprintf(" Platform: %s\n", version.Platform) } if version.KustomizeVersion != "" { - fmt.Printf(" Kustomize Version: %s\n", version.KustomizeVersion) + output += fmt.Sprintf(" Kustomize Version: %s\n", version.KustomizeVersion) } if version.HelmVersion != "" { - fmt.Printf(" Helm Version: %s\n", version.HelmVersion) + output += fmt.Sprintf(" Helm Version: %s\n", version.HelmVersion) } if version.KubectlVersion != "" { - fmt.Printf(" Kubectl Version: %s\n", version.KubectlVersion) + output += fmt.Sprintf(" Kubectl Version: %s\n", version.KubectlVersion) } if version.JsonnetVersion != "" { - fmt.Printf(" Jsonnet Version: %s\n", version.JsonnetVersion) + output += fmt.Sprintf(" Jsonnet Version: %s\n", version.JsonnetVersion) } + return output } diff --git a/cmd/argocd/commands/version_test.go b/cmd/argocd/commands/version_test.go index 26a2d549362f4..88aa689b48669 100644 --- a/cmd/argocd/commands/version_test.go +++ b/cmd/argocd/commands/version_test.go @@ -5,12 +5,13 @@ import ( "testing" argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient" + "github.com/argoproj/argo-cd/v2/pkg/apiclient/version" "github.com/stretchr/testify/assert" ) -func TestShortVersion(t *testing.T) { +func TestShortVersionClient(t *testing.T) { buf := new(bytes.Buffer) - cmd := NewVersionCmd(&argocdclient.ClientOptions{}) + cmd := NewVersionCmd(&argocdclient.ClientOptions{}, nil) cmd.SetOutput(buf) cmd.SetArgs([]string{"version", "--short", "--client"}) err := cmd.Execute() @@ -20,3 +21,17 @@ func TestShortVersion(t *testing.T) { output := buf.String() assert.Equal(t, output, "argocd: v99.99.99+unknown\n") } + +func TestShortVersion(t *testing.T) { + serverVersion := &version.VersionMessage{Version: "v99.99.99+unknown"} + buf := new(bytes.Buffer) + cmd := NewVersionCmd(&argocdclient.ClientOptions{}, serverVersion) + cmd.SetOutput(buf) + cmd.SetArgs([]string{"argocd", "version", "--short"}) + err := cmd.Execute() + if err != nil { + t.Fatal("Failed to execute short version command") + } + output := buf.String() + assert.Equal(t, output, "argocd: v99.99.99+unknown\nargocd-server: v99.99.99+unknown\n") +}