Skip to content

Commit cf0220f

Browse files
authored
Merge pull request #11 from yoanbernabeu/10-bug-report-cli-doesnt-return-non-zero-exit-code-on-error
10 bug report cli doesnt return non zero exit code on error
2 parents 14c43ec + 0b4ca21 commit cf0220f

File tree

5 files changed

+102
-494
lines changed

5 files changed

+102
-494
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ jobs:
3030
github_token: ${{ secrets.GITHUB_TOKEN }}
3131
goos: ${{ matrix.goos }}
3232
goarch: ${{ matrix.goarch }}
33-
goversion: "https://dl.google.com/go/go1.18.1.linux-amd64.tar.gz"
33+
goversion: "1.24.2"
3434
binary_name: "GoJelastic"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.json
22
.env
3+
GoJelastic

cmd/root.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright © 2022 Yoan BERNABEU <yoan.bernabeu@gmail.com>
3-
43
*/
54
package cmd
65

@@ -66,7 +65,7 @@ func initConfig() map[string]interface{} {
6665
return viper.AllSettings()
6766
}
6867

69-
//writeConfig writes config file and ENV variables if set.
68+
// writeConfig writes config file and ENV variables if set.
7069
func writeConfig(url string, token string) {
7170
viper.SetConfigType("env")
7271
viper.SetConfigName("gojelastic") // name of config file (without extension)
@@ -96,6 +95,14 @@ func makeHTTPRequest(url string, method string, body string) (*http.Response, er
9695
return resp, nil
9796
}
9897

98+
// JelasticResponse represents the structure of a Jelastic API response
99+
type JelasticResponse struct {
100+
Result int `json:"result"`
101+
Error string `json:"error"`
102+
Raw interface{} `json:"raw"`
103+
Source string `json:"source"`
104+
}
105+
99106
func formatResponse(resp *http.Response) (string, error) {
100107
defer resp.Body.Close()
101108

@@ -113,7 +120,25 @@ func formatResponse(resp *http.Response) (string, error) {
113120
return string(jsonBytes), nil
114121
}
115122

116-
//makeRequest makes a request to the Jelastic API
123+
// checkResponseForErrors checks if the Jelastic response contains an error
124+
// and exits with code 1 if it does
125+
func checkResponseForErrors(responseJSON string) {
126+
var jelasticResp JelasticResponse
127+
err := json.Unmarshal([]byte(responseJSON), &jelasticResp)
128+
if err != nil {
129+
// If we can't parse as JelasticResponse, it might be a different format
130+
// In this case, we don't exit as it might be a valid response
131+
return
132+
}
133+
134+
// Check if there's an error in the response
135+
if jelasticResp.Result != 0 || jelasticResp.Error != "" {
136+
fmt.Fprintln(os.Stderr, responseJSON)
137+
os.Exit(1)
138+
}
139+
}
140+
141+
// makeRequest makes a request to the Jelastic API
117142
func makeRequest(url string, method string, body string) string {
118143
resp, err := makeHTTPRequest(url, method, body)
119144
if err != nil {
@@ -127,5 +152,8 @@ func makeRequest(url string, method string, body string) string {
127152
os.Exit(1)
128153
}
129154

155+
// Check for Jelastic API errors and exit with non-zero code if found
156+
checkResponseForErrors(formattedResponse)
157+
130158
return formattedResponse
131159
}

go.mod

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
module github.com/yoanbernabeu/GoJelastic
22

3-
go 1.18
3+
go 1.24.0
44

5-
require github.com/spf13/cobra v1.6.1
5+
toolchain go1.24.2
66

77
require (
8-
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
9-
github.com/fsnotify/fsnotify v1.6.0 // indirect
10-
github.com/hashicorp/hcl v1.0.0 // indirect
11-
github.com/inconshreveable/mousetrap v1.0.1 // indirect
12-
github.com/magiconair/properties v1.8.6 // indirect
13-
github.com/mitchellh/mapstructure v1.5.0 // indirect
14-
github.com/pelletier/go-toml v1.9.5 // indirect
15-
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
8+
github.com/spf13/cobra v1.10.1
9+
github.com/spf13/viper v1.21.0
10+
)
11+
12+
require (
13+
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
14+
github.com/fsnotify/fsnotify v1.9.0 // indirect
15+
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
16+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
17+
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
1618
github.com/russross/blackfriday/v2 v2.1.0 // indirect
17-
github.com/spf13/afero v1.9.2 // indirect
18-
github.com/spf13/cast v1.5.0 // indirect
19-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
20-
github.com/spf13/pflag v1.0.5 // indirect
21-
github.com/spf13/viper v1.14.0 // indirect
22-
github.com/subosito/gotenv v1.4.1 // indirect
23-
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
24-
golang.org/x/text v0.4.0 // indirect
25-
gopkg.in/ini.v1 v1.67.0 // indirect
26-
gopkg.in/yaml.v2 v2.4.0 // indirect
19+
github.com/sagikazarmark/locafero v0.12.0 // indirect
20+
github.com/spf13/afero v1.15.0 // indirect
21+
github.com/spf13/cast v1.10.0 // indirect
22+
github.com/spf13/pflag v1.0.10 // indirect
23+
github.com/subosito/gotenv v1.6.0 // indirect
24+
go.yaml.in/yaml/v3 v3.0.4 // indirect
25+
golang.org/x/sys v0.37.0 // indirect
26+
golang.org/x/text v0.30.0 // indirect
27+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2728
gopkg.in/yaml.v3 v3.0.1 // indirect
2829
)

0 commit comments

Comments
 (0)