Skip to content

Commit 63e084b

Browse files
authored
JSON output for cortex get (#1365)
1 parent cca0fe8 commit 63e084b

File tree

13 files changed

+291
-40
lines changed

13 files changed

+291
-40
lines changed

cli/cmd/delete.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ package cmd
1818

1919
import (
2020
"fmt"
21+
"strings"
2122

2223
"github.com/cortexlabs/cortex/cli/cluster"
2324
"github.com/cortexlabs/cortex/cli/local"
25+
"github.com/cortexlabs/cortex/cli/types/flags"
2426
"github.com/cortexlabs/cortex/pkg/lib/exit"
27+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2528
"github.com/cortexlabs/cortex/pkg/lib/print"
2629
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
2730
"github.com/cortexlabs/cortex/pkg/operator/schema"
@@ -42,6 +45,7 @@ func deleteInit() {
4245
// only applies to aws provider because local doesn't support multiple replicas
4346
_deleteCmd.Flags().BoolVarP(&_flagDeleteForce, "force", "f", false, "delete the api without confirmation")
4447
_deleteCmd.Flags().BoolVarP(&_flagDeleteKeepCache, "keep-cache", "c", false, "keep cached data for the api")
48+
_deleteCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
4549
}
4650

4751
var _deleteCmd = &cobra.Command{
@@ -86,6 +90,15 @@ var _deleteCmd = &cobra.Command{
8690
}
8791
}
8892

93+
if _flagOutput == flags.JSONOutputType {
94+
bytes, err := libjson.Marshal(deleteResponse)
95+
if err != nil {
96+
exit.Error(err)
97+
}
98+
fmt.Println(string(bytes))
99+
return
100+
}
101+
89102
print.BoldFirstLine(deleteResponse.Message)
90103
},
91104
}

cli/cmd/deploy.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import (
2323

2424
"github.com/cortexlabs/cortex/cli/cluster"
2525
"github.com/cortexlabs/cortex/cli/local"
26+
"github.com/cortexlabs/cortex/cli/types/flags"
2627
"github.com/cortexlabs/cortex/pkg/lib/errors"
2728
"github.com/cortexlabs/cortex/pkg/lib/exit"
2829
"github.com/cortexlabs/cortex/pkg/lib/files"
30+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2931
"github.com/cortexlabs/cortex/pkg/lib/pointer"
3032
"github.com/cortexlabs/cortex/pkg/lib/print"
3133
"github.com/cortexlabs/cortex/pkg/lib/prompt"
@@ -57,6 +59,7 @@ func deployInit() {
5759
_deployCmd.Flags().StringVarP(&_flagDeployEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
5860
_deployCmd.Flags().BoolVarP(&_flagDeployForce, "force", "f", false, "override the in-progress api update")
5961
_deployCmd.Flags().BoolVarP(&_flagDeployDisallowPrompt, "yes", "y", false, "skip prompts")
62+
_deployCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
6063
}
6164

6265
var _deployCmd = &cobra.Command{
@@ -108,6 +111,16 @@ var _deployCmd = &cobra.Command{
108111
exit.Error(err)
109112
}
110113
}
114+
115+
if _flagOutput == flags.JSONOutputType {
116+
bytes, err := libjson.Marshal(deployResponse)
117+
if err != nil {
118+
exit.Error(err)
119+
}
120+
fmt.Println(string(bytes))
121+
return
122+
}
123+
111124
message := deployMessage(deployResponse.Results, env.Name)
112125
print.BoldFirstBlock(message)
113126
},

cli/cmd/get.go

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6467
var _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

cli/cmd/lib_batch_apis.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import (
2222

2323
"github.com/cortexlabs/cortex/cli/cluster"
2424
"github.com/cortexlabs/cortex/cli/types/cliconfig"
25+
"github.com/cortexlabs/cortex/cli/types/flags"
2526
"github.com/cortexlabs/cortex/pkg/lib/console"
26-
"github.com/cortexlabs/cortex/pkg/lib/json"
27+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2728
"github.com/cortexlabs/cortex/pkg/lib/pointer"
2829
s "github.com/cortexlabs/cortex/pkg/lib/strings"
2930
"github.com/cortexlabs/cortex/pkg/lib/table"
@@ -143,6 +144,14 @@ func getJob(env cliconfig.Environment, apiName string, jobID string) (string, er
143144
return "", err
144145
}
145146

147+
if _flagOutput == flags.JSONOutputType {
148+
bytes, err := libjson.Marshal(resp)
149+
if err != nil {
150+
return "", err
151+
}
152+
return string(bytes), nil
153+
}
154+
146155
job := resp.JobStatus
147156

148157
out := ""
@@ -238,7 +247,7 @@ func getJob(env cliconfig.Environment, apiName string, jobID string) (string, er
238247

239248
out += "\n" + console.Bold("job endpoint: ") + resp.Endpoint + "\n"
240249

241-
jobSpecStr, err := json.Pretty(job.Job)
250+
jobSpecStr, err := libjson.Pretty(job.Job)
242251
if err != nil {
243252
return "", err
244253
}

cli/cmd/logs.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ package cmd
1919
import (
2020
"fmt"
2121
"path"
22-
"strings"
2322

2423
"github.com/cortexlabs/cortex/cli/cluster"
2524
"github.com/cortexlabs/cortex/cli/local"
26-
"github.com/cortexlabs/cortex/pkg/lib/console"
27-
"github.com/cortexlabs/cortex/pkg/lib/errors"
2825
"github.com/cortexlabs/cortex/pkg/lib/exit"
2926
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
3027
"github.com/cortexlabs/cortex/pkg/types"
@@ -60,11 +57,6 @@ var _logsCmd = &cobra.Command{
6057
logPath := path.Join(args...)
6158
err := cluster.StreamLogs(MustGetOperatorConfig(env.Name), logPath)
6259
if err != nil {
63-
// note: if modifying this string, search the codebase for it and change all occurrences
64-
if strings.HasSuffix(errors.Message(err), "is not deployed") {
65-
fmt.Println(console.Bold(errors.Message(err)))
66-
return
67-
}
6860
exit.Error(err)
6961
}
7062
} else {

cli/cmd/refresh.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"fmt"
21+
"strings"
22+
2023
"github.com/cortexlabs/cortex/cli/cluster"
24+
"github.com/cortexlabs/cortex/cli/types/flags"
2125
"github.com/cortexlabs/cortex/pkg/lib/exit"
26+
libjson "github.com/cortexlabs/cortex/pkg/lib/json"
2227
"github.com/cortexlabs/cortex/pkg/lib/print"
2328
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
2429
"github.com/cortexlabs/cortex/pkg/types"
@@ -34,6 +39,7 @@ func refreshInit() {
3439
_refreshCmd.Flags().SortFlags = false
3540
_refreshCmd.Flags().StringVarP(&_flagRefreshEnv, "env", "e", getDefaultEnv(_generalCommandType), "environment to use")
3641
_refreshCmd.Flags().BoolVarP(&_flagRefreshForce, "force", "f", false, "override the in-progress api update")
42+
_refreshCmd.Flags().VarP(&_flagOutput, "output", "o", fmt.Sprintf("output format: one of %s", strings.Join(flags.OutputTypeStrings(), "|")))
3743
}
3844

3945
var _refreshCmd = &cobra.Command{
@@ -61,6 +67,16 @@ var _refreshCmd = &cobra.Command{
6167
if err != nil {
6268
exit.Error(err)
6369
}
70+
71+
if _flagOutput == flags.JSONOutputType {
72+
bytes, err := libjson.Marshal(refreshResponse)
73+
if err != nil {
74+
exit.Error(err)
75+
}
76+
fmt.Println(string(bytes))
77+
return
78+
}
79+
6480
print.BoldFirstLine(refreshResponse.Message)
6581
},
6682
}

cli/cmd/root.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"strings"
2424

25+
"github.com/cortexlabs/cortex/cli/types/flags"
2526
"github.com/cortexlabs/cortex/pkg/lib/errors"
2627
"github.com/cortexlabs/cortex/pkg/lib/exit"
2728
s "github.com/cortexlabs/cortex/pkg/lib/strings"
@@ -35,6 +36,7 @@ var (
3536
_cmdStr string
3637

3738
_configFileExts = []string{"yaml", "yml"}
39+
_flagOutput = flags.PrettyOutputType
3840

3941
_localDir string
4042
_cliConfigPath string
@@ -193,7 +195,9 @@ func printEnvIfNotSpecified(envName string, cmd *cobra.Command) error {
193195
return err
194196
}
195197

196-
fmt.Print(out)
198+
if _flagOutput == flags.PrettyOutputType {
199+
fmt.Print(out)
200+
}
197201
return nil
198202
}
199203

@@ -203,7 +207,7 @@ func envStringIfNotSpecified(envName string, cmd *cobra.Command) (string, error)
203207
return "", err
204208
}
205209

206-
if !wasEnvFlagProvided(cmd) && len(envNames) > 1 {
210+
if _flagOutput == flags.PrettyOutputType && !wasEnvFlagProvided(cmd) && len(envNames) > 1 {
207211
return fmt.Sprintf("using %s environment\n\n", envName), nil
208212
}
209213

0 commit comments

Comments
 (0)