Skip to content

Commit a627a65

Browse files
authored
Merge pull request #992 from smallstep/herman/find-more-plugins-on-windows
Find more plugins on Windows
2 parents 02fd73b + 2fb162f commit a627a65

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

internal/plugin/plugin.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os/exec"
66
"path/filepath"
77
"runtime"
8+
"strings"
89

910
"github.com/urfave/cli"
1011
"go.step.sm/cli-utils/step"
@@ -19,7 +20,22 @@ func LookPath(name string) (string, error) {
1920
fileName := "step-" + name + "-plugin"
2021
switch runtime.GOOS {
2122
case "windows":
22-
for _, ext := range []string{".com", ".exe", ".bat", ".cmd"} {
23+
var exts []string
24+
x := os.Getenv(`PATHEXT`)
25+
if x != "" {
26+
for _, e := range strings.Split(strings.ToLower(x), `;`) {
27+
if e == "" {
28+
continue
29+
}
30+
if e[0] != '.' {
31+
e = "." + e
32+
}
33+
exts = append(exts, e)
34+
}
35+
} else {
36+
exts = []string{".com", ".exe", ".bat", ".cmd", ".ps1"}
37+
}
38+
for _, ext := range exts {
2339
path := filepath.Join(step.BasePath(), "plugins", fileName+ext)
2440
if _, err := os.Stat(path); err == nil {
2541
return path, nil
@@ -38,8 +54,16 @@ func LookPath(name string) (string, error) {
3854
// it to complete.
3955
func Run(ctx *cli.Context, file string) error {
4056
args := ctx.Args()
41-
//nolint:gosec // arguments controlled by step.
42-
cmd := exec.Command(file, args[1:]...)
57+
cmdName := file
58+
59+
// if running on Windows and (likely) a PowerShell script, invoke powershell
60+
// with the arguments instead of the plugin file directly.
61+
if runtime.GOOS == "windows" && strings.ToLower(filepath.Ext(file)) == ".ps1" {
62+
cmdName = "powershell"
63+
args = append([]string{args[0], "-noprofile", "-nologo", file}, args[1:]...)
64+
}
65+
66+
cmd := exec.Command(cmdName, args[1:]...)
4367
cmd.Stdin = os.Stdin
4468
cmd.Stdout = os.Stdout
4569
cmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)