-
Notifications
You must be signed in to change notification settings - Fork 150
/
cobra_usage_builder.go
204 lines (169 loc) · 5.45 KB
/
cobra_usage_builder.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package core
import (
"bytes"
"context"
"fmt"
"io"
"sort"
"strings"
"text/tabwriter"
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/spf13/cobra"
)
const (
sliceSchema = "{index}"
mapSchema = "{key}"
)
func buildUsageAliases(ctx context.Context, cmd *Command) string {
var aliasesBuffer bytes.Buffer
tw := tabwriter.NewWriter(&aliasesBuffer, 0, 0, 2, ' ', 0)
// Copy and sort alias list
aliases := make([]string, len(cmd.Aliases))
copy(aliases, cmd.Aliases)
sort.Strings(aliases)
aliasCfg := ExtractAliases(ctx)
for _, aliasName := range aliases {
_, _ = fmt.Fprintf(tw, " %s\t%s\n", aliasName, strings.Join(aliasCfg.GetAlias(aliasName), " "))
}
tw.Flush()
aliasesStr := strings.TrimSuffix(aliasesBuffer.String(), "\n")
return aliasesStr
}
// BuildUsageArgs builds usage args string.
// If deprecated is true, true only deprecated argSpecs will be considered.
// This string will be used by cobra usage template.
func BuildUsageArgs(ctx context.Context, cmd *Command, deprecated bool) string {
var argsBuffer bytes.Buffer
tw := tabwriter.NewWriter(&argsBuffer, 0, 0, 3, ' ', 0)
// Filter deprecated argSpecs.
argSpecs := cmd.ArgSpecs.GetDeprecated(deprecated)
err := _buildUsageArgs(ctx, tw, argSpecs)
if err != nil {
// TODO: decide how to handle this error
err = fmt.Errorf("building %v: %v", cmd.getPath(), err)
logger.Debugf("%v", err)
}
tw.Flush()
paramsStr := strings.TrimSuffix(argsBuffer.String(), "\n")
return paramsStr
}
// _buildUsageArgs builds the arg usage list.
// This should not be called directly.
func _buildUsageArgs(ctx context.Context, w io.Writer, argSpecs ArgSpecs) error {
inOneOfGroup := false
lastOneOfGroup := ""
for _, argSpec := range argSpecs {
argSpecUsageLeftPart := argSpec.Name
argSpecUsageRightPart := _buildArgShort(argSpec)
if argSpec.OneOfGroup != "" {
if argSpec.OneOfGroup != lastOneOfGroup {
inOneOfGroup = true
lastOneOfGroup = argSpec.OneOfGroup
_, err := fmt.Fprintf(w, " %s (one of):\n", argSpec.OneOfGroup)
if err != nil {
return err
}
}
} else {
inOneOfGroup = false
lastOneOfGroup = ""
}
if argSpec.Default != nil {
_, doc := argSpec.Default(ctx)
argSpecUsageLeftPart = fmt.Sprintf("%s=%s", argSpecUsageLeftPart, doc)
}
if !argSpec.Required && !argSpec.Positional {
argSpecUsageLeftPart = fmt.Sprintf("[%s]", argSpecUsageLeftPart)
}
if argSpec.CanLoadFile {
argSpecUsageRightPart += " (Support file loading with @/path/to/file)"
}
if inOneOfGroup {
argSpecUsageLeftPart = " " + argSpecUsageLeftPart
}
_, err := fmt.Fprintf(w, " %s\t%s\n", argSpecUsageLeftPart, argSpecUsageRightPart)
if err != nil {
return err
}
}
return nil
}
// _buildArgShort builds the arg short string.
// This should not be called directly.
func _buildArgShort(as *ArgSpec) string {
if len(as.EnumValues) > 0 {
return fmt.Sprintf("%s (%s)", as.Short, strings.Join(as.EnumValues, " | "))
}
return as.Short
}
// buildExamples builds usage examples string.
// This string will be used by cobra usage template.
func buildExamples(binaryName string, cmd *Command) string {
// Build the examples array.
examples := make([]string, 0, len(cmd.Examples))
for _, cmdExample := range cmd.Examples {
// Build title.
title := fmt.Sprintf(" %s", cmdExample.Short)
commandLine := cmdExample.GetCommandLine(binaryName, cmd)
commandLine = interactive.Indent(commandLine, 4)
commandLine = strings.Trim(commandLine, "\n")
// Add the whole example as a single string.
exampleLines := []string{
title,
commandLine,
}
examples = append(examples, strings.Join(exampleLines, "\n"))
}
// Return a single string for all examples.
return strings.Join(examples, "\n\n")
}
// usageFuncBuilder returns the usage function that will be used by cobra to print usage,
// the builder also takes a function that will fill annotations used by the usage template,
// this is done like this to avoid build annotations for each command if not required
func usageFuncBuilder(cmd *cobra.Command, annotationBuilder func()) func(*cobra.Command) error {
return func(command *cobra.Command) error {
annotationBuilder()
// after building annotation we remove this function as we prefer to use default UsageFunc
cmd.SetUsageFunc(nil)
return cmd.UsageFunc()(command)
}
}
func orderCobraCommands(cobraCommands []*cobra.Command) []*cobra.Command {
commands := make([]*cobra.Command, len(cobraCommands))
copy(commands, cobraCommands)
sort.Slice(commands, func(i, j int) bool {
deprecatedI := commands[i].Deprecated != ""
deprecatedJ := commands[j].Deprecated != ""
if deprecatedI == deprecatedJ {
return commands[i].Use < commands[j].Use
}
return !deprecatedI && deprecatedJ
})
return commands
}
func orderCobraGroups(cobraGroups []*cobra.Group) []*cobra.Group {
groups := make([]*cobra.Group, len(cobraGroups))
copy(groups, cobraGroups)
sort.Slice(groups, func(i, j int) bool {
return groups[i].Title < groups[j].Title
})
return groups
}
func getCobraCommandsGroups(cobraCommands []*cobra.Command) []*cobra.Group {
var groups []*cobra.Group
addedGroups := make(map[string]struct{})
for _, cobraCommand := range cobraCommands {
if !cobraCommand.IsAvailableCommand() {
continue
}
for _, group := range cobraCommand.Groups() {
if _, ok := addedGroups[group.ID]; ok {
continue
}
addedGroups[group.ID] = struct{}{}
groups = append(groups, group)
}
}
return groups
}