-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.go
More file actions
171 lines (150 loc) · 4.1 KB
/
Copy pathtree.go
File metadata and controls
171 lines (150 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"encoding/json"
"fmt"
"slices"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var treeCmd = &cobra.Command{
Use: "tree",
Short: "Display the full command tree",
Long: "Display the command and subcommand tree. Use --flags to include local flag details and --inherited to include inherited flag details.",
Args: cobra.NoArgs,
RunE: runTree,
}
var (
treeIncludeFlags bool
treeIncludeInherited bool
)
type printCommandTreeInput struct {
cmd *cobra.Command
prefix string
}
type commandTreeJSON struct {
Name string `json:"name"`
Short string `json:"short"`
LocalFlags []string `json:"local_flags,omitempty"`
InheritedFlags []string `json:"inherited_flags,omitempty"`
Subcommands []commandTreeJSON `json:"subcommands,omitempty"`
}
func init() {
treeCmd.Flags().BoolVar(&treeIncludeFlags, "flags", false, "Include local flags in text tree output")
treeCmd.Flags().BoolVar(&treeIncludeInherited, "inherited", false, "Include inherited flags in text tree output")
rootCmd.AddCommand(treeCmd)
}
func runTree(_ *cobra.Command, _ []string) error {
initTreeSurface(rootCmd)
if jsonOutput {
data, err := json.MarshalIndent(buildCommandTreeJSON(rootCmd), "", " ")
if err != nil {
return fmt.Errorf("marshaling JSON: %w", err)
}
fmt.Println(string(data))
return nil
}
var b strings.Builder
printCommandTree(&b, printCommandTreeInput{cmd: rootCmd})
fmt.Print(b.String())
return nil
}
func initTreeSurface(cmd *cobra.Command) {
cmd.InitDefaultHelpFlag()
if cmd == rootCmd {
cmd.InitDefaultHelpCmd()
cmd.InitDefaultVersionFlag()
cmd.InitDefaultCompletionCmd()
}
for _, child := range cmd.Commands() {
initTreeSurface(child)
}
}
// printCommandTree recursively prints a command and its children with
// box-drawing connectors. Flag details are opt-in so the default tree stays
// focused on the command surface.
func printCommandTree(b *strings.Builder, in printCommandTreeInput) {
cmd := in.cmd
prefix := in.prefix
// Print this command's name and short description.
if cmd == rootCmd {
fmt.Fprintf(b, "%s — %s\n", cmd.Name(), cmd.Short)
}
localFlags := visibleFlagNames(cmd.LocalFlags())
inheritedFlags := visibleFlagNames(cmd.InheritedFlags())
// Collect non-hidden subcommands.
var visible []*cobra.Command
for _, child := range cmd.Commands() {
if !child.Hidden {
visible = append(visible, child)
}
}
total := len(visible)
if treeIncludeFlags {
total += len(localFlags)
}
if treeIncludeInherited && len(inheritedFlags) > 0 {
total++
}
idx := 0
// Print local flags.
if treeIncludeFlags {
for _, flag := range localFlags {
idx++
connector := "├── "
if idx == total {
connector = "└── "
}
fmt.Fprintf(b, "%s%s%s\n", prefix, connector, flag)
}
}
if treeIncludeInherited && len(inheritedFlags) > 0 {
idx++
connector := "├── "
if idx == total {
connector = "└── "
}
fmt.Fprintf(b, "%s%sinherits: %s\n", prefix, connector, strings.Join(inheritedFlags, ", "))
}
// Print subcommands.
for i, child := range visible {
idx++
connector := "├── "
childPrefix := prefix + "│ "
if i == len(visible)-1 && idx == total {
connector = "└── "
childPrefix = prefix + " "
}
fmt.Fprintf(b, "%s%s%s — %s\n", prefix, connector, child.Name(), child.Short)
printCommandTree(b, printCommandTreeInput{
cmd: child,
prefix: childPrefix,
})
}
}
func visibleFlagNames(flags *pflag.FlagSet) []string {
var names []string
flags.VisitAll(func(f *pflag.Flag) {
if f.Hidden {
return
}
names = append(names, "--"+f.Name)
})
slices.Sort(names)
return names
}
func buildCommandTreeJSON(cmd *cobra.Command) commandTreeJSON {
node := commandTreeJSON{
Name: cmd.Name(),
Short: cmd.Short,
LocalFlags: visibleFlagNames(cmd.LocalFlags()),
InheritedFlags: visibleFlagNames(cmd.InheritedFlags()),
}
for _, child := range cmd.Commands() {
if child.Hidden {
continue
}
node.Subcommands = append(node.Subcommands, buildCommandTreeJSON(child))
}
return node
}