-
Notifications
You must be signed in to change notification settings - Fork 219
/
list.go
139 lines (122 loc) · 3.05 KB
/
list.go
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
package main
import (
"os"
"slices"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/superfly/flyctl/internal/command/root"
)
func formatRawText(desc string) string {
return strings.ReplaceAll(desc, "\n", "⏎")
}
func newListCmd() *cobra.Command {
var maxDepth int
var printUsage bool
var printDescription bool
var outputFormat string
var flags bool
cmd := &cobra.Command{
Use: "list",
Short: "List flyctl commands",
RunE: func(cmd *cobra.Command, args []string) error {
output := newOutput(os.Stdout, outputFormat)
root := root.New()
header := []string{"Command", "Aliases", "Flags", "Details"}
if flags {
header = slices.Insert(header, 1, "Flag")
}
if printUsage {
header = append(header, "Usage")
}
if printDescription {
header = append(header, "Description")
}
output.SetHeader(header)
walkMaxDepth(root, maxDepth, func(cmd *cobra.Command, depth int) {
cols := []string{
cmd.CommandPath(),
strings.Join(cmd.Aliases, ", "),
strconv.Itoa(countFlags(cmd)),
commandAttrs(cmd),
}
if flags {
cols = slices.Insert(cols, 1, "")
}
if printUsage {
cols = append(cols, formatRawText(cmd.UseLine()))
}
if printDescription {
cols = append(cols, formatRawText(cmd.Short))
}
output.Append(cols)
if flags {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
cols = []string{
cmd.CommandPath(),
"--" + f.Name,
f.Shorthand,
"",
flagAttrs(f),
}
if printUsage {
cols = append(cols, f.Usage)
}
if printDescription {
cols = append(cols, "")
}
output.Append(cols)
})
}
})
return output.Flush()
},
}
cmd.Flags().IntVar(&maxDepth, "max-depth", -1, "Maximum depth to walk the command tree")
cmd.Flags().BoolVar(&printUsage, "usage", false, "Print usage for each command")
cmd.Flags().BoolVar(&printDescription, "description", false, "Print description for each command")
cmd.Flags().StringVar(&outputFormat, "output", "table", "Output format (table, csv, json)")
cmd.Flags().BoolVar(&flags, "flags", false, "Include flags for each command")
return cmd
}
func commandAttrs(cmd *cobra.Command) string {
attrs := []string{}
if cmd.GroupID != "" {
attrs = append(attrs, "group:"+cmd.GroupID)
}
if cmd.Deprecated != "" {
attrs = append(attrs, "deprecated")
}
if cmd.Hidden {
attrs = append(attrs, "hidden")
}
if !cmd.Runnable() {
attrs = append(attrs, "not_runnable")
}
if cmd.HasExample() {
attrs = append(attrs, "has_example")
}
slices.Sort(attrs)
return strings.Join(attrs, ", ")
}
func flagAttrs(f *pflag.Flag) string {
attrs := []string{}
if f.Deprecated != "" {
attrs = append(attrs, "deprecated")
}
if f.Hidden {
attrs = append(attrs, "hidden")
}
if f.ShorthandDeprecated != "" {
attrs = append(attrs, "shorthand_deprecated")
}
slices.Sort(attrs)
return strings.Join(attrs, ", ")
}
func countFlags(cmd *cobra.Command) (count int) {
cmd.Flags().VisitAll(func(f *pflag.Flag) {
count++
})
return
}