Repository navigation
Restructure commands in isolated units #770
Description
This issue serves to capture the completion of the work @mislav started in #909 ( #909 (comment) ) to structure our commands in isolated units instead of grouped together in a big soup of global state.
This work allows us to complete #759
Original issue body below, which captures the spirit of these improvements.
Describe the feature or problem you’d like to solve
Looking under the the command package of the project, it could have some better organization to help address a few problems that may come up while trying to maintain the CLI:
- It makes it simpler to find where all source associated with a particular subcommand is (e.g. all the code for
gh prshould be isolated from other source - It will help to eliminate dependencies that exist currently between subcommands. As of now, functions are defined in one subcommand (e.g.
displayUrlinissue.goand used inpr/reposource. It makes it hard to keep track where functions are defined/how changes to it will effect other subcommands. - It can help make it easier for contributors to understand the command structure of the CLI. Cobra structure allows subcommands to be imported into
root.goand added directly to the root command and this same structure can be added at the subcommand level as well by adding all those commands as opposed to using aninit()function.
Proposed solution
Create separate packages for subcommands/helper functions:
pr- all source forgh prissue- all source forgh issuerepo- all source forgh repocompletion- all source forgh completionversion- all source forgh versioncommon- all shared functions that are used across all commands (e.g.contextForCommand,determineBaseRepo). Should probably have a better name than something non specific likeutil,helper, orcommon.
Remove init() from root.go, import all subcommand packages to root.go, and add subcommands directly to RootCmd. Apply this same approach at the subcommand level.
How will it benefit CLI and its users?
It
will help bring better organization to the project and make it easier for people to contribute back to.
Additional context
Due to issues around circular dependencies, this may take a bit of thought, which is why I think it would make sense to remove version command logic from root.go/commonly used functions to their own package.
Thought process of structure in root.go:
import (
"github.com/cli/cli/command/issue"
"github.com/cli/cli/command/pr"
...
)
....
RootCmd.AddCommand(
issue.issueCmd(),
repo.repoCmd(),
pr.prCmd(),
version.versionCmd(),
completion.completionCmd(),
)
Activity
mislav commentedon Apr 14, 2020
Hi, thank you for the suggestion! We already planned to restructure how we organize and initialize Cobra commands in #759. Some of that work might prompt reorganizing how commands are structured in a way that maps to what you are suggesting here. We'll let you know how it goes!
[-]Subcommand Packages Under Commands Package[/-][+]Restructure commands[/+][-]Restructure commands[/-][+]Restructure commands in isolated units[/+]mislav commentedon Aug 12, 2020
@danielhelfand We have completely broken down the old
commandmega-package into a package structure underpkg/cmd/where each command is its own package. #1500Thanks for the recommendation!