Skip to content

Commit

Permalink
address feedback and add experimental option rendering
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Prasek <[email protected]>
  • Loading branch information
prasek committed Oct 30, 2024
1 parent 68f575f commit 1b98d76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 57 deletions.
12 changes: 11 additions & 1 deletion temporalcli/commandsgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (w *docWriter) writeSubcommand(c *Command) {
w.fileMap[fileName].WriteString(prefix + " " + c.LeafName + "\n\n")
w.fileMap[fileName].WriteString(c.Description + "\n\n")

if c.IsLeafCommand {
if isLeafCommand(c) {
w.fileMap[fileName].WriteString("Use the following options to change the behavior of this command.\n\n")

// gather options from command and all options aviailable from parent commands
Expand All @@ -101,6 +101,12 @@ func (w *docWriter) writeSubcommand(c *Command) {
if len(option.Short) > 0 {
w.fileMap[c.FileName].WriteString("Alias: `" + option.Short + "`\n\n")
}

if option.Experimental {
w.fileMap[fileName].WriteString(":::note" + "\n\n")
w.fileMap[fileName].WriteString("Option is experimental." + "\n\n")
w.fileMap[fileName].WriteString(":::" + "\n\n")
}
}
}
}
Expand Down Expand Up @@ -130,3 +136,7 @@ func encodeJSONExample(v string) string {
v = re.ReplaceAllString(v, "`$1`")
return v
}

func isLeafCommand(c *Command) bool {
return len(c.Children) == 0
}
56 changes: 0 additions & 56 deletions temporalcli/commandsgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ type (
OptionSets []string `yaml:"option-sets"`
Docs Docs `yaml:"docs"`
Index int
Base *Command
Parent *Command
Children []*Command
Depth int
FileName string
SubCommandName string
IsLeafCommand bool
LeafName string
MaxChildDepth int
}

// Docs represents docs-only information that is not used in CLI generation.
Expand Down Expand Up @@ -258,42 +254,6 @@ func enrichCommands(m Commands) (Commands, error) {
}

m.CommandList[c.Parent.Index].Children = append(m.CommandList[c.Parent.Index].Children, &m.CommandList[c.Index])

base := &c
for base.Depth > 1 {
base = base.Parent
}
m.CommandList[c.Index].Base = &m.CommandList[base.Index]
}

setMaxChildDepthVisitor(*rootCommand, &m)

for i, c := range m.CommandList {
if c.Parent == nil {
continue
}

subCommandStartDepth := 1
if c.Base.MaxChildDepth > 2 {
subCommandStartDepth = 2
}

subCommandName := ""
if c.Depth >= subCommandStartDepth {
subCommandName = strings.Join(strings.Split(c.FullName, " ")[subCommandStartDepth:], " ")
}

if len(subCommandName) == 0 && c.Depth == 1 {
// for operator base command to show up in tags, keywords, etc.
subCommandName = c.LeafName
}

m.CommandList[i].SubCommandName = subCommandName

if len(c.Children) == 0 {
m.CommandList[i].IsLeafCommand = true
}

}

// sorted ascending by full name of command (activity complete, batch list, etc)
Expand Down Expand Up @@ -323,19 +283,3 @@ func sortChildrenVisitor(c *Command) {
sortChildrenVisitor(command)
}
}

func setMaxChildDepthVisitor(c Command, commands *Commands) int {
maxChildDepth := 0
children := commands.CommandList[c.Index].Children
if len(children) > 0 {
for _, child := range children {
depth := setMaxChildDepthVisitor(*child, commands)
if depth > maxChildDepth {
maxChildDepth = depth
}
}
}

commands.CommandList[c.Index].MaxChildDepth = maxChildDepth
return maxChildDepth + 1
}

0 comments on commit 1b98d76

Please sign in to comment.