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

Switch to dependency injection for the main CLI #6331

Merged
merged 2 commits into from
Oct 7, 2024

Conversation

Groxx
Copy link
Member

@Groxx Groxx commented Oct 7, 2024

Sets up some foundations for the coming coverage sprint:

  • a standard way to inject dependencies into this CLI app (via cli.App.Metadata)
  • a standard way to read and write CLI text (not yet used, but hopefully the intent is clear enough)
  • many more error returns to cover the new branches

Next steps are roughly:

  • write tests!
  • remove all fmt.*Print* calls and use the appropriate output writer instead
    • this will expose more error branches: we should be using them. standard outputs can fail if a pipe fails, and we should stop running when that occurs.
  • remove all stdin-reading calls, and use the input reader instead
    • this will allow us to answer prompts / continue paging in tests
  • move ErrorAndExits over to error returns at some point
    • a lot of these really are "impossible" or are validated extremely early (CLI flags), so a panic equivalent is kinda reasonable for ergonomics
    • os.Exit(1) is much more restricting than panics, as it prevents running any defers (for printing or flushing to files) and cannot be checked in tests or suppressed for "attempt or use fallback"

I did try to use the cli.Context.Context field for more "normal" dependency injection... but it ended up more annoying.

The main issue it that requires using app.RunContext(populatedContext), but direct access to the *cli.App exists all over and urfave's API largely requires this. That leaves a rather large hard-to-protect error path, and it takes a few additional awkward lines of code to construct and/or pass it around.

With Metadata, that construction can be done where all the other construction is done: inside cli.App-constructors. And since the Metadata field is completely ignored by urfave's internals currently, it's basically equivalent but easier.

And last but not least: if they do start printing Metadata some day or something, we can switch to context key(s) with relatively minimal fuss since they're both available in the same way in the same locations.

Sets up some foundations for the coming coverage sprint:
- a standard way to inject dependencies into this CLI app (via app.Metadata)
- a standard way to read and write CLI text (not yet used, but hopefully the intent is clear enough)
- many more error returns to cover the new branches

Next steps are roughly:
- write tests!
- remove all `fmt.*Print*` calls and use the appropriate output writer instead.
  - this will expose more error branches: we should be using them, as output can fail if a pipe fails.
- remove all stdin-reading calls, and use the input reader instead
  - this will allow us to answer prompts / continue paging in tests
Copy link

codecov bot commented Oct 7, 2024

Codecov Report

Attention: Patch coverage is 14.79714% with 357 lines in your changes missing coverage. Please review.

Project coverage is 73.48%. Comparing base (084949b) to head (f326478).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
tools/cli/factory.go 0.00% 75 Missing ⚠️
tools/cli/workflow_commands.go 24.41% 51 Missing and 14 partials ⚠️
tools/cli/admin_commands.go 2.77% 34 Missing and 1 partial ⚠️
tools/cli/admin.go 13.04% 19 Missing and 1 partial ⚠️
tools/cli/domain_migration_command.go 0.00% 19 Missing ⚠️
tools/cli/admin_failover_commands.go 23.80% 13 Missing and 3 partials ⚠️
tools/cli/admin_dlq_commands.go 0.00% 15 Missing ⚠️
tools/cli/isolation-groups.go 0.00% 14 Missing ⚠️
tools/cli/admin_config_store_commands.go 0.00% 13 Missing ⚠️
tools/cli/workflow_batch_commands.go 0.00% 13 Missing ⚠️
... and 11 more
Additional details and impacted files
Files with missing lines Coverage Δ
tools/cli/admin_db_clean_command.go 0.00% <ø> (ø)
tools/cli/domain.go 64.70% <100.00%> (+4.70%) ⬆️
tools/cli/utils.go 55.98% <100.00%> (ø)
tools/cli/database.go 46.72% <0.00%> (ø)
tools/cli/cluster_commands.go 50.00% <33.33%> (-5.56%) ⬇️
tools/cli/admin_kafka_commands.go 0.00% <0.00%> (ø)
tools/cli/task_list_commands.go 42.18% <16.66%> (-2.82%) ⬇️
tools/cli/admin_task_list_commands.go 0.00% <0.00%> (ø)
tools/cli/app.go 95.09% <45.45%> (-3.64%) ⬇️
tools/cli/admin_async_queue_commands.go 0.00% <0.00%> (ø)
... and 14 more

... and 4 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 084949b...f326478. Read the comment docs.

Comment on lines +218 to +224
// currently Metadata is completely unused by urfave/cli/v2, and it has fewer ways to fail
// than using the ctx.Context (as you must use RunContext to supply dependencies via the Context).
//
// this is fairly easy to move to ctx.Context if needed, it just leads to slightly more complex code.

// intentionally panics when an invalid context is not passed in, to help collapse logic branches.
// generally speaking this should not be possible to trigger without doing something obviously questionable.
Copy link
Member

Choose a reason for hiding this comment

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

👍

@Groxx Groxx merged commit 9e6cf5f into cadence-workflow:master Oct 7, 2024
19 of 20 checks passed
@Groxx Groxx deleted the cli-prep branch October 7, 2024 17:20
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