src/server/pkg/cmdutil/cobra.go
func MergeCommands(root *cobra.Command, children []*cobra.Command) {
	// Implement our own 'find' function because Command.Find is not reliable?
	findCommand := func(parent *cobra.Command, name string) *cobra.Command {
		for _, cmd := range parent.Commands() {
			if cmd.Name() == name {
				return cmd
			}
		}
		return nil
	}

	// Sort children by max nesting depth - this will put 'docs' commands first,
	// so they are not overwritten by logical commands added by aliases.
	var depth func(*cobra.Command) int
	depth = func(cmd *cobra.Command) int {
		maxDepth := 0
		for _, subcmd := range cmd.Commands() {
			subcmdDepth := depth(subcmd)
			if subcmdDepth > maxDepth {
				maxDepth = subcmdDepth
			}
		}
		return maxDepth + 1
	}

	sort.Slice(children, func(i, j int) bool {
		return depth(children[i]) < depth(children[j])
	})

	// Move each child command over to the main command tree recursively
	for _, cmd := range children {
		parent := findCommand(root, cmd.Name())
		if parent == nil {
			root.AddCommand(cmd)
		} else {
			MergeCommands(parent, cmd.Commands())
		}
	}
}
func MergeCommands(root *cobra.Command, children []*cobra.Command) {
	// Implement our own 'find' function because Command.Find is not reliable?
	findCommand := func(parent *cobra.Command, name string) *cobra.Command {
		for _, cmd := range parent.Commands() {
			if cmd.Name() == name {
				return cmd
			}
		}
		return nil
	}

	// Sort children by max nesting depth - this will put 'docs' commands first,
	// so they are not overwritten by logical commands added by aliases.
	var depth func(*cobra.Command) int
	depth = func(cmd *cobra.Command) int {
		maxDepth := 0
		for _, subcmd := range cmd.Commands() {
			subcmdDepth := depth(subcmd)
			if subcmdDepth > maxDepth {
				maxDepth = subcmdDepth
			}
		}
		return maxDepth + 1
	}

	sort.Slice(children, func(i, j int) bool {
		return depth(children[i]) < depth(children[j])
	})

	// Move each child command over to the main command tree recursively
	for _, cmd := range children {
		parent := findCommand(root, cmd.Name())
		if parent == nil {
			root.AddCommand(cmd)
		} else {
			MergeCommands(parent, cmd.Commands())
		}
	}
}
// MergeCommands merges several command aliases (generated by 'CreateAlias'
// above) into a single coherent cobra command tree (with root command 'root').
// Because 'CreateAlias' generates empty commands to preserve the right
// hierarchy, we go through a little extra effort to allow intermediate 'docs'
// commands to be preserved in the final command structure.