11package main
22
33import (
4+ "encoding/json"
5+ "errors"
46 "fmt"
57 "os"
68 "strings"
79
810 "github.com/ClouGence/cloudcanal-openapi-cli/internal/app"
11+ "github.com/ClouGence/cloudcanal-openapi-cli/internal/buildinfo"
912 "github.com/ClouGence/cloudcanal-openapi-cli/internal/config"
1013 "github.com/ClouGence/cloudcanal-openapi-cli/internal/console"
1114 "github.com/ClouGence/cloudcanal-openapi-cli/internal/i18n"
@@ -61,6 +64,9 @@ func handleEarlyCommands(args []string) (bool, int) {
6164 fmt .Println (helpText )
6265 return true , 0
6366 }
67+ if handled , exitCode := handleVersionCommand (args ); handled {
68+ return true , exitCode
69+ }
6470
6571 if len (args ) == 0 {
6672 return false , 0
@@ -84,3 +90,112 @@ func handleEarlyCommands(args []string) (bool, int) {
8490 return false , 0
8591 }
8692}
93+
94+ func handleVersionCommand (args []string ) (bool , int ) {
95+ filtered , format , err := extractOutputFormat (args )
96+ if err != nil {
97+ fmt .Fprintln (os .Stderr , err .Error ())
98+ return true , 1
99+ }
100+ if len (filtered ) == 0 {
101+ return false , 0
102+ }
103+
104+ switch {
105+ case len (filtered ) == 1 && strings .EqualFold (filtered [0 ], "--version" ):
106+ return true , printVersion (format )
107+ case strings .EqualFold (filtered [0 ], "version" ):
108+ if len (filtered ) != 1 {
109+ fmt .Fprintln (os .Stderr , versionUsageText ())
110+ return true , 1
111+ }
112+ return true , printVersion (format )
113+ case containsVersionFlag (filtered ):
114+ fmt .Fprintln (os .Stderr , versionFlagErrorText ())
115+ return true , 1
116+ default :
117+ return false , 0
118+ }
119+ }
120+
121+ func printVersion (format string ) int {
122+ info := buildinfo .Current ()
123+ if format == "json" {
124+ data , err := json .MarshalIndent (info , "" , " " )
125+ if err != nil {
126+ fmt .Fprintln (os .Stderr , err .Error ())
127+ return 1
128+ }
129+ fmt .Println (string (data ))
130+ return 0
131+ }
132+
133+ fmt .Println ("version: " + info .Version )
134+ fmt .Println ("commit: " + info .Commit )
135+ fmt .Println ("buildTime: " + info .BuildTime )
136+ return 0
137+ }
138+
139+ func extractOutputFormat (args []string ) ([]string , string , error ) {
140+ format := "text"
141+ filtered := make ([]string , 0 , len (args ))
142+ seen := false
143+
144+ for i := 0 ; i < len (args ); i ++ {
145+ token := args [i ]
146+ switch {
147+ case token == "--output" :
148+ if i + 1 >= len (args ) {
149+ return nil , "" , errors .New (i18n .T ("parser.outputOptionRequiresValue" ))
150+ }
151+ if seen {
152+ return nil , "" , errors .New (i18n .T ("parser.duplicateOption" , "output" ))
153+ }
154+ value := strings .ToLower (strings .TrimSpace (args [i + 1 ]))
155+ if value != "text" && value != "json" {
156+ return nil , "" , errors .New (i18n .T ("parser.outputOptionInvalid" ))
157+ }
158+ format = value
159+ seen = true
160+ i ++
161+ case strings .HasPrefix (token , "--output=" ):
162+ if seen {
163+ return nil , "" , errors .New (i18n .T ("parser.duplicateOption" , "output" ))
164+ }
165+ _ , value , _ := strings .Cut (token , "=" )
166+ value = strings .ToLower (strings .TrimSpace (value ))
167+ if value != "text" && value != "json" {
168+ return nil , "" , errors .New (i18n .T ("parser.outputOptionInvalid" ))
169+ }
170+ format = value
171+ seen = true
172+ default :
173+ filtered = append (filtered , token )
174+ }
175+ }
176+
177+ return filtered , format , nil
178+ }
179+
180+ func containsVersionFlag (args []string ) bool {
181+ for _ , arg := range args {
182+ if strings .EqualFold (arg , "--version" ) {
183+ return true
184+ }
185+ }
186+ return false
187+ }
188+
189+ func versionUsageText () string {
190+ if i18n .CurrentLanguage () == i18n .Chinese {
191+ return "用法:version"
192+ }
193+ return "Usage: version"
194+ }
195+
196+ func versionFlagErrorText () string {
197+ if i18n .CurrentLanguage () == i18n .Chinese {
198+ return "--version 只能单独使用,或与 --output 一起使用"
199+ }
200+ return "--version can only be used by itself or with --output"
201+ }
0 commit comments