return root on tree if found

This commit is contained in:
Roberto Hidalgo 2023-09-25 19:08:23 -06:00
parent d3e5afbdfc
commit 614119b22d
1 changed files with 8 additions and 3 deletions

View File

@ -3,6 +3,7 @@
package tree
import (
"bytes"
"sort"
"git.rob.mx/nidito/chinampa/internal/registry"
@ -31,8 +32,12 @@ func (t *CommandTree) Traverse(fn func(cmd *command.Command) error) error {
var tree *CommandTree
func Build(cc *cobra.Command, depth int) {
root := registry.FromCobra(cc)
if root == nil && cc.Root() == cc {
root = command.Root
}
tree = &CommandTree{
Command: registry.FromCobra(cc),
Command: root,
Children: []*CommandTree{},
}
@ -59,11 +64,11 @@ func Build(cc *cobra.Command, depth int) {
}
func Serialize(serializationFn func(any) ([]byte, error)) (string, error) {
bytes, err := serializationFn(tree)
content, err := serializationFn(tree)
if err != nil {
return "", err
}
return string(bytes), nil
return string(bytes.ReplaceAll(content, []byte("﹅"), []byte("`"))), nil
}
func ChildrenNames() []string {