From 3c6819151f3f7c8e13b959b23c03634ffe8185f0 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Tue, 23 Jun 2026 14:02:58 +0200 Subject: [PATCH] generate command tree from within (esctl --show-command-tree) --- README.md | 166 +++++++++++++++++++++++++++------------------------- cmd/root.go | 59 +++++++++++++++---- go.mod | 2 +- go.sum | 2 + 4 files changed, 135 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index 000831d..c284696 100644 --- a/README.md +++ b/README.md @@ -61,87 +61,91 @@ Features: Command tree: ```console - api - list - repl - show - ccr - follower - add - delete - pause - renew - resume - show - unfollow - info - pause - resume - status - cluster - list - settings - list - set - status - datastream - create - delete - list - rollover - show - debug - doc - add - delete - show - help - help-jsonpath - ilm - create - list - retry - show - status - index - alias - create - delete - list - rollover - allocation - close - create - delete - fields - ilm - list - modify - show - template - create - delete - list - modify - show - node - list - show - role - diff - list - show - search - shard - list - show - snapshot - list - show - task - cancel - list - version +api - api access and documentation + list - list index of API calls + show - show an API doc + repl - interactive API repl +ccr - manage cross cluster replication + status - cross cluster replication status (yaml config with 2 clusters required) + pause - pause shard allocation + resume - resume shard allocation + follower - manage ccr follower indices + show - show ccr follower index details + add - add ccr follower index + delete - delete ccr follower index + unfollow - unfollow ccr follower index + pause - pause ccr index to follow + resume - resume ccr index to follow + renew - renew ccr follower index + info - show ccr remote info +cluster - manage cluster[s] + status - show cluster status + list - list configured clusters + settings - cluster settings management + list - show cluster settings + set - set|update cluster settings +datastream - manage data streams + list - list indicies + show - show details about an data stream + create - create a new data stream + delete - delete a data stream + rollover - roll over a data stream +doc - manage documents + add - add JSON document index + show - show a JSON document + delete - delete JSON document[s] from index[es] +ilm - manage index lifecycle + retry - retry applying an ILM profile to an index + status - get the current index lifecycle management status + list - list index lifecycle policies + show - show details about an index lifecycle policy + create - create a index lifecycle policy +index - manage indicies + list - list indicies + show - show details about an index + create - create a new index + delete - delete an index + close - close an index + allocation - explain index allocation + modify - modify an index + fields - show info about field capabilities + ilm - show ilm status + alias - manage index aliases + create - create an index alias + list - list index aliases + delete - delete an index alias + rollover - roll over an index alias + template - manage index templates + list - list index templates + show - show details about an index template + create - create a new index template + modify - modify a new index template + delete - delete an index template +node - manage nodes + list - list nodes + show - show details about a node +role - manage roles + list - list roles + show - show details about a role + diff - show differences between roles and CSV baseline +search - search within an index +shard - manage shards + list - list shards + show - show details about a shard +snapshot - manage snapshots + list - list snapshots + show - show details about a snapshot +task - manage tasks + list - list tasks + cancel - cancel running task +version - show esctl version information +debug - developer only +help-jsonpath - show jsonpath help +completion - Output shell completion script for bash, zsh, fish, or Powershell + bash - Output bash completion script + zsh - Output zsh completion script + fish - Output fish completion script + pwsh - Output pwsh completion script ``` Configure `esctl` with environment variables: diff --git a/cmd/root.go b/cmd/root.go index 614169b..d7f7b0b 100644 --- a/cmd/root.go +++ b/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 +} diff --git a/go.mod b/go.mod index 0bfff19..c268fb9 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/olekukonko/tablewriter v1.1.4 github.com/tidwall/gjson v1.19.0 github.com/tlinden/yadu v0.1.3 - github.com/urfave/cli/v3 v3.9.1-0.20260524212652-be8b79d0c8de + github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 2f28db8..ba50cb2 100644 --- a/go.sum +++ b/go.sum @@ -189,6 +189,8 @@ github.com/tlinden/yadu v0.1.3 h1:5cRCUmj+l5yvlM2irtpFBIJwVV2DPEgYSaWvF19FtcY= github.com/tlinden/yadu v0.1.3/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA= github.com/urfave/cli/v3 v3.9.1-0.20260524212652-be8b79d0c8de h1:ESKPiS7inVoBnv4FmgGNZdjWBI/wmvaragoyD3D9nM4= github.com/urfave/cli/v3 v3.9.1-0.20260524212652-be8b79d0c8de/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso= +github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65 h1:NXitXXO9DupDLEFj8hIyYtsubRxSFTp5JXqohElT0nU= +github.com/urfave/cli/v3 v3.10.1-0.20260623012112-f980ca84bf65/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=