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

Add --json flag for resource-class cmds #1088

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

jdelnano
Copy link

@jdelnano jdelnano commented Dec 20, 2024

Checklist

=========

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have checked for similar issues and haven't found anything relevant.
  • This is not a security issue (which should be reported here: https://circleci.com/security/)
  • I have read Contribution Guidelines.

Internal Checklist

Not applicable to me, a non-CircleCI employee

  • I am requesting a review from my own team as well as the owning team
  • I have a plan in place for the monitoring of the changes that I am making (this can include new monitors, logs to be aware of, etc...)

Changes

=======

Implementation

I implemented my changes in the following way:

  • I added a local --json flag to the resource_class.go file where it was appropriate to, for the create and list command actions.
  • In each command, I check whether or not the --json flag was set on the command line, and if so, we Marshal the appropriate object and set it in the Writer interface offered by the cobra cmd object.
  • I introduced the JSON formatting logic such that the original CLI behavior is unchanged. If you submit commands like:
    • circleci runner resource-class list <>
    • circleci runner resource-class create <>

they will behave exactly as they did. Only if you include a --json flag will you see new behavior, returned to stdout.

Tests

I made sure to add tests in resource_class_test.go where appropriate as well as update the [create-expected-usage.txt](https://github.com/CircleCI-Public/circleci-cli/compare/develop...jdelnano:circleci-cli:add-json-output-flag-for-resource-class-cmds?expand=1#diff-694751a2b7d7a982b3a13b6bcb00d307f3e25dc7014c17520056d3d3c1dbd8ca) file.

Rationale

=========

Background

In my position at Okta, a CircleCI customer, I have a few automation tasks that require the use of the circleci runner resource-class list and circleci runner resource-class create commands, where I specifically need to grab the output of these commands and do subsequent actions. As a result, my motivation for adding the --json flag support for the circleci runner resource-class command is to return more easily parsable output, instead of the default ASCII table format.

Manual Testing

I executed the following commands manually (alongside the automated testing), to ensure:

  • They work as expected, adding in the new JSON formatting.
  • The old CLI behavior is maintained as before these changes.
make build

➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class list <circleci-org>
➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class list <circleci-org> --json
➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class create <circleci-org>/<resource-class-name> "test"
➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class create <circleci-org>/<resource-class-name> "test" --json
➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class create <circleci-org>/<resource-class-name> "test" --generate-token
➜  circleci-cli git:(add-json-output-flag-for-resource-class-cmds) ./build/darwin/arm64/circleci runner resource-class create <circleci-org>/<resource-class-name> "test" --generate-token --json

Screenshots

============

Before

Image or [gif](https://giphy.com/apps/giphycapture)

After

Image or gif where change can be clearly seen

Here are some helpful tips you can follow when submitting a pull request:

  1. Fork the repository and create your branch from main.
  2. Run make build in the repository root.
  3. If you've fixed a bug or added code that should be tested, add tests!
  4. Ensure the test suite passes (make test).
  5. The --debug flag is often helpful for debugging HTTP client requests and responses.
  6. Format your code with gofmt.
  7. Make sure your code lints (make lint). Note: This requires Docker to run inside a local job.

@jdelnano jdelnano requested a review from a team as a code owner December 20, 2024 20:18
@jdelnano jdelnano force-pushed the add-json-output-flag-for-resource-class-cmds branch from 4b4fb83 to 5ecaaae Compare December 20, 2024 20:25
@jdelnano jdelnano marked this pull request as draft December 20, 2024 20:26
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