forked from rancher-sandbox/rancher-desktop
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_linux.go
More file actions
284 lines (260 loc) · 8.73 KB
/
main_linux.go
File metadata and controls
284 lines (260 loc) · 8.73 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// package main produces stubs for the nerdctl subcommands (and their
// options); this is expected to be overridden for options that involve paths.
// All options generated this will have their values ignored.
package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"runtime/debug"
"sort"
"strings"
"text/template"
"unicode"
"github.com/sirupsen/logrus"
)
// nerdctl contains the path to the nerdctl binary to run.
var nerdctl = "/usr/local/bin/nerdctl"
// outputPath is the file we should generate.
var outputPath = "../nerdctl_commands_generated.go"
type helpData struct {
// Commands lists the subcommands available
Commands []string
// options available for this command; the key is the long option
// (`--version`) or the short option (`-v`), and the value is whether the
// option takes an argument.
Options map[string]bool
// If set, this command can have subcommands; this alters argument parsing.
canHaveSubcommands bool
// If set, this command can pass flags to foreign commands, as in `nerdctl run`.
// This alters argument parsing by letting us ignore unknown flags.
HasForeignFlags bool
// mergedOptions includes local options plus inherited options.
mergedOptions map[string]struct{}
}
// prologueTemplate describes the file header for the generated file.
const prologueTemplate = `
// Code generated by {{ .package }} - DO NOT EDIT.
// package main implements a stub for nerdctl
package main
// commands supported by nerdctl; the key here is a space-separated subcommand
// path to reach the given subcommand (where the root command is empty).
var commands = map[string]commandDefinition {
`
// epilogueTemplate describes the file trailer for the generated file.
const epilogueTemplate = `
}
`
func main() {
verbose := flag.Bool("verbose", false, "extra logging")
flag.Parse()
if *verbose {
logrus.SetLevel(logrus.TraceLevel)
}
output, err := os.Create(outputPath)
if err != nil {
logrus.WithError(err).WithField("path", outputPath).Fatal("error creating output")
}
defer output.Close()
//nolint:dogsled // we only require the file name; we can also ignore `ok`, as
// on failure we just have no useful file name.
_, filename, _, _ := runtime.Caller(0)
data := map[string]interface{}{
"package": filename,
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
data["package"] = buildInfo.Main.Path
}
err = template.Must(template.New("").Parse(prologueTemplate)).Execute(output, data)
if err != nil {
logrus.WithError(err).Fatal("could not execute prologue")
}
err = buildSubcommand([]string{}, helpData{}, output)
if err != nil {
logrus.WithError(err).Fatal("could not build subcommands")
}
err = template.Must(template.New("").Parse(epilogueTemplate)).Execute(output, data)
if err != nil {
logrus.WithError(err).Fatal("could not execute epilogue")
}
}
// buildSubcommand generates the option parser data for a given subcommand.
// args provides the list of arguments to get to the subcommand; the last
// element in the slice is the name of the subcommand.
// writer is the file to write to for the result; it is expected that `go fmt`
// will be run on it eventually.
func buildSubcommand(args []string, parentData helpData, writer io.Writer) error {
help, err := getHelp(args)
if err != nil {
return fmt.Errorf("error getting help for %v: %w", args, err)
}
subcommands := parseHelp(help, parentData)
fields := logrus.Fields{"args": args}
if subcommands.HasForeignFlags {
fields["type"] = "arguments"
} else if !subcommands.canHaveSubcommands {
fields["type"] = "positional"
}
logrus.WithFields(fields).Trace("building subcommand")
if !subcommands.canHaveSubcommands && len(subcommands.Commands) > 0 {
return fmt.Errorf("invalid command %v: has positional arguments, but also subcommands %+v", args, subcommands.Commands)
}
if subcommands.canHaveSubcommands && subcommands.HasForeignFlags {
return fmt.Errorf("invalid command %v: has subcommands and foreign flags", args)
}
err = emitCommand(args, subcommands, writer)
if err != nil {
return err
}
for _, subcommand := range subcommands.Commands {
newArgs := make([]string, 0, len(args))
newArgs = append(newArgs, args...)
newArgs = append(newArgs, subcommand)
err := buildSubcommand(newArgs, subcommands, writer)
if err != nil {
return err
}
}
return nil
}
// getHelp runs `nerdctl <args...> -help` and returns the result.
func getHelp(args []string) (string, error) {
newArgs := make([]string, 0, len(args)+1)
newArgs = append(newArgs, args...)
newArgs = append(newArgs, "--help")
cmd := exec.Command(nerdctl, newArgs...)
cmd.Stderr = os.Stderr
result, err := cmd.Output()
if err != nil {
return "", err
}
return string(result), nil
}
const (
STATE_OTHER = iota
STATE_COMMANDS
STATE_OPTIONS
)
// parseHelp consumes the output of `nerdctl help` (possibly for a subcommand)
// and returns the available subcommands and options.
func parseHelp(help string, parentData helpData) helpData {
result := helpData{Options: make(map[string]bool), mergedOptions: make(map[string]struct{})}
for k := range parentData.mergedOptions {
result.mergedOptions[k] = struct{}{}
}
state := STATE_OTHER
for _, line := range strings.Split(help, "\n") {
line = strings.TrimRightFunc(line, unicode.IsSpace)
if line == "" {
// Skip empty lines (don't switch state either)
continue
}
if !strings.HasPrefix(line, " ") {
// Line does not start with a space; it's a section header.
if strings.HasSuffix(strings.ToUpper(line), "COMMANDS:") {
state = STATE_COMMANDS
} else if strings.HasSuffix(strings.ToUpper(line), "FLAGS:") {
state = STATE_OPTIONS
} else if strings.HasPrefix(strings.ToUpper(line), "USAGE:") {
// Usage is on the same line, so we have to process this now.
// Command is `nerdctl subcommand [flags]`; anything after `[flags]` is
// assumed to be positional arguments.
_, newArgs, _ := strings.Cut(strings.ToUpper(line), "[FLAGS]")
newArgs = strings.TrimSpace(newArgs)
// Unlike everything else, `nerdctl compose` has a usage string of
// `nerdctl compose [flags] COMMAND` so we need to ignore that.
if newArgs == "" || newArgs == "COMMAND" {
result.canHaveSubcommands = true
} else if strings.Contains(newArgs, "COMMAND") && strings.Contains(newArgs, "...") {
result.HasForeignFlags = true
}
} else {
state = STATE_OTHER
}
continue
}
line = strings.TrimLeftFunc(line, unicode.IsSpace)
if state == STATE_COMMANDS {
parts := strings.SplitN(line, " ", 2)
if len(parts) < 2 {
// This line does not contain a command.
continue
}
words := strings.Split(strings.TrimSpace(parts[0]), ", ")
result.Commands = append(result.Commands, words...)
} else if state == STATE_OPTIONS {
parts := strings.SplitN(line, " ", 2)
if len(parts) < 2 {
// This line does not contain an option.
continue
}
// The flags help has the format: `-f, --foo string Description`
// In order to figure out if the option takes arguments, we need to
// parse the whole line first.
var words []string
hasOptions := false
for _, word := range strings.Split(strings.TrimSpace(parts[0]), ", ") {
spaceIndex := strings.Index(word, " ")
if spaceIndex > -1 {
hasOptions = true
word = word[:spaceIndex]
}
words = append(words, word)
}
// We may find an inherited flag; skip if the long option exists in
// the parent
if len(words) < 1 {
continue
}
if _, ok := parentData.mergedOptions[words[len(words)-1]]; !ok {
for _, word := range words {
result.Options[word] = hasOptions
result.mergedOptions[word] = struct{}{}
}
}
}
}
sort.Strings(result.Commands)
return result
}
// commandTemplate is the text/template template for a single subcommand.
const commandTemplate = `
{{ printf "%q" .Args }}: {
commandPath: {{ printf "%q" .Args }},
subcommands: map[string]struct{} {
{{- range .Data.Commands }}
{{ printf "%q" . }}: {},
{{- end }}
},
options: map[string]argHandler {
{{ range $k, $v := .Data.Options }}
{{- printf "%q" $k -}}: {{ if $v -}} ignoredArgHandler {{- else -}} nil {{- end -}},
{{ end }}
},
{{- if .Data.HasForeignFlags }}
hasForeignFlags: true,
{{- end }}
},
`
// commandTemplateInput describes the data that will be fed to commandTemplate.
type commandTemplateInput struct {
Args string
Data helpData
}
// emitCommand outputs the golang code to the given writer. args indicates the
// arguments to reach this subcommand, and data is the parsed help output.
func emitCommand(args []string, data helpData, writer io.Writer) error {
templateData := commandTemplateInput{
Args: strings.Join(args, " "),
Data: data,
}
tmpl := template.Must(template.New("").Parse(commandTemplate))
err := tmpl.Execute(writer, templateData)
if err != nil {
return err
}
return nil
}