-
-
Notifications
You must be signed in to change notification settings - Fork 580
/
commands.go
501 lines (437 loc) · 15.8 KB
/
commands.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package cmd
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/ddev/ddev/pkg/ddevapp"
"github.com/ddev/ddev/pkg/exec"
"github.com/ddev/ddev/pkg/fileutil"
"github.com/ddev/ddev/pkg/globalconfig"
"github.com/ddev/ddev/pkg/nodeps"
"github.com/ddev/ddev/pkg/util"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
)
const (
CustomCommand = "customCommand"
BundledCustomCommand = "customCommand:bundled"
)
func IsUserDefinedCustomCommand(cmd *cobra.Command) bool {
_, customCommand := cmd.Annotations[CustomCommand]
_, bundledCustomCommand := cmd.Annotations[BundledCustomCommand]
return customCommand && !bundledCustomCommand
}
// addCustomCommands looks for custom command scripts in
// ~/.ddev/commands/<servicename> etc. and
// .ddev/commands/<servicename> and .ddev/commands/host
// and if it finds them adds them to Cobra's commands.
func addCustomCommands(rootCmd *cobra.Command) error {
// Custom commands are shell scripts - so we can't use them on windows without bash.
if runtime.GOOS == "windows" {
windowsBashPath := util.FindBashPath()
if windowsBashPath == "" {
fmt.Println("Unable to find bash.exe in PATH, not loading custom commands")
return nil
}
}
// Keep a map so we don't add multiple commands with the same name.
commandsAdded := map[string]int{}
app, err := ddevapp.GetActiveApp("")
// If we're not running ddev inside a project directory, we should still add any host commands that can run without one.
if err != nil {
globalHostCommandPath := filepath.Join(globalconfig.GetGlobalDdevDir(), "commands", "host")
commandFiles, err := fileutil.ListFilesInDir(globalHostCommandPath)
if err != nil {
return err
}
err = addCustomCommandsFromDir(rootCmd, nil, globalHostCommandPath, commandFiles, true, commandsAdded)
if err != nil {
return err
}
return nil
}
projectCommandPath := app.GetConfigPath("commands")
// Make sure our target global command directory is empty
globalCommandPath := filepath.Join(globalconfig.GetGlobalDdevDir(), "commands")
err = os.MkdirAll(globalCommandPath, 0755)
if err != nil {
return err
}
for _, commandSet := range []string{projectCommandPath, globalCommandPath} {
commandDirs, err := fileutil.ListFilesInDirFullPath(commandSet)
if err != nil {
return err
}
for _, serviceDirOnHost := range commandDirs {
// If the item isn't a directory, skip it.
if !fileutil.IsDirectory(serviceDirOnHost) {
continue
}
// Skip hidden directories as well.
if strings.HasPrefix(filepath.Base(serviceDirOnHost), ".") {
continue
}
commandFiles, err := fileutil.ListFilesInDir(serviceDirOnHost)
if err != nil {
return err
}
err = addCustomCommandsFromDir(rootCmd, app, serviceDirOnHost, commandFiles, commandSet == globalCommandPath, commandsAdded)
if err != nil {
return err
}
}
}
return nil
}
// addCustomCommandsFromDir adds the custom commands from inside a given directory
func addCustomCommandsFromDir(rootCmd *cobra.Command, app *ddevapp.DdevApp, serviceDirOnHost string, commandFiles []string, isGlobalSet bool, commandsAdded map[string]int) error {
service := filepath.Base(serviceDirOnHost)
var err error
for _, commandName := range commandFiles {
onHostFullPath := filepath.Join(serviceDirOnHost, commandName)
if strings.HasSuffix(commandName, ".example") || strings.HasPrefix(commandName, "README") || strings.HasPrefix(commandName, ".") || fileutil.IsDirectory(onHostFullPath) {
continue
}
// If command has already been added, we won't work with it again.
if _, ok := commandsAdded[commandName]; ok {
continue
}
// Any command we find will want to be executable on Linux
// Note that this only affects host commands and project-level commands.
// Global container commands are made executable on `ddev start` instead.
_ = os.Chmod(onHostFullPath, 0755)
if hasCR, _ := fileutil.FgrepStringInFile(onHostFullPath, "\r\n"); hasCR {
util.Warning("Command '%s' contains CRLF, please convert to Linux-style linefeeds with dos2unix or another tool, skipping %s", commandName, onHostFullPath)
continue
}
directives := findDirectivesInScriptCommand(onHostFullPath)
var description, usage, example, projectTypes, osTypes, hostBinaryExists, dbTypes string
// Skip host commands that need a project if we aren't in a project directory.
if service == "host" && app == nil {
if val, ok := directives["CanRunGlobally"]; !ok || val != "true" {
continue
}
}
description = commandName
if val, ok := directives["Description"]; ok {
description = val
}
usage = commandName + " [flags] [args]"
if val, ok := directives["Usage"]; ok {
usage = val
}
if val, ok := directives["Example"]; ok {
example = " " + strings.ReplaceAll(val, `\n`, "\n ")
}
autocompleteTerms := []string{}
if val, ok := directives["AutocompleteTerms"]; ok {
if err = json.Unmarshal([]byte(val), &autocompleteTerms); err != nil {
util.Warning("Error '%s', command '%s' contains an invalid autocomplete args definition '%s', skipping adding terms", err, commandName, val)
}
}
// Init and import flags
var flags Flags
flags.Init(commandName, onHostFullPath)
disableFlags := true
if val, ok := directives["Flags"]; ok {
disableFlags = false
if err = flags.LoadFromJSON(val); err != nil {
util.Warning("Error '%s', command '%s' contains an invalid flags definition '%s', skipping add flags of %s", err, commandName, val, onHostFullPath)
}
}
// Import and handle ProjectTypes
if val, ok := directives["ProjectTypes"]; ok {
projectTypes = val
}
// Default is to exec with Bash interpretation (not raw)
execRaw := false
if val, ok := directives["ExecRaw"]; ok {
if val == "true" {
execRaw = true
}
}
// If ProjectTypes is specified and we aren't of that type, skip
if projectTypes != "" && (app == nil || !strings.Contains(projectTypes, app.Type)) {
continue
}
// Import and handle OSTypes
if val, ok := directives["OSTypes"]; ok {
osTypes = val
}
// If OSTypes is specified and we aren't on one of the specified OSes, skip
if osTypes != "" {
if !strings.Contains(osTypes, runtime.GOOS) && !(strings.Contains(osTypes, "wsl2") && nodeps.IsWSL2()) {
continue
}
}
// Import and handle HostBinaryExists
if val, ok := directives["HostBinaryExists"]; ok {
hostBinaryExists = val
}
// If hostBinaryExists is specified it doesn't exist here, skip
if hostBinaryExists != "" {
binExists := false
bins := strings.Split(hostBinaryExists, ",")
for _, bin := range bins {
if fileutil.FileExists(bin) {
binExists = true
break
}
}
if !binExists {
continue
}
}
// Import and handle DBTypes
if val, ok := directives["DBTypes"]; ok {
dbTypes = val
}
// If DBTypes is specified and we aren't using that DBTypes
if dbTypes != "" && app != nil {
if !strings.Contains(dbTypes, app.Database.Type) {
continue
}
}
// Create proper description suffix
descSuffix := " (shell " + service + " container command)"
if isGlobalSet {
descSuffix = " (global shell " + service + " container command)"
}
// Initialize the new command
commandToAdd := &cobra.Command{
Use: usage,
Short: description + descSuffix,
Example: example,
DisableFlagParsing: disableFlags,
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
ValidArgs: autocompleteTerms,
}
// Add flags to command
if err = flags.AssignToCommand(commandToAdd); err != nil {
util.Warning("Error '%s' in the flags definition for command '%s', skipping %s", err, commandName, onHostFullPath)
continue
}
// Execute the command matching the host working directory relative
// to the app root.
relative := false
if val, ok := directives["HostWorkingDir"]; ok {
if val == "true" {
relative = true
}
}
autocompletePathOnHost := filepath.Join(serviceDirOnHost, "autocomplete", commandName)
if service == "host" {
commandToAdd.Run = makeHostCmd(app, onHostFullPath, commandName)
if fileutil.FileExists(autocompletePathOnHost) {
// Make sure autocomplete script can be executed
_ = os.Chmod(autocompletePathOnHost, 0755)
if hasCR, _ := fileutil.FgrepStringInFile(autocompletePathOnHost, "\r\n"); hasCR {
util.Warning("Command '%s' contains CRLF, please convert to Linux-style linefeeds with dos2unix or another tool, skipping %s", commandName, onHostFullPath)
continue
}
// Add autocomplete script
commandToAdd.ValidArgsFunction = makeHostCompletionFunc(autocompletePathOnHost, commandToAdd)
}
} else {
// Use path.Join() for the container path because it's about the path in the container, not on the
// host; a Windows path is not useful here.
containerBasePath := path.Join("/mnt/ddev_config", filepath.Base(filepath.Dir(serviceDirOnHost)), service)
if strings.HasPrefix(serviceDirOnHost, globalconfig.GetGlobalDdevDir()) {
containerBasePath = path.Join("/mnt/ddev-global-cache/global-commands/", service)
}
inContainerFullPath := path.Join(containerBasePath, commandName)
commandToAdd.Run = makeContainerCmd(app, inContainerFullPath, commandName, service, execRaw, relative)
if fileutil.FileExists(autocompletePathOnHost) {
// Make sure autocomplete script can be executed
_ = os.Chmod(autocompletePathOnHost, 0755)
if hasCR, _ := fileutil.FgrepStringInFile(autocompletePathOnHost, "\r\n"); hasCR {
util.Warning("Autocomplete script for command '%s' contains CRLF, please convert to Linux-style linefeeds with dos2unix or another tool, skipping %s", commandName, autocompletePathOnHost)
continue
}
// Add autocomplete script
autocompletePathInContainer := path.Join(containerBasePath, "autocomplete", commandName)
commandToAdd.ValidArgsFunction = makeContainerCompletionFunc(autocompletePathInContainer, service, app, commandToAdd)
}
}
if disableFlags {
// Hide -h because we are disabling flags
// Also hide --json-output for the same reason
// @see https://github.com/spf13/cobra/issues/1328
commandToAdd.InitDefaultHelpFlag()
err = commandToAdd.Flags().MarkHidden("help")
originalHelpFunc := commandToAdd.HelpFunc()
if err == nil {
commandToAdd.SetHelpFunc(func(command *cobra.Command, strings []string) {
_ = command.Flags().MarkHidden("json-output")
originalHelpFunc(command, strings)
})
}
}
// Mark custom command
if commandToAdd.Annotations == nil {
commandToAdd.Annotations = map[string]string{}
}
commandToAdd.Annotations[CustomCommand] = "true"
if ddevapp.IsBundledCustomCommand(isGlobalSet, service, commandName) {
commandToAdd.Annotations[BundledCustomCommand] = "true"
}
// Add the command and mark as added
rootCmd.AddCommand(commandToAdd)
commandsAdded[commandName] = 1
}
return nil
}
func makeHostCompletionFunc(autocompletePathOnHost string, commandToAdd *cobra.Command) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Add quotes to an empty item, so it gets passed as an empty string to the script
if toComplete == "" {
toComplete = "''"
}
args = append(args, toComplete)
args = append([]string{commandToAdd.Name()}, args...)
result, err := exec.RunCommand(autocompletePathOnHost, args)
if err != nil {
cobra.CompDebugln("error: "+err.Error(), true)
return nil, cobra.ShellCompDirectiveDefault
}
// Turn result (which was separated by line breaks) into an array and return it to cobra to deal with
return strings.Split(strings.TrimSpace(result), "\n"), cobra.ShellCompDirectiveDefault
}
}
func makeContainerCompletionFunc(autocompletePathInContainer string, service string, app *ddevapp.DdevApp, commandToAdd *cobra.Command) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// Add quotes to an empty item, so it gets passed as an empty string to the script
if toComplete == "" {
toComplete = "''"
}
args = append(args, toComplete)
compWords := commandToAdd.Name() + " " + strings.Join(args, " ")
// Prepare docker exec command
opts := &ddevapp.ExecOpts{
Cmd: autocompletePathInContainer + " " + compWords,
Service: service,
Dir: app.GetWorkingDir(service, ""),
Tty: false,
NoCapture: false,
}
// Execute completion in docker container
result, stderr, err := app.Exec(opts)
if err != nil {
cobra.CompDebugln("error: "+stderr+","+err.Error(), true)
return nil, cobra.ShellCompDirectiveDefault
}
// Turn result (which was separated by line breaks) into an array and return it to cobra to deal with
return strings.Split(strings.TrimSpace(result), "\n"), cobra.ShellCompDirectiveDefault
}
}
// makeHostCmd creates a command which will run on the host
func makeHostCmd(app *ddevapp.DdevApp, fullPath, name string) func(*cobra.Command, []string) {
var windowsBashPath = ""
if runtime.GOOS == "windows" {
windowsBashPath = util.FindBashPath()
}
return func(_ *cobra.Command, _ []string) {
if app != nil {
status, _ := app.SiteStatus()
app.DockerEnv()
_ = os.Setenv("DDEV_PROJECT_STATUS", status)
} else {
_ = os.Setenv("DDEV_PROJECT_STATUS", "")
}
osArgs := []string{}
if len(os.Args) > 2 {
osArgs = os.Args[2:]
}
var err error
// Load environment variables that may be useful for script.
if app != nil {
app.DockerEnv()
}
if runtime.GOOS == "windows" {
// Sadly, not sure how to have a Bash interpreter without this.
args := []string{fullPath}
args = append(args, osArgs...)
err = exec.RunInteractiveCommand(windowsBashPath, args)
} else {
err = exec.RunInteractiveCommand(fullPath, osArgs)
}
if err != nil {
util.Failed("Failed to run %s %v; error=%v", name, strings.Join(osArgs, " "), err)
}
}
}
// makeContainerCmd creates the command which will app.Exec to a container command
func makeContainerCmd(app *ddevapp.DdevApp, fullPath, name, service string, execRaw bool, relative bool) func(*cobra.Command, []string) {
s := service
if s[0:1] == "." {
s = s[1:]
}
return func(_ *cobra.Command, _ []string) {
status, _ := app.SiteStatus()
if status != ddevapp.SiteRunning {
err := app.Start()
if err != nil {
util.Failed("Failed to start project for custom command: %v", err)
}
}
app.DockerEnv()
osArgs := []string{}
if len(os.Args) > 2 {
osArgs = os.Args[2:]
}
opts := &ddevapp.ExecOpts{
Cmd: fullPath + " " + strings.Join(osArgs, " "),
Service: s,
Dir: app.GetWorkingDir(s, ""),
Tty: isatty.IsTerminal(os.Stdin.Fd()),
NoCapture: true,
}
if relative {
opts.Dir = path.Join(opts.Dir, app.GetRelativeWorkingDirectory())
}
if execRaw {
opts.RawCmd = append([]string{fullPath}, osArgs...)
}
_, _, err := app.Exec(opts)
if err != nil {
util.Failed("Failed to run %s %v: %v", name, strings.Join(osArgs, " "), err)
}
}
}
// findDirectivesInScriptCommand() Returns a map of directives and their contents
// found in the named script
func findDirectivesInScriptCommand(script string) map[string]string {
f, err := os.Open(script)
if err != nil {
util.Failed("Failed to open %s: %v", script, err)
}
// nolint errcheck
defer f.Close()
var directives = make(map[string]string)
// Splits on newlines by default.
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "## ") && strings.Contains(line, ":") {
line = strings.Replace(line, "## ", "", 1)
parts := strings.SplitN(line, ":", 2)
if parts[0] == "Example" {
parts[1] = strings.Trim(parts[1], " ")
} else {
parts[1] = strings.Trim(parts[1], " \"'")
}
directives[parts[0]] = parts[1]
}
}
if err := scanner.Err(); err != nil {
return nil
}
return directives
}