Skip to content

fix: reject an unknown equinix api --format before the request - #91

Open
lbarrosby wants to merge 1 commit into
equinix:mainfrom
lbarrosby:fix/api-validate-format
Open

lbarrosby wants to merge 1 commit into
equinix:mainfrom
lbarrosby:fix/api-validate-format

Conversation

@lbarrosby

Copy link
Copy Markdown
Contributor

Fixes part 1 of #88: equinix api --format <anything but json|yaml> made the request, matched no case in the output switch and printed a bare newline with exit 0, so a typo looked like an empty API response.

The flag is now checked in PreRunE, before the client is built or any request is sent:

$ equinix api fabric/v4/ports --format xml
Error: invalid --format "xml": must be json or yaml
Usage:
  equinix api [url-path] [flags]
...

Part 2 of #88 (error bodies dropped on 4xx/5xx) is in internal/api and goes in a separate PR, as suggested in the issue.

Test

TestE2E_APIRejectsUnknownFormat (in cmd/api_output_test.go) runs the real command against a fake API that returns JSON, and checks the error and that no request was made. It resets the package-level format flag in t.Cleanup so later tests still get the json default.

# on main
--- FAIL: TestE2E_APIRejectsUnknownFormat
    api_output_test.go:95: command succeeded with --format xml, output "\n"
# with the fix
$ go test ./... && go vet ./...
ok  	github.com/equinix/cli/cmd
ok  	github.com/equinix/cli/internal/api
ok  	github.com/equinix/cli/internal/casing
ok  	github.com/equinix/cli/internal/naming

No flag or help text changes, so no docs regeneration.

The output switch only handled json and yaml, so any other --format
made the request, printed a blank line and exited 0. Validate the flag
in PreRunE so the command fails with a usage error naming the supported
values, without calling the API.

Refs equinix#88

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
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