Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

silence usage messages when usage was valid and API errors are returned #162

Merged
merged 1 commit into from
Dec 13, 2021

Conversation

displague
Copy link
Member

@displague displague commented Dec 10, 2021

Silence usage errors to make API errors more apparent. Currently, the API Usage messages can easily hide API errors.

See spf13/cobra#340 (comment) for implementation alternatives. The linked comment is the approach that I found to be most effective.

A few functions were refactored to get usage validation out of the way before silencing usage rendering and executing API commands. Some of these functions had condition branches that could be logically hoisted out of else conditions.

Before:

$ go run ./cmd/metal/main.go  capacity check --metro xy -P c3.small  -q 1
Error: Could not check capacity: POST https://api.equinix.com/metal/v1/capacity/metros: 422 Metro xy is invalid
Usage:
metal capacity check [flags]
... 20 more lines of text

and After:

$ go run ./cmd/metal/main.go  capacity check --metro xy -P c3.small  -q 1
Error: Could not check capacity: POST https://api.equinix.com/metal/v1/capacity/metros: 422 Metro xy is invalid 

When invalid argument combinations are offered, the usage text is rendered.

$ go run ./cmd/metal/main.go  capacity check --metro xy -P c3.small 
Error: required flag(s) "quantity" not set
Usage:
  metal capacity check [flags]

@displague displague merged commit a0698e1 into main Dec 13, 2021
@displague displague deleted the silent-usage branch December 13, 2021 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant