diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
commit | 26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch) | |
tree | f1fe939311c75bd615071e96f3d37822cccd77a7 /commands/commands.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz |
commands: refactor to avoid globals
Diffstat (limited to 'commands/commands.go')
-rw-r--r-- | commands/commands.go | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/commands/commands.go b/commands/commands.go index bd8cb14d..103d5412 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -1,27 +1,42 @@ package commands import ( - "fmt" "sort" "github.com/spf13/cobra" ) -var ( - commandsDesc bool -) +type commandOptions struct { + desc bool +} -type commandSorterByName []*cobra.Command +func newCommandsCommand() *cobra.Command { + env := newEnv() + options := commandOptions{} -func (c commandSorterByName) Len() int { return len(c) } -func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] } -func (c commandSorterByName) Less(i, j int) bool { return c[i].CommandPath() < c[j].CommandPath() } + cmd := &cobra.Command{ + Use: "commands [<option>...]", + Short: "Display available commands.", + RunE: func(cmd *cobra.Command, args []string) error { + return runCommands(env, options) + }, + } + + flags := cmd.Flags() + flags.SortFlags = false + + flags.BoolVarP(&options.desc, "pretty", "p", false, + "Output the command description as well as Markdown compatible comment", + ) -func runCommands(cmd *cobra.Command, args []string) error { + return cmd +} + +func runCommands(env *Env, opts commandOptions) error { first := true var allCmds []*cobra.Command - queue := []*cobra.Command{RootCmd} + queue := []*cobra.Command{NewRootCommand()} for len(queue) > 0 { cmd := queue[0] @@ -34,41 +49,31 @@ func runCommands(cmd *cobra.Command, args []string) error { for _, cmd := range allCmds { if !first { - fmt.Println() + env.out.Println() } first = false - if commandsDesc { - fmt.Printf("# %s\n", cmd.Short) + if opts.desc { + env.out.Printf("# %s\n", cmd.Short) } - fmt.Print(cmd.UseLine()) + env.out.Print(cmd.UseLine()) - if commandsDesc { - fmt.Println() + if opts.desc { + env.out.Println() } } - if !commandsDesc { - fmt.Println() + if !opts.desc { + env.out.Println() } return nil } -var commandsCmd = &cobra.Command{ - Use: "commands [<option>...]", - Short: "Display available commands.", - RunE: runCommands, -} - -func init() { - RootCmd.AddCommand(commandsCmd) - - commandsCmd.Flags().SortFlags = false +type commandSorterByName []*cobra.Command - commandsCmd.Flags().BoolVarP(&commandsDesc, "pretty", "p", false, - "Output the command description as well as Markdown compatible comment", - ) -} +func (c commandSorterByName) Len() int { return len(c) } +func (c commandSorterByName) Swap(i, j int) { c[i], c[j] = c[j], c[i] } +func (c commandSorterByName) Less(i, j int) bool { return c[i].CommandPath() < c[j].CommandPath() } |