@@ -23,9 +23,11 @@ import (
2323 "github.com/cortexlabs/cortex/cli/cluster"
2424 "github.com/cortexlabs/cortex/cli/local"
2525 "github.com/cortexlabs/cortex/cli/types/cliconfig"
26+ "github.com/cortexlabs/cortex/cli/types/flags"
2627 "github.com/cortexlabs/cortex/pkg/lib/console"
2728 "github.com/cortexlabs/cortex/pkg/lib/errors"
2829 "github.com/cortexlabs/cortex/pkg/lib/exit"
30+ libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2931 "github.com/cortexlabs/cortex/pkg/lib/sets/strset"
3032 s "github.com/cortexlabs/cortex/pkg/lib/strings"
3133 "github.com/cortexlabs/cortex/pkg/lib/table"
@@ -59,6 +61,7 @@ func getInit() {
5961 _getCmd .Flags ().SortFlags = false
6062 _getCmd .Flags ().StringVarP (& _flagGetEnv , "env" , "e" , getDefaultEnv (_generalCommandType ), "environment to use" )
6163 _getCmd .Flags ().BoolVarP (& _flagWatch , "watch" , "w" , false , "re-run the command every 2 seconds" )
64+ _getCmd .Flags ().VarP (& _flagOutput , "output" , "o" , fmt .Sprintf ("output format: one of %s" , strings .Join (flags .OutputTypeStrings (), "|" )))
6265}
6366
6467var _getCmd = & cobra.Command {
@@ -93,6 +96,11 @@ var _getCmd = &cobra.Command{
9396 if err != nil {
9497 return "" , err
9598 }
99+
100+ if _flagOutput == flags .JSONOutputType {
101+ return apiTable , nil
102+ }
103+
96104 return out + apiTable , nil
97105 } else if len (args ) == 2 {
98106 env , err := ReadOrConfigureEnv (_flagGetEnv )
@@ -109,11 +117,15 @@ var _getCmd = &cobra.Command{
109117 return "" , errors .Wrap (ErrorNotSupportedInLocalEnvironment (), fmt .Sprintf ("cannot get status of job %s for api %s" , args [1 ], args [0 ]))
110118 }
111119
112- apiTable , err := getJob (env , args [0 ], args [1 ])
120+ jobTable , err := getJob (env , args [0 ], args [1 ])
113121 if err != nil {
114122 return "" , err
115123 }
116- return out + apiTable , nil
124+ if _flagOutput == flags .JSONOutputType {
125+ return jobTable , nil
126+ }
127+
128+ return out + jobTable , nil
117129 } else {
118130 if wasEnvFlagProvided (cmd ) {
119131 env , err := ReadOrConfigureEnv (_flagGetEnv )
@@ -130,6 +142,11 @@ var _getCmd = &cobra.Command{
130142 if err != nil {
131143 return "" , err
132144 }
145+
146+ if _flagOutput == flags .JSONOutputType {
147+ return apiTable , nil
148+ }
149+
133150 return out + apiTable , nil
134151 }
135152
@@ -157,6 +174,14 @@ func getAPIsInAllEnvironments() (string, error) {
157174 var allTrafficSplitters []schema.TrafficSplitter
158175 var allTrafficSplitterEnvs []string
159176
177+ type getAPIsOutput struct {
178+ EnvName string `json:"env_name"`
179+ Response schema.GetAPIsResponse `json:"response"`
180+ Error string `json:"error"`
181+ }
182+
183+ allAPIsOutput := []getAPIsOutput {}
184+
160185 errorsMap := map [string ]error {}
161186 // get apis from both environments
162187 for _ , env := range cliConfig .Environments {
@@ -168,6 +193,11 @@ func getAPIsInAllEnvironments() (string, error) {
168193 apisRes , err = local .GetAPIs ()
169194 }
170195
196+ apisOutput := getAPIsOutput {
197+ EnvName : env .Name ,
198+ Response : apisRes ,
199+ }
200+
171201 if err == nil {
172202 for range apisRes .BatchAPIs {
173203 allBatchAPIEnvs = append (allBatchAPIEnvs , env .Name )
@@ -182,8 +212,20 @@ func getAPIsInAllEnvironments() (string, error) {
182212 allBatchAPIs = append (allBatchAPIs , apisRes .BatchAPIs ... )
183213 allTrafficSplitters = append (allTrafficSplitters , apisRes .TrafficSplitters ... )
184214 } else {
215+ apisOutput .Error = err .Error ()
185216 errorsMap [env .Name ] = err
186217 }
218+
219+ allAPIsOutput = append (allAPIsOutput , apisOutput )
220+ }
221+
222+ if _flagOutput == flags .JSONOutputType {
223+ bytes , err := libjson .Marshal (allAPIsOutput )
224+ if err != nil {
225+ return "" , err
226+ }
227+
228+ return string (bytes ), nil
187229 }
188230
189231 out := ""
@@ -278,11 +320,27 @@ func getAPIsByEnv(env cliconfig.Environment, printEnv bool) (string, error) {
278320 if err != nil {
279321 return "" , err
280322 }
323+
324+ if _flagOutput == flags .JSONOutputType {
325+ bytes , err := libjson .Marshal (apisRes )
326+ if err != nil {
327+ return "" , err
328+ }
329+ return string (bytes ), nil
330+ }
281331 } else {
282332 apisRes , err = local .GetAPIs ()
283333 if err != nil {
284334 return "" , err
285335 }
336+
337+ if _flagOutput == flags .JSONOutputType {
338+ bytes , err := libjson .Marshal (apisRes )
339+ if err != nil {
340+ return "" , err
341+ }
342+ return string (bytes ), nil
343+ }
286344 }
287345
288346 if len (apisRes .RealtimeAPIs ) == 0 && len (apisRes .BatchAPIs ) == 0 && len (apisRes .TrafficSplitters ) == 0 {
@@ -372,13 +430,17 @@ func getAPI(env cliconfig.Environment, apiName string) (string, error) {
372430 if env .Provider == types .AWSProviderType {
373431 apiRes , err := cluster .GetAPI (MustGetOperatorConfig (env .Name ), apiName )
374432 if err != nil {
375- // note: if modifying this string, search the codebase for it and change all occurrences
376- if strings .HasSuffix (errors .Message (err ), "is not deployed" ) {
377- return console .Bold (errors .Message (err )), nil
378- }
379433 return "" , err
380434 }
381435
436+ if _flagOutput == flags .JSONOutputType {
437+ bytes , err := libjson .Marshal (apiRes )
438+ if err != nil {
439+ return "" , err
440+ }
441+ return string (bytes ), nil
442+ }
443+
382444 if apiRes .RealtimeAPI != nil {
383445 return realtimeAPITable (apiRes .RealtimeAPI , env )
384446 }
@@ -390,13 +452,17 @@ func getAPI(env cliconfig.Environment, apiName string) (string, error) {
390452
391453 apiRes , err := local .GetAPI (apiName )
392454 if err != nil {
393- // note: if modifying this string, search the codebase for it and change all occurrences
394- if strings .HasSuffix (errors .Message (err ), "is not deployed" ) {
395- return console .Bold (errors .Message (err )), nil
396- }
397455 return "" , err
398456 }
399457
458+ if _flagOutput == flags .JSONOutputType {
459+ bytes , err := libjson .Marshal (apiRes )
460+ if err != nil {
461+ return "" , err
462+ }
463+ return string (bytes ), nil
464+ }
465+
400466 return realtimeAPITable (apiRes .RealtimeAPI , env )
401467}
402468
0 commit comments