1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package commands
import (
"sort"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/commands/execenv"
)
type commandOptions struct {
desc bool
}
func newCommandsCommand(env *execenv.Env) *cobra.Command {
options := commandOptions{}
cmd := &cobra.Command{
Use: "commands",
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",
)
return cmd
}
func runCommands(env *execenv.Env, opts commandOptions) error {
first := true
var allCmds []*cobra.Command
queue := []*cobra.Command{NewRootCommand()}
for len(queue) > 0 {
cmd := queue[0]
queue = queue[1:]
allCmds = append(allCmds, cmd)
queue = append(queue, cmd.Commands()...)
}
sort.Sort(commandSorterByName(allCmds))
for _, cmd := range allCmds {
if !first {
env.Out.Println()
}
first = false
if opts.desc {
env.Out.Printf("# %s\n", cmd.Short)
}
env.Out.Print(cmd.UseLine())
if opts.desc {
env.Out.Println()
}
}
if !opts.desc {
env.Out.Println()
}
return nil
}
type commandSorterByName []*cobra.Command
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() }
|