@@ -9,24 +9,27 @@ import (
99)
1010
1111type Command struct {
12- Name string
13- Description string
14- Usage string
15- Aliases []string
16- Examples string
17- Slug string
18- RunInWebCLI bool
12+ Name string `json:"name"`
13+ Description string `json:"description"`
14+ Usage string `json:"usage"`
15+ Aliases []string `json:"aliases,omitempty"`
16+ Examples string `json:"examples,omitempty"`
17+ Slug string `json:"slug,omitempty"`
18+ RunInWebCLI bool `json:"runInWebCLI,omitempty"`
19+ CommandType string `json:"commandType"`
1920
20- Flags map [string ][] Flag
21+ Annotations map [string ]string `json:"annotations,omitempty"`
2122
22- SubCommands []Command
23+ Flags map [string ][]Flag `json:"flags,omitempty"`
24+
25+ SubCommands []Command `json:"subCommands,omitempty"`
2326}
2427
2528type Flag struct {
26- Name string
27- Shorthand string
28- Description string
29- Default string
29+ Name string `json:"name"`
30+ Shorthand string `json:"shorthand,omitempty"`
31+ Description string `json:"description"`
32+ Default string `json:"default"`
3033}
3134
3235func newFlag (flag * pflag.Flag ) Flag {
@@ -61,6 +64,8 @@ func newCommand(cmd *cobra.Command) Command {
6164 Aliases : cmd .Aliases ,
6265 Examples : cmd .Example ,
6366 RunInWebCLI : false ,
67+ CommandType : commandType (cmd ),
68+ Annotations : cmd .Annotations ,
6469 }
6570 if value , ok := cmd .Annotations ["runInWebCLI" ]; ok && value != "" {
6671 command .RunInWebCLI = true
@@ -89,6 +94,22 @@ func newCommand(cmd *cobra.Command) Command {
8994 return command
9095}
9196
97+ func commandType (cmd * cobra.Command ) string {
98+ switch cmd .Name () {
99+ case "tail" :
100+ return "stream"
101+ case "search" , "browse" , "get" , "list" , "stats" , "analyze" , "describe" , "schema" , "open" :
102+ return "read"
103+ case "create" , "delete" , "clear" , "import" , "update" , "set" , "save" , "move" , "copy" , "crawl" , "run" , "pause" , "reindex" , "unblock" , "remove" , "add" , "setdefault" :
104+ return "write"
105+ default :
106+ if cmd .HasAvailableSubCommands () {
107+ return "namespace"
108+ }
109+ return "other"
110+ }
111+ }
112+
92113func getCommands (cmd * cobra.Command ) []Command {
93114 var commands []Command
94115 for _ , c := range cmd .Commands () {
@@ -113,6 +134,49 @@ func getCommands(cmd *cobra.Command) []Command {
113134 return commands
114135}
115136
137+ func describeCommand (cmd * cobra.Command ) Command {
138+ command := newCommand (cmd )
139+ if cmd .HasAvailableSubCommands () {
140+ command .SubCommands = getCommands (cmd )
141+ }
142+ return command
143+ }
144+
145+ // DescribeCommand returns a machine-readable description of a command.
146+ func DescribeCommand (cmd * cobra.Command ) Command {
147+ return describeCommand (cmd )
148+ }
149+
150+ // FindCommand resolves a command path against the provided root command.
151+ func FindCommand (root * cobra.Command , args []string ) (* cobra.Command , error ) {
152+ current := root
153+ for _ , arg := range args {
154+ next := findChildCommand (current , arg )
155+ if next == nil {
156+ return nil , cmdutil .FlagErrorf ("unknown command %q for %q" , arg , current .CommandPath ())
157+ }
158+ current = next
159+ }
160+ return current , nil
161+ }
162+
163+ func findChildCommand (cmd * cobra.Command , name string ) * cobra.Command {
164+ for _ , child := range cmd .Commands () {
165+ if child .Hidden || child .Name () == "help" {
166+ continue
167+ }
168+ if child .Name () == name {
169+ return child
170+ }
171+ for _ , alias := range child .Aliases {
172+ if alias == name {
173+ return child
174+ }
175+ }
176+ }
177+ return nil
178+ }
179+
116180type Example struct {
117181 Desc string
118182 Code string
0 commit comments