refactor(cli): use command runner to run predictable logic #4176
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.
Summary
The CLI that runs the commands has become very repeated. I had attempted few refactors before, and eventually landed on this one.
As for now, this refactor only touches the
format
command, to show how it has become. We can discuss the architecture here, and one we merge this PR, I will refactor the rest of the commands.We now have a
CommandRunner
trait that all the payloads need to implement in order to be run. Eventually, only therun
command is called, then the rest is just repeated logic that was moved fromformat.rs
.Pros
With a trait, now the bulk of the logic lives inside one single function, and it is shared among commands. This should reduce the repeated work, especially when we add new CLI options that affect multiple commands e.g. VCS integration.
Cons
When merging the configuration, because we use
&mut self
, we need to useclone()
every time we need to merge CLI options into the configuration loaded from disk.Test Plan
The existing tests should pass