Skip to content

Commit eb986a0

Browse files
authored
Display single environment error message when there are no apis (#1076)
1 parent 4ad6d53 commit eb986a0

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

cli/cmd/get.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,19 @@ func getAPIsInAllEnvironments() (string, error) {
146146
return "", err
147147
}
148148

149-
var errorEnvNames []string
150149
var allAPIs []spec.API
151150
var allAPIStatuses []status.Status
152151
var allMetrics []metrics.Metrics
153152
var allEnvs []string
154-
allErrs := make([]error, len(cliConfig.Environments))
155-
for i, env := range cliConfig.Environments {
153+
errorsMap := map[string]error{}
154+
for _, env := range cliConfig.Environments {
156155
var apisRes schema.GetAPIsResponse
157156
var err error
158157
if env.Provider == types.AWSProviderType {
159158
apisRes, err = cluster.GetAPIs(MustGetOperatorConfig(env.Name))
160159
} else {
161160
apisRes, err = local.GetAPIs()
162161
}
163-
allErrs[i] = err
164162

165163
if err == nil {
166164
for range apisRes.APIs {
@@ -171,19 +169,19 @@ func getAPIsInAllEnvironments() (string, error) {
171169
allAPIStatuses = append(allAPIStatuses, apisRes.Statuses...)
172170
allMetrics = append(allMetrics, apisRes.AllMetrics...)
173171
} else {
174-
errorEnvNames = append(errorEnvNames, env.Name)
172+
errorsMap[env.Name] = err
175173
}
176174
}
177175

178176
out := ""
179177

180178
if len(allAPIs) == 0 {
181-
if len(cliConfig.Environments) == 1 && allErrs[0] != nil {
182-
// Print the error if there is just one env
183-
exit.Error(allErrs[0])
179+
if len(errorsMap) == 1 {
180+
// Print the error if there is just one
181+
exit.Error(errors.FirstErrorInMap(errorsMap))
184182
}
185-
// if all envs errored, "no apis are deployed" is misleading, so skip it
186-
if !errors.AreAllErrors(allErrs) {
183+
// if all envs errored, skip it "no apis are deployed" since it's misleading
184+
if len(errorsMap) != len(cliConfig.Environments) {
187185
out += console.Bold("no apis are deployed") + "\n"
188186
}
189187
} else {
@@ -196,12 +194,12 @@ func getAPIsInAllEnvironments() (string, error) {
196194
out += t.MustFormat()
197195
}
198196

199-
if len(errorEnvNames) == 1 {
197+
if len(errorsMap) == 1 {
200198
out = s.EnsureBlankLineIfNotEmpty(out)
201-
out += fmt.Sprintf("unable to detect apis from the %s environment; run `cortex get --env %s` if this is unexpected\n", errorEnvNames[0], errorEnvNames[0])
202-
} else if len(errorEnvNames) > 1 {
199+
out += fmt.Sprintf("unable to detect apis from the %s environment; run `cortex get --env %s` if this is unexpected\n", errors.FirstKeyInErrorMap(errorsMap), errors.FirstKeyInErrorMap(errorsMap))
200+
} else if len(errorsMap) > 1 {
203201
out = s.EnsureBlankLineIfNotEmpty(out)
204-
out += fmt.Sprintf("unable to detect apis from the %s environments; run `cortex get --env ENV_NAME` if this is unexpected\n", s.StrsAnd(errorEnvNames))
202+
out += fmt.Sprintf("unable to detect apis from the %s environments; run `cortex get --env ENV_NAME` if this is unexpected\n", s.StrsAnd(errors.NonNilErrorMapKeys(errorsMap)))
205203
}
206204

207205
mismatchedAPIMessage, err := getLocalVersionMismatchedAPIsMessage()

pkg/lib/errors/multi.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,40 @@ func FirstError(errs ...error) error {
7373
}
7474
return nil
7575
}
76+
77+
func MapHasError(errs map[string]error) bool {
78+
for _, err := range errs {
79+
if err != nil {
80+
return true
81+
}
82+
}
83+
return false
84+
}
85+
86+
func FirstErrorInMap(errs map[string]error) error {
87+
for _, err := range errs {
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
return nil
93+
}
94+
95+
func FirstKeyInErrorMap(errs map[string]error) string {
96+
for k, err := range errs {
97+
if err != nil {
98+
return k
99+
}
100+
}
101+
return ""
102+
}
103+
104+
func NonNilErrorMapKeys(errs map[string]error) []string {
105+
var keys []string
106+
for k, err := range errs {
107+
if err != nil {
108+
keys = append(keys, k)
109+
}
110+
}
111+
return keys
112+
}

0 commit comments

Comments
 (0)