github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd/commands/version.go (about) 1 package commands 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/golang/protobuf/ptypes/empty" 8 log "github.com/sirupsen/logrus" 9 "github.com/spf13/cobra" 10 11 "github.com/argoproj/argo-cd/v3/cmd/argocd/commands/headless" 12 "github.com/argoproj/argo-cd/v3/common" 13 argocdclient "github.com/argoproj/argo-cd/v3/pkg/apiclient" 14 "github.com/argoproj/argo-cd/v3/pkg/apiclient/version" 15 "github.com/argoproj/argo-cd/v3/util/errors" 16 utilio "github.com/argoproj/argo-cd/v3/util/io" 17 ) 18 19 // NewVersionCmd returns a new `version` command to be used as a sub-command to root 20 func NewVersionCmd(clientOpts *argocdclient.ClientOptions, serverVersion *version.VersionMessage) *cobra.Command { 21 var ( 22 short bool 23 client bool 24 output string 25 ) 26 27 versionCmd := cobra.Command{ 28 Use: "version", 29 Short: "Print version information", 30 Example: ` # Print the full version of client and server to stdout 31 argocd version 32 33 # Print only full version of the client - no connection to server will be made 34 argocd version --client 35 36 # Print the full version of client and server in JSON format 37 argocd version -o json 38 39 # Print only client and server core version strings in YAML format 40 argocd version --short -o yaml 41 `, 42 Run: func(cmd *cobra.Command, _ []string) { 43 ctx := cmd.Context() 44 45 cv := common.GetVersion() 46 switch output { 47 case "yaml", "json": 48 v := make(map[string]any) 49 50 if short { 51 v["client"] = map[string]string{cliName: cv.Version} 52 } else { 53 v["client"] = cv 54 } 55 56 if !client { 57 var sv *version.VersionMessage 58 if serverVersion == nil { 59 sv = getServerVersion(ctx, clientOpts, cmd) 60 } else { 61 sv = serverVersion 62 } 63 64 if short { 65 v["server"] = map[string]string{"argocd-server": sv.Version} 66 } else { 67 v["server"] = sv 68 } 69 } 70 71 err := PrintResource(v, output) 72 errors.CheckError(err) 73 case "wide", "short", "": 74 fmt.Fprint(cmd.OutOrStdout(), printClientVersion(&cv, short || (output == "short"))) 75 if !client { 76 var sv *version.VersionMessage 77 if serverVersion == nil { 78 sv = getServerVersion(ctx, clientOpts, cmd) 79 } else { 80 sv = serverVersion 81 } 82 fmt.Fprint(cmd.OutOrStdout(), printServerVersion(sv, short || (output == "short"))) 83 } 84 default: 85 log.Fatalf("unknown output format: %s", output) 86 } 87 }, 88 } 89 versionCmd.Flags().StringVarP(&output, "output", "o", "wide", "Output format. One of: json|yaml|wide|short") 90 versionCmd.Flags().BoolVar(&short, "short", false, "print just the version number") 91 versionCmd.Flags().BoolVar(&client, "client", false, "client version only (no server required)") 92 return &versionCmd 93 } 94 95 func getServerVersion(ctx context.Context, options *argocdclient.ClientOptions, c *cobra.Command) *version.VersionMessage { 96 conn, versionIf := headless.NewClientOrDie(options, c).NewVersionClientOrDie() 97 defer utilio.Close(conn) 98 99 v, err := versionIf.Version(ctx, &empty.Empty{}) 100 errors.CheckError(err) 101 102 return v 103 } 104 105 func printClientVersion(version *common.Version, short bool) string { 106 output := fmt.Sprintf("%s: %s\n", cliName, version) 107 if short { 108 return output 109 } 110 output += fmt.Sprintf(" BuildDate: %s\n", version.BuildDate) 111 output += fmt.Sprintf(" GitCommit: %s\n", version.GitCommit) 112 output += fmt.Sprintf(" GitTreeState: %s\n", version.GitTreeState) 113 if version.GitTag != "" { 114 output += fmt.Sprintf(" GitTag: %s\n", version.GitTag) 115 } 116 output += fmt.Sprintf(" GoVersion: %s\n", version.GoVersion) 117 output += fmt.Sprintf(" Compiler: %s\n", version.Compiler) 118 output += fmt.Sprintf(" Platform: %s\n", version.Platform) 119 if version.ExtraBuildInfo != "" { 120 output += fmt.Sprintf(" ExtraBuildInfo: %s\n", version.ExtraBuildInfo) 121 } 122 return output 123 } 124 125 func printServerVersion(version *version.VersionMessage, short bool) string { 126 output := fmt.Sprintf("%s: %s\n", "argocd-server", version.Version) 127 128 if short { 129 return output 130 } 131 132 if version.BuildDate != "" { 133 output += fmt.Sprintf(" BuildDate: %s\n", version.BuildDate) 134 } 135 if version.GitCommit != "" { 136 output += fmt.Sprintf(" GitCommit: %s\n", version.GitCommit) 137 } 138 if version.GitTreeState != "" { 139 output += fmt.Sprintf(" GitTreeState: %s\n", version.GitTreeState) 140 } 141 if version.GitTag != "" { 142 output += fmt.Sprintf(" GitTag: %s\n", version.GitTag) 143 } 144 if version.GoVersion != "" { 145 output += fmt.Sprintf(" GoVersion: %s\n", version.GoVersion) 146 } 147 if version.Compiler != "" { 148 output += fmt.Sprintf(" Compiler: %s\n", version.Compiler) 149 } 150 if version.Platform != "" { 151 output += fmt.Sprintf(" Platform: %s\n", version.Platform) 152 } 153 if version.ExtraBuildInfo != "" { 154 output += fmt.Sprintf(" ExtraBuildInfo: %s\n", version.ExtraBuildInfo) 155 } 156 if version.KustomizeVersion != "" { 157 output += fmt.Sprintf(" Kustomize Version: %s\n", version.KustomizeVersion) 158 } 159 if version.HelmVersion != "" { 160 output += fmt.Sprintf(" Helm Version: %s\n", version.HelmVersion) 161 } 162 if version.KubectlVersion != "" { 163 output += fmt.Sprintf(" Kubectl Version: %s\n", version.KubectlVersion) 164 } 165 if version.JsonnetVersion != "" { 166 output += fmt.Sprintf(" Jsonnet Version: %s\n", version.JsonnetVersion) 167 } 168 return output 169 }