Skip to content

Commit bffa119

Browse files
committed
Added request result
1 parent ab6e095 commit bffa119

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

cmd/stopScheduledJobs.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
package cmd
1616

1717
import (
18+
"encoding/json"
1819
"errors"
19-
20+
"fmt"
2021
radixapi "github.com/equinor/radix-cli/generated/radixapi/client"
2122
"github.com/equinor/radix-cli/generated/radixapi/client/job"
2223
"github.com/equinor/radix-cli/pkg/client"
2324
"github.com/equinor/radix-cli/pkg/config"
2425
"github.com/equinor/radix-cli/pkg/flagnames"
26+
"github.com/equinor/radix-cli/pkg/model"
2527
"github.com/equinor/radix-cli/pkg/utils/completion"
2628
"github.com/spf13/cobra"
2729
)
@@ -52,6 +54,7 @@ var stopScheduledJobsCmd = &cobra.Command{
5254
rx stop scheduled-job --application radix-test --environment dev --all
5355
`,
5456
RunE: func(cmd *cobra.Command, args []string) error {
57+
outputFormat, _ := cmd.Flags().GetString(flagnames.Output)
5558
appName, err := config.GetAppNameFromConfigOrFromParameter(cmd, flagnames.Application)
5659
if err != nil {
5760
return err
@@ -117,12 +120,40 @@ var stopScheduledJobsCmd = &cobra.Command{
117120
}
118121

119122
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
121126
}
122127
return stopJob(apiClient, appName, envName, cmpName, jobName)
123128
},
124129
}
125130

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+
126157
func stopBatchListOrJobList(apiClient *radixapi.Radixapi, appName, envName, cmpName string, allJobs, allBatches, allBatchesAndJobs bool) error {
127158
if len(cmpName) == 0 {
128159
return stopAllBatchesAndJobsForEnvironment(apiClient, appName, envName)
@@ -145,14 +176,15 @@ func stopBatchListOrJobList(apiClient *radixapi.Radixapi, appName, envName, cmpN
145176
return nil
146177
}
147178

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) {
149180
parameters := job.NewStopBatchParams().
150181
WithAppName(appName).
151182
WithEnvName(envName).
152183
WithJobComponentName(cmpName).
153184
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
156188
}
157189

158190
func stopJob(apiClient *radixapi.Radixapi, appName, envName, cmpName, jobName string) error {
@@ -201,6 +233,7 @@ func init() {
201233
stopScheduledJobsCmd.Flags().BoolP(flagnames.Batches, "", false, "Stop all scheduled batches.")
202234
stopScheduledJobsCmd.Flags().BoolP(flagnames.Jobs, "", false, "Stop all scheduled jobs.")
203235
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)")
204237
_ = stopScheduledJobsCmd.RegisterFlagCompletionFunc(flagnames.Application, completion.ApplicationCompletion)
205238
_ = stopScheduledJobsCmd.RegisterFlagCompletionFunc(flagnames.Environment, completion.EnvironmentCompletion)
206239
_ = stopScheduledJobsCmd.RegisterFlagCompletionFunc(flagnames.Component, completion.ComponentCompletion)

pkg/model/api_request_result.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package model
2+
3+
import (
4+
"bytes"
5+
"github.com/go-openapi/runtime"
6+
)
7+
8+
// ApiRequestResult Interface for API request result
9+
type ApiRequestResult interface {
10+
ResponseReaderClientOption(operation *runtime.ClientOperation)
11+
GetResponseBody() string
12+
GetResponseCode() int
13+
GetResponseMessage() string
14+
}
15+
type apiRequestResult struct {
16+
baseReader runtime.ClientResponseReader
17+
responseBody string
18+
responseCode int
19+
responseMessage string
20+
}
21+
22+
// NewApiRequestResult creates a new ApiRequestResult
23+
func NewApiRequestResult() ApiRequestResult {
24+
return &apiRequestResult{}
25+
}
26+
27+
// ReadResponse reads a server response
28+
func (reader *apiRequestResult) ReadResponse(response runtime.ClientResponse, consumer runtime.Consumer) (interface{}, error) {
29+
body := response.Body()
30+
var buf bytes.Buffer
31+
if _, err := buf.ReadFrom(body); err != nil {
32+
return nil, err
33+
}
34+
reader.responseBody = buf.String()
35+
reader.responseCode = response.Code()
36+
reader.responseMessage = response.Message()
37+
return reader.baseReader.ReadResponse(response, consumer)
38+
}
39+
40+
// GetResponseBody returns the response body
41+
func (reader *apiRequestResult) GetResponseBody() string {
42+
return reader.responseBody
43+
}
44+
45+
// GetResponseCode returns the response code
46+
func (reader *apiRequestResult) GetResponseCode() int {
47+
return reader.responseCode
48+
}
49+
50+
// GetResponseMessage returns the response message
51+
func (reader *apiRequestResult) GetResponseMessage() string {
52+
return reader.responseMessage
53+
}
54+
55+
// ResponseReaderClientOption sets the response reader client option
56+
func (reader *apiRequestResult) ResponseReaderClientOption(operation *runtime.ClientOperation) {
57+
reader.baseReader = operation.Reader
58+
operation.Reader = reader
59+
}

0 commit comments

Comments
 (0)