mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 09:54:18 +02:00
turn --show-command-tree into sub command, hide hidden commands (#56)
This commit is contained in:
85
cmd/root.go
85
cmd/root.go
@@ -42,7 +42,6 @@ func Finish(err error) int {
|
|||||||
|
|
||||||
func Main() int {
|
func Main() int {
|
||||||
conf := cfg.NewConfig()
|
conf := cfg.NewConfig()
|
||||||
tree := false
|
|
||||||
|
|
||||||
cmd := &cli.Command{
|
cmd := &cli.Command{
|
||||||
Name: "esctl",
|
Name: "esctl",
|
||||||
@@ -64,13 +63,6 @@ func Main() int {
|
|||||||
Usage: "enable HTTP debugging",
|
Usage: "enable HTTP debugging",
|
||||||
Destination: &conf.DebugHTTP,
|
Destination: &conf.DebugHTTP,
|
||||||
},
|
},
|
||||||
&cli.BoolFlag{
|
|
||||||
Name: "show-command-tree",
|
|
||||||
Value: false,
|
|
||||||
Usage: "generate a command tree",
|
|
||||||
Destination: &tree,
|
|
||||||
Hidden: true,
|
|
||||||
},
|
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "align-ints",
|
Name: "align-ints",
|
||||||
Aliases: []string{"I"},
|
Aliases: []string{"I"},
|
||||||
@@ -125,17 +117,10 @@ func Main() int {
|
|||||||
Version(conf),
|
Version(conf),
|
||||||
Debug(conf),
|
Debug(conf),
|
||||||
HelpJsonPath(conf),
|
HelpJsonPath(conf),
|
||||||
|
HelpUsage(conf),
|
||||||
},
|
},
|
||||||
|
|
||||||
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
|
||||||
if tree {
|
|
||||||
if err := Tree(cmd); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := conf.Init(); err != nil {
|
if err := conf.Init(); err != nil {
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -261,22 +246,64 @@ func Debug(conf *cfg.Config) *cli.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Tree(cmd *cli.Command) error {
|
func HelpUsage(conf *cfg.Config) *cli.Command {
|
||||||
max := 20
|
return &cli.Command{
|
||||||
|
Name: "help-command-overview",
|
||||||
|
Usage: "show overview of all available commands",
|
||||||
|
Aliases: []string{"usage"},
|
||||||
|
|
||||||
return cmd.Walk(func(cmd *cli.Command) error {
|
Flags: []cli.Flag{
|
||||||
path := cmd.Path()
|
&cli.BoolFlag{
|
||||||
command := path[len(path)-1]
|
Name: "hidden",
|
||||||
|
Usage: "include hidden commands",
|
||||||
|
Destination: &conf.Hidden,
|
||||||
|
Aliases: []string{"H"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
if len(path) == 1 || command == "help" {
|
Action: func(ctx context.Context, cmd *cli.Command) error {
|
||||||
return nil
|
max := 22
|
||||||
}
|
|
||||||
|
|
||||||
indent := strings.Repeat(" ", len(path[1:])-1)
|
return walkVisible(conf, cmd.Root(), func(cmd *cli.Command) error {
|
||||||
space := strings.Repeat(" ", max-(len(command)+len(indent)))
|
path := cmd.Path()
|
||||||
|
command := path[len(path)-1]
|
||||||
|
|
||||||
fmt.Printf("%s%s %s - %s\n", indent, command, space, cmd.Usage)
|
if len(path) == 1 || command == "help" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
indent := strings.Repeat(" ", len(path[1:])-1)
|
||||||
|
space := strings.Repeat(" ", max-(len(command)+len(indent)))
|
||||||
|
|
||||||
|
fmt.Printf("%s%s %s - %s\n", indent, command, space, cmd.Usage)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
}
|
||||||
})
|
|
||||||
|
// copy of cmd.Walk() with the exception to skip hidden commands and its siblings
|
||||||
|
// see: https://github.com/urfave/cli/issues/2372
|
||||||
|
func walkVisible(conf *cfg.Config, cmd *cli.Command, fn func(*cli.Command) error) error {
|
||||||
|
if fn == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if !conf.Hidden && cmd.Hidden {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fn(cmd); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sub := range cmd.Commands {
|
||||||
|
if err := walkVisible(conf, sub, fn); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = `v0.0.22`
|
Version string = `v0.0.23`
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
Reference in New Issue
Block a user