Skip to content

Commit 48d96ef

Browse files
committed
windows cmd.exe and powershell tweaks
1 parent 5f95a9d commit 48d96ef

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

exec/runner.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"github.com/sirupsen/logrus"
1313
)
1414

15+
// the default windows shell must be cmd.exe for compatibility with older Windows versions
1516
const defaultWindowsShell = "cmd.exe"
17+
1618
const defaultUnixShell = "sh"
1719

1820
type SystemAPI interface {
@@ -86,7 +88,15 @@ func (rm *RunnerMock) Run(execContext *Context) error {
8688
}
8789

8890
func (sr SystemRunner) Run(execContext *Context) error {
89-
tmpFile, err := ioutil.TempFile(os.TempDir(), "taco-")
91+
tmpPattern := "taco-*"
92+
if runtime.GOOS == "windows" {
93+
if execContext.Shell == defaultWindowsShell {
94+
tmpPattern = "taco-*.cmd"
95+
} else {
96+
tmpPattern = "taco-*.ps1"
97+
}
98+
}
99+
tmpFile, err := ioutil.TempFile(os.TempDir(), tmpPattern)
90100
if err != nil {
91101
return err
92102
}
@@ -130,16 +140,34 @@ func (sr SystemRunner) setWorkingDir(cmd *exec.Cmd, execContext *Context) {
130140
}
131141

132142
func (sr SystemRunner) createCmd(execContext *Context, tmpFile *os.File) (cmd *exec.Cmd, err error) {
133-
rawCmds := strings.Join(execContext.Cmds, "\n")
143+
prelude := ""
144+
newLine := "\n"
145+
146+
if runtime.GOOS == "windows" {
147+
newLine = "\r\n"
148+
if execContext.Shell == defaultWindowsShell {
149+
prelude = "@echo off" + newLine
150+
}
151+
}
152+
153+
rawCmds := prelude + strings.Join(execContext.Cmds, newLine)
154+
134155
if _, err := tmpFile.Write([]byte(rawCmds)); err != nil {
135156
return nil, err
136157
}
137158

159+
tmpFile.Close()
160+
138161
logrus.Debugf("WROTE TO FILE:\n%s\n----\n", rawCmds)
139162

140163
shellParam := sr.parseShellParam(execContext.Shell)
141164
cmdName, cmdArgs := sr.buildCmdParts(shellParam)
142-
cmdArgs = append(cmdArgs, tmpFile.Name())
165+
166+
if runtime.GOOS == "windows" && execContext.Shell == defaultWindowsShell {
167+
cmdName = tmpFile.Name()
168+
} else {
169+
cmdArgs = append(cmdArgs, tmpFile.Name())
170+
}
143171

144172
cmd = exec.Command(cmdName, cmdArgs...)
145173

0 commit comments

Comments
 (0)