generate command tree from within (esctl --show-command-tree)

This commit is contained in:
2026-06-23 14:02:58 +02:00
parent faa8c0fe26
commit 3c6819151f
4 changed files with 135 additions and 94 deletions

View File

@@ -22,6 +22,7 @@ import (
golog "log"
"os"
"runtime/pprof"
"strings"
"codeberg.org/scip/esctl/pkg/cfg"
"codeberg.org/scip/esctl/pkg/es"
@@ -41,11 +42,11 @@ func Finish(err error) int {
func Main() int {
conf := cfg.NewConfig()
tree := false
cmd := &cli.Command{
Name: "esctl",
Usage: "manage elasticsearch from cli",
//Version: cfg.Version,
Name: "esctl",
Usage: "manage elasticsearch from cli",
EnableShellCompletion: true,
Flags: []cli.Flag{
@@ -63,6 +64,13 @@ func Main() int {
Usage: "enable HTTP debugging",
Destination: &conf.DebugHTTP,
},
&cli.BoolFlag{
Name: "show-command-tree",
Value: false,
Usage: "generate a command tree",
Destination: &tree,
Hidden: true,
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
@@ -94,25 +102,30 @@ func Main() int {
},
Commands: []*cli.Command{
Search(conf),
Cluster(conf),
Api(conf),
Ccr(conf),
Index(conf),
Ilm(conf),
Cluster(conf),
Datastream(conf),
Doc(conf),
Ilm(conf),
Index(conf),
Node(conf),
Roles(conf),
Search(conf),
Shard(conf),
Snapshot(conf),
Node(conf),
Doc(conf),
Task(conf),
Version(conf),
Debug(conf),
Roles(conf),
Task(conf),
HelpJsonPath(conf),
Api(conf),
},
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
if tree {
Tree(cmd)
os.Exit(0)
}
if err := conf.Init(); err != nil {
if len(os.Args) > 1 {
return nil, err
@@ -237,3 +250,25 @@ func Debug(conf *cfg.Config) *cli.Command {
},
}
}
func Tree(cmd *cli.Command) error {
max := 20
cmd.Walk(func(cmd *cli.Command) error {
path := cmd.Path()
command := path[len(path)-1]
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
}