Created
September 24, 2024 11:50
-
-
Save MarioBinder/faca1b759be22083906f5d19fb1c5f55 to your computer and use it in GitHub Desktop.
skeema shellout for compiling in windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file contains shellout functionality that is specific to Windows. | |
//go:build windows | |
// +build windows | |
package shellout | |
import ( | |
"context" | |
"fmt" | |
"os/exec" | |
"regexp" | |
"strings" | |
) | |
func (c *Command) cmd() (*exec.Cmd, error) { | |
if c.timeout > 0 { | |
ctx, cancel := context.WithTimeout(context.Background(), c.timeout) | |
c.cancelFunc = cancel | |
// Use 'cmd.exe' for Windows command shell | |
return exec.CommandContext(ctx, "cmd.exe", "/C", c.command), nil | |
} | |
// If no timeout, use 'cmd.exe' without context | |
return exec.Command("cmd.exe", "/C", c.command), nil | |
} | |
// noQuotesNeeded is a regexp for detecting which variable values do not require | |
// escaping and quote-wrapping in escapeVarValue() | |
var noQuotesNeeded = regexp.MustCompile(`^[\w/@%=:.,+-]*$`) | |
// escapeVarValue takes a string and wraps it in double-quotes so that it will | |
// be interpreted as a single argument in a command line. On Windows, escaping | |
// is handled slightly differently. | |
func escapeVarValue(value string) string { | |
if noQuotesNeeded.MatchString(value) { | |
return value | |
} | |
// Escape by wrapping in double quotes, since single quotes do not behave the same in cmd.exe | |
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(value, `"`, `\"`)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment