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

engflow_auth: Migrate to CLI library #15

Merged
merged 1 commit into from
Jul 29, 2024
Merged

Conversation

minor-fixes
Copy link
Contributor

@minor-fixes minor-fixes commented Jul 29, 2024

This change moves CLI parsing into urfave/cli/v2, so that we can easily add more subcommands without incrementally also writing our own CLI parser. The app is now split into two objects:

  • appState, which contains injected dependencies that commands can use to interact with the various subsystems
  • cli.App, which takes care of the CLI parsing logic. Each leaf subcommand has its logic implemented as a method of appState.

This change requires the following migrations:

  • rootCmd type renamed to appState, since the former no longer does any CLI arg parsing
  • methods of appState are now integrated with cli.App, as they can be used as Actions on subcommands:
    • they now take a *cli.Context as a parameter
    • they read from/write to Readers/Writers found on the *cli.Context, rather than those on the appState (which are now removed)
  • help text is now printed on stdout rather than stderr (default behavior of cli.App)
  • autherr.CodedError implements cli.ExitCoder (it may be possible to remove CodedError in favor of cli.Exit() in the future)

Tested:

  • Because main's tests are shimming at the level of os.Args/stdin/stdout/stderr, we can have high confidence that the unit tests passing means that behavior of the CLI is not meaningfully changed
  • Manually tested version, help, login subcommands and did build on Kyanite using new binary


func TestCodedErrorImplementsExitCoder(t *testing.T) {
var want cli.ExitCoder
assert.Implements(t, &want, &CodedError{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a static check at global scope:

var _ cli.ExitCoder = (*CodedError)(nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

This change moves CLI parsing into urfave/cli/v2, so that we can easily
add more subcommands without incrementally also writing our own CLI
parser. The app is now split into two objects:

* `appState`, which contains injected dependencies that commands can use
  to interact with the various subsystems
* `cli.App`, which takes care of the CLI parsing logic. Each leaf
  subcommand has its logic implemented as a method of `appState`.

This change requires the following migrations:

* `rootCmd` type renamed to `appState`, since the former no longer does
  any CLI arg parsing
* methods of `appState` are now integrated with `cli.App`, as they can
  be used as Actions on subcommands:
  * they now take a `*cli.Context` as a parameter
  * they read from/write to Readers/Writers found on the `*cli.Context`,
    rather than those on the `appState` (which are now removed)
* help text is now printed on stdout rather than stderr (default
  behavior of `cli.App`)
* `autherr.CodedError` implements `cli.ExitCoder` (it may be possible to
  remove CodedError in favor of `cli.Exit()` in the future)

Tested:
* Because main's tests are shimming at the level of
  os.Args/stdin/stdout/stderr, we can have high confidence that the unit
  tests passing means that behavior of the CLI is not meaningfully
  changed
* TODO(scott): Manually run commands anyway
@minor-fixes minor-fixes merged commit c50599b into main Jul 29, 2024
4 checks passed
@minor-fixes minor-fixes deleted the scott/cli_migration branch July 29, 2024 22:35
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.

2 participants