mirror of
https://codeberg.org/scip/esctl.git
synced 2026-08-24 06:34:18 +02:00
generate command tree from within (esctl --show-command-tree)
This commit is contained in:
59
cmd/root.go
59
cmd/root.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user