-
Notifications
You must be signed in to change notification settings - Fork 805
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Groxx
requested review from
Shaddoll,
neil-xie,
davidporter-id-au,
shijiesheng,
jakobht,
3vilhamster,
sankari165,
dkrotx,
taylanisikdemir and
demirkayaender
as code owners
October 7, 2024 01:42
taylanisikdemir
approved these changes
Oct 7, 2024
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Sets up some foundations for the coming coverage sprint:
cli.App.Metadata
)Next steps are roughly:
fmt.*Print*
calls and use the appropriate output writer insteadErrorAndExit
s over to error returns at some pointos.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: insidecli.App
-constructors. And since theMetadata
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.