15
15
package cmd
16
16
17
17
import (
18
+ "encoding/json"
18
19
"errors"
19
-
20
+ "fmt"
20
21
radixapi "github.com/equinor/radix-cli/generated/radixapi/client"
21
22
"github.com/equinor/radix-cli/generated/radixapi/client/job"
22
23
"github.com/equinor/radix-cli/pkg/client"
23
24
"github.com/equinor/radix-cli/pkg/config"
24
25
"github.com/equinor/radix-cli/pkg/flagnames"
26
+ "github.com/equinor/radix-cli/pkg/model"
25
27
"github.com/equinor/radix-cli/pkg/utils/completion"
26
28
"github.com/spf13/cobra"
27
29
)
@@ -52,6 +54,7 @@ var stopScheduledJobsCmd = &cobra.Command{
52
54
rx stop scheduled-job --application radix-test --environment dev --all
53
55
` ,
54
56
RunE : func (cmd * cobra.Command , args []string ) error {
57
+ outputFormat , _ := cmd .Flags ().GetString (flagnames .Output )
55
58
appName , err := config .GetAppNameFromConfigOrFromParameter (cmd , flagnames .Application )
56
59
if err != nil {
57
60
return err
@@ -117,12 +120,40 @@ var stopScheduledJobsCmd = &cobra.Command{
117
120
}
118
121
119
122
if len (batchName ) > 0 {
120
- return stopBatch (apiClient , appName , envName , cmpName , batchName )
123
+ requestResult , err := stopBatch (apiClient , appName , envName , cmpName , batchName )
124
+ printResult (requestResult , outputFormat )
125
+ return err
121
126
}
122
127
return stopJob (apiClient , appName , envName , cmpName , jobName )
123
128
},
124
129
}
125
130
131
+ func printResult (result model.ApiRequestResult , outputFormat string ) {
132
+ responseBody := result .GetResponseBody ()
133
+ if outputFormat == "json" {
134
+ fmt .Println (responseBody )
135
+ return
136
+ }
137
+ fmt .Printf ("Response code: %d\n " , result .GetResponseCode ())
138
+ fmt .Printf ("Response message: %s\n " , result .GetResponseMessage ())
139
+ type ResponseBody struct {
140
+ responseType string `json:"type"`
141
+ message string `json:"message"`
142
+ errorMessage string `json:"error"`
143
+ }
144
+ body := & ResponseBody {}
145
+ bytes := []byte (responseBody )
146
+ if err := json .Unmarshal (bytes , body ); err == nil {
147
+ fmt .Printf ("Type: %s\n " , body .responseType )
148
+ fmt .Printf ("Message: %s\n " , body .message )
149
+ if len (body .errorMessage ) > 0 {
150
+ fmt .Printf ("Error: %s\n " , body .errorMessage )
151
+ }
152
+ } else {
153
+ fmt .Printf ("Response body: %s\n " , responseBody )
154
+ }
155
+ }
156
+
126
157
func stopBatchListOrJobList (apiClient * radixapi.Radixapi , appName , envName , cmpName string , allJobs , allBatches , allBatchesAndJobs bool ) error {
127
158
if len (cmpName ) == 0 {
128
159
return stopAllBatchesAndJobsForEnvironment (apiClient , appName , envName )
@@ -145,14 +176,15 @@ func stopBatchListOrJobList(apiClient *radixapi.Radixapi, appName, envName, cmpN
145
176
return nil
146
177
}
147
178
148
- func stopBatch (apiClient * radixapi.Radixapi , appName , envName , cmpName , batchName string ) error {
179
+ func stopBatch (apiClient * radixapi.Radixapi , appName , envName , cmpName , batchName string ) (model. ApiRequestResult , error ) {
149
180
parameters := job .NewStopBatchParams ().
150
181
WithAppName (appName ).
151
182
WithEnvName (envName ).
152
183
WithJobComponentName (cmpName ).
153
184
WithBatchName (batchName )
154
- _ , err := apiClient .Job .StopBatch (parameters , nil )
155
- return err
185
+ apiRequestResult := model .NewApiRequestResult ()
186
+ _ , err := apiClient .Job .StopBatch (parameters , nil , apiRequestResult .ResponseReaderClientOption )
187
+ return apiRequestResult , err
156
188
}
157
189
158
190
func stopJob (apiClient * radixapi.Radixapi , appName , envName , cmpName , jobName string ) error {
@@ -201,6 +233,7 @@ func init() {
201
233
stopScheduledJobsCmd .Flags ().BoolP (flagnames .Batches , "" , false , "Stop all scheduled batches." )
202
234
stopScheduledJobsCmd .Flags ().BoolP (flagnames .Jobs , "" , false , "Stop all scheduled jobs." )
203
235
stopScheduledJobsCmd .Flags ().BoolP (flagnames .All , "" , false , "Stop all scheduled batches and jobs." )
236
+ stopScheduledJobsCmd .Flags ().StringP (flagnames .Output , "o" , "text" , "(Optional) Output format. json or not set (plain text)" )
204
237
_ = stopScheduledJobsCmd .RegisterFlagCompletionFunc (flagnames .Application , completion .ApplicationCompletion )
205
238
_ = stopScheduledJobsCmd .RegisterFlagCompletionFunc (flagnames .Environment , completion .EnvironmentCompletion )
206
239
_ = stopScheduledJobsCmd .RegisterFlagCompletionFunc (flagnames .Component , completion .ComponentCompletion )
0 commit comments