2018-07-12 10:55:13 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2018-09-16 02:45:59 +03:00
|
|
|
"sort"
|
2018-08-13 19:32:11 +03:00
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-12 10:55:13 +03:00
|
|
|
)
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type commandOptions struct {
|
|
|
|
desc bool
|
|
|
|
}
|
2018-09-16 02:45:59 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
func newCommandsCommand() *cobra.Command {
|
|
|
|
env := newEnv()
|
|
|
|
options := commandOptions{}
|
2018-09-16 02:45:59 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
cmd := &cobra.Command{
|
2020-07-28 21:24:24 +03:00
|
|
|
Use: "commands",
|
2020-06-28 19:26:29 +03:00
|
|
|
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",
|
|
|
|
)
|
2018-07-16 16:21:21 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runCommands(env *Env, opts commandOptions) error {
|
2018-07-16 16:21:21 +03:00
|
|
|
first := true
|
|
|
|
|
2018-09-16 02:45:59 +03:00
|
|
|
var allCmds []*cobra.Command
|
2020-06-28 19:26:29 +03:00
|
|
|
queue := []*cobra.Command{NewRootCommand()}
|
2018-09-16 02:45:59 +03:00
|
|
|
|
|
|
|
for len(queue) > 0 {
|
|
|
|
cmd := queue[0]
|
|
|
|
queue = queue[1:]
|
|
|
|
allCmds = append(allCmds, cmd)
|
2020-02-08 18:17:15 +03:00
|
|
|
queue = append(queue, cmd.Commands()...)
|
2018-09-16 02:45:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(commandSorterByName(allCmds))
|
2018-07-16 23:38:52 +03:00
|
|
|
|
2018-07-19 13:30:25 +03:00
|
|
|
for _, cmd := range allCmds {
|
2018-07-16 16:21:21 +03:00
|
|
|
if !first {
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Println()
|
2018-07-16 16:21:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
first = false
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
if opts.desc {
|
|
|
|
env.out.Printf("# %s\n", cmd.Short)
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
2018-07-16 23:38:52 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
env.out.Print(cmd.UseLine())
|
2018-07-19 13:30:25 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
if opts.desc {
|
|
|
|
env.out.Println()
|
2018-07-16 16:21:21 +03:00
|
|
|
}
|
2018-07-19 13:30:25 +03:00
|
|
|
}
|
2018-07-16 16:21:21 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
if !opts.desc {
|
|
|
|
env.out.Println()
|
2018-07-16 16:21:21 +03:00
|
|
|
}
|
2018-07-12 10:55:13 +03:00
|
|
|
|
2018-07-16 16:21:21 +03:00
|
|
|
return nil
|
2018-07-12 10:55:13 +03:00
|
|
|
}
|
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
type commandSorterByName []*cobra.Command
|
2018-09-10 19:16:16 +03:00
|
|
|
|
2020-06-28 19:26:29 +03:00
|
|
|
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() }
|