-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
89 lines (76 loc) · 2.17 KB
/
main.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/dasginganinja/drush-launcher/drushlauncher"
)
func main() {
// Parse command-line flags
altRoot := ""
// Strip program name from arguments before looping
progArgs := os.Args[1:]
for i, arg := range progArgs {
// If we have -r or --root we will use the next argument as the drupal root (if exists)
if arg == "-r" || arg == "--root" {
if i+1 < len(progArgs) {
altRoot = progArgs[i+1]
break // no need to loop when we find the root
} else {
fmt.Println("Error: Missing value for root argument")
os.Exit(1)
}
}
// If we have --root={altRoot}
if strings.HasPrefix(arg, "--root=") {
altRoot = strings.Replace(arg, "--root=", "", 1)
break // no need to loop when we find the root
}
}
var drupalRoot string
cwd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current directory:", err)
os.Exit(1)
}
// Use the alternative Drupal root if provided
if altRoot != "" {
var err error
drupalRoot, err = drushlauncher.FindDrushExecutable(altRoot)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
// If no alternative root provided, find the Drupal root from the current directory
drupalRoot, err = drushlauncher.FindDrushExecutable(cwd)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// Construct the full path to the drush executable
drushExec := filepath.Join(drupalRoot, "vendor", "bin", "drush")
// Check if the drush executable exists
if _, err := os.Stat(drushExec); os.IsNotExist(err) {
fmt.Println("Error: Drush executable not found at", drushExec)
os.Exit(1)
}
// Construct the full command to run drush
drushCmd := exec.Command(drushExec, progArgs...)
// Pass the current environment variables to the drush command
drushCmd.Env = os.Environ()
// Set the correct working directory for the drush command
drushCmd.Dir = cwd
// Redirect standard input/output/error for the drush command
drushCmd.Stdin = os.Stdin
drushCmd.Stdout = os.Stdout
drushCmd.Stderr = os.Stderr
// Run the drush command
if err := drushCmd.Run(); err != nil {
fmt.Println("Error executing drush:", err)
os.Exit(1)
}
}