Skip to content

Commit

Permalink
Deep copy cobra command tree that is remapped
Browse files Browse the repository at this point in the history
Clone Command node involved in a remap along with all its descendent
commands. This ensures that the markdown documents generated from the
cloned nodes are consistent and reflect the remap, but does not
introduce inconsistencies in the existing nodes.

Signed-off-by: Vui Lam <vui.lam@broadcom.com>
  • Loading branch information
vuil committed Apr 29, 2024
1 parent cf25cd9 commit 80c8993
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion plugin/generate_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,39 @@ func mapCommand(tanzuCmd, subCommand *cobra.Command, mapEntry *CommandMapEntry)
}
}

func cloneChildCommands(parentCloneCmd *cobra.Command) {
childCommands := parentCloneCmd.Commands()
for _, child := range childCommands {
newChild := *child
cloneChildCommands(&newChild)
childParent := child.Parent()
// child's parent pointer is wrong (still point to childParent, the
// source that parentCloneCmd is cloned from), so remove said child
parentCloneCmd.RemoveCommand(child)
// and add the properly updated clone of child instead
parentCloneCmd.AddCommand(&newChild)
// however, the removal has the side-effect of wiping the child's
// parent link, so remove then re-add back as child of the clone source
childParent.RemoveCommand(child)
childParent.AddCommand(child)

// there is currently no way to obtain the current help command so
// check the .Use field instead
// failure to update the help command to the clone will result in md
// generated for the clone (tanzu_xxx_help.md), something that is
// extraneous but not incorrect.
if child.Use == "help [command]" {
parentCloneCmd.SetHelpCommand(&newChild)
}
}
}

func deepCopy(cmd *cobra.Command) *cobra.Command {
clone := *cmd
cloneChildCommands(&clone)
return &clone
}

// rebuildTanzuCommandTree reconstructs the tanzu CLI's placement of the
// plugin's commands after accounting for the plugin descriptor's CommandMap.
// It is primarily used to prep a Command tree for cobra's doc generation APIs
Expand All @@ -86,7 +119,13 @@ func rebuildTanzuCommandTree(tanzuCmd, pluginRootCmd *cobra.Command, desc *Plugi
for _, v := range cmap {
mapEntry := v
cmd, _ := findSubCommandByHierarchy(pluginRootCmd, hierarchyFromPath(mapEntry.SourceCommandPath), matchOnCommandName)
mapCommand(tanzuCmd, cmd, &mapEntry)
copyOfCmd := deepCopy(cmd)
mapCommand(tanzuCmd, copyOfCmd, &mapEntry)
if cmd == pluginRootCmd {
// if this is a plugin level mapping, assume that the original
// plugin's command tree should not be made available anymore
cmd.Hidden = true
}
}
}

Expand Down

0 comments on commit 80c8993

Please sign in to comment.