Skip to content

Commit 75a63f8

Browse files
authored
Fix powershell command to execute step on windows (#22)
1 parent 9a70dc3 commit 75a63f8

File tree

1 file changed

+71
-69
lines changed

1 file changed

+71
-69
lines changed

plugin/harness/execer.go

Lines changed: 71 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -79,85 +79,87 @@ func (e *Execer) Exec(ctx context.Context) error {
7979
// execute the plugin. the execution logic differs
8080
// based on programming language.
8181
if source := out.Run.Binary.Source; source != "" {
82-
parsedURL, err := NewMetadata(source, e.Ref).Generate()
83-
if err != nil {
84-
return err
85-
}
86-
binpath, err := file.Download(parsedURL)
87-
if err != nil {
88-
return err
89-
}
82+
return e.runSourceExecutable(ctx, out.Run.Binary.Source)
83+
} else if module := out.Run.Go.Module; module != "" {
84+
return e.runGoExecutable(ctx, module)
85+
} else {
86+
return e.runShellExecutable(ctx, out)
87+
}
9088

91-
if e.DownloadOnly {
92-
slog.Info("Download only flag is set. Not executing the plugin")
93-
return nil
94-
}
89+
return nil
90+
}
9591

96-
var cmds []*exec.Cmd
97-
if runtime.GOOS != "windows" {
98-
cmds = append(cmds, exec.Command("chmod", "+x", binpath))
99-
}
100-
cmds = append(cmds, exec.Command(binpath))
101-
err = runCmds(ctx, cmds, e.Environ, e.Workdir, e.Stdout, e.Stderr)
102-
if err != nil {
103-
return err
104-
}
105-
} else if module := out.Run.Go.Module; module != "" {
106-
// if the plugin is a Go module
107-
binpath, err := e.buildGoExecutable(ctx, module)
108-
if err != nil {
109-
return err
110-
}
92+
func (e *Execer) runSourceExecutable(ctx context.Context, source string) error {
93+
parsedURL, err := NewMetadata(source, e.Ref).Generate()
94+
if err != nil {
95+
return err
96+
}
97+
binpath, err := file.Download(parsedURL)
98+
if err != nil {
99+
return err
100+
}
111101

112-
if e.DownloadOnly {
113-
slog.Info("Download only flag is set. Not executing the plugin")
114-
return nil
115-
}
102+
if e.DownloadOnly {
103+
slog.Info("Download only flag is set. Not executing the plugin")
104+
return nil
105+
}
116106

117-
slog.Debug("go run", slog.String("module", module))
118-
// execute the binary
119-
cmd := exec.Command(binpath)
120-
err = runCmds(ctx, []*exec.Cmd{cmd}, e.Environ, e.Workdir, e.Stdout, e.Stderr)
121-
if err != nil {
122-
return err
123-
}
124-
} else {
125-
if e.DownloadOnly {
126-
slog.Info("Download only flag is set. Not executing the plugin")
127-
return nil
128-
}
107+
var cmds []*exec.Cmd
108+
if runtime.GOOS != "windows" {
109+
cmds = append(cmds, exec.Command("chmod", "+x", binpath))
110+
}
111+
cmds = append(cmds, exec.Command(binpath))
112+
return runCmds(ctx, cmds, e.Environ, e.Workdir, e.Stdout, e.Stderr)
113+
}
114+
115+
func (e *Execer) runGoExecutable(ctx context.Context, module string) error {
116+
// if the plugin is a Go module
117+
binpath, err := e.buildGoExecutable(ctx, module)
118+
if err != nil {
119+
return err
120+
}
129121

130-
var path, shell string
131-
// if the bash shell does not exist fallback
132-
// to posix shell.
133-
switch runtime.GOOS {
134-
case "windows":
135-
// TODO we may want to disable profile and interactive mode
136-
// when executing powershell scripts -noprofile -noninteractive
137-
shell = "powershell"
138-
path = filepath.Join(e.Source, out.Run.Pwsh.Path)
139-
140-
case "linux", "darwin":
141-
shell = "bash"
142-
path = filepath.Join(e.Source, out.Run.Bash.Path)
143-
144-
// fallback to the posix shell if bash
145-
// is not available on the host.
146-
if _, err := exec.LookPath("bash"); err != nil {
147-
shell = "sh"
148-
}
122+
if e.DownloadOnly {
123+
slog.Info("Download only flag is set. Not executing the plugin")
124+
return nil
125+
}
126+
127+
slog.Debug("go run", slog.String("module", module))
128+
// execute the binary
129+
cmd := exec.Command(binpath)
130+
return runCmds(ctx, []*exec.Cmd{cmd}, e.Environ, e.Workdir, e.Stdout, e.Stderr)
131+
}
132+
133+
func (e *Execer) runShellExecutable(ctx context.Context, out *spec) error {
134+
if e.DownloadOnly {
135+
slog.Info("Download only flag is set. Not executing the plugin")
136+
return nil
137+
}
138+
139+
switch runtime.GOOS {
140+
case "windows":
141+
// TODO we may want to disable profile and interactive mode
142+
// when executing powershell scripts -noprofile -noninteractive
143+
path := filepath.Join(e.Source, out.Run.Pwsh.Path)
144+
slog.Debug("execute", slog.String("file", path))
145+
cmd := exec.Command("pwsh", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; %s", path)
146+
return runCmds(ctx, []*exec.Cmd{cmd}, e.Environ, e.Workdir, e.Stdout, e.Stderr)
147+
case "linux", "darwin":
148+
path := filepath.Join(e.Source, out.Run.Bash.Path)
149+
150+
// fallback to the posix shell if bash
151+
// is not available on the host.
152+
shell := "bash"
153+
if _, err := exec.LookPath("bash"); err != nil {
154+
shell = "sh"
149155
}
150156
slog.Debug("execute", slog.String("file", path))
151157

152-
// execute the binary
153158
cmd := exec.Command(shell, path)
154-
err = runCmds(ctx, []*exec.Cmd{cmd}, e.Environ, e.Workdir, e.Stdout, e.Stderr)
155-
if err != nil {
156-
return err
157-
}
159+
return runCmds(ctx, []*exec.Cmd{cmd}, e.Environ, e.Workdir, e.Stdout, e.Stderr)
160+
default:
161+
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
158162
}
159-
160-
return nil
161163
}
162164

163165
func (e *Execer) buildGoExecutable(ctx context.Context, module string) (

0 commit comments

Comments
 (0)