|
4 | 4 | package commands
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "errors" |
7 | 8 | "fmt"
|
8 | 9 | "log"
|
9 | 10 | "os"
|
| 11 | + "os/exec" |
10 | 12 | "path"
|
11 | 13 | "runtime"
|
12 | 14 | "strings"
|
@@ -88,21 +90,42 @@ func Execute(customArgs []string) {
|
88 | 90 | }
|
89 | 91 |
|
90 | 92 | if cmd1 != nil && len(args1) > 0 {
|
91 |
| - |
92 | 93 | found := ""
|
93 | 94 | for _, plugin := range plugins {
|
94 |
| - if path.Base(plugin) == args1[0] { |
| 95 | + pluginName := args1[0] |
| 96 | + if runtime.GOOS == "windows" { |
| 97 | + pluginName = fmt.Sprintf("%s.exe", args1[0]) |
| 98 | + } |
| 99 | + |
| 100 | + if path.Base(plugin) == pluginName { |
95 | 101 | found = plugin
|
96 | 102 | }
|
97 | 103 | }
|
98 | 104 | if len(found) > 0 {
|
99 |
| - |
100 |
| - // if we have found the plugin then sysexec it by replacing current process. |
101 |
| - if err := syscall.Exec(found, append([]string{found}, os.Args[2:]...), os.Environ()); err != nil { |
102 |
| - fmt.Fprintf(os.Stderr, "Error from plugin: %v", err) |
103 |
| - os.Exit(127) |
| 105 | + // If we have found the plugin then sysexec it by replacing the current process. |
| 106 | + // On Windows we use the os/exec package to run the plugins since replacing the current |
| 107 | + // process with syscall.exec is not supported. |
| 108 | + if runtime.GOOS == "windows" { |
| 109 | + cmd := exec.Command(found, os.Args[2:]...) |
| 110 | + cmd.Stdout = os.Stdout |
| 111 | + cmd.Stderr = os.Stderr |
| 112 | + if err := cmd.Run(); err != nil { |
| 113 | + var exitErr *exec.ExitError |
| 114 | + if errors.As(err, &exitErr) { |
| 115 | + os.Exit(exitErr.ExitCode()) |
| 116 | + } else { |
| 117 | + fmt.Println("Error from plugin", err) |
| 118 | + os.Exit(127) |
| 119 | + } |
| 120 | + } |
| 121 | + return |
| 122 | + } else { |
| 123 | + if err := syscall.Exec(found, append([]string{found}, os.Args[2:]...), os.Environ()); err != nil { |
| 124 | + fmt.Fprintf(os.Stderr, "Error from plugin: %v", err) |
| 125 | + os.Exit(127) |
| 126 | + } |
| 127 | + return |
104 | 128 | }
|
105 |
| - return |
106 | 129 | }
|
107 | 130 | }
|
108 | 131 |
|
|
0 commit comments