Skip to content

Commit fc5a508

Browse files
committed
fix: run windows shell command
1 parent 447609e commit fc5a508

1 file changed

Lines changed: 27 additions & 9 deletions

File tree

internal/upstream/core/connection.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ func (c *Client) wrapWithUserShell(command string, args []string) (shellCommand
605605
shell, _ := c.envManager.GetSystemEnvVar("SHELL")
606606
if shell == "" {
607607
// Fallback to common shells based on OS
608-
if strings.Contains(strings.ToLower(command), "windows") {
608+
if runtime.GOOS == "windows" {
609609
shell = "cmd"
610610
} else {
611611
shell = pathBinBash // Default fallback
@@ -629,24 +629,42 @@ func (c *Client) wrapWithUserShell(command string, args []string) (shellCommand
629629
zap.String("shell", shell),
630630
zap.String("wrapped_command", commandString))
631631

632-
// Return shell with -l (login) flag to load user's full environment
633-
// The -c flag executes the command string
634-
return shell, []string{"-l", "-c", commandString}
632+
// Return shell with appropriate flags for the OS
633+
if runtime.GOOS == "windows" {
634+
// Windows cmd.exe uses /c flag to execute command string
635+
return shell, []string{"/c", commandString}
636+
} else {
637+
// Unix shells use -l (login) flag to load user's full environment
638+
// The -c flag executes the command string
639+
return shell, []string{"-l", "-c", commandString}
640+
}
635641
}
636642

637643
// shellescape escapes a string for safe shell execution
638644
func shellescape(s string) string {
639645
if s == "" {
646+
if runtime.GOOS == "windows" {
647+
return `""`
648+
}
640649
return "''"
641650
}
642651

643652
// If string contains no special characters, return as-is
644-
if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~") {
645-
return s
653+
if runtime.GOOS == "windows" {
654+
// Windows cmd.exe special characters
655+
if !strings.ContainsAny(s, " \t\n\r\"&|<>()^%") {
656+
return s
657+
}
658+
// For Windows, use double quotes and escape internal double quotes
659+
return `"` + strings.ReplaceAll(s, `"`, `\"`) + `"`
660+
} else {
661+
// Unix shell special characters
662+
if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~") {
663+
return s
664+
}
665+
// Use single quotes and escape any single quotes in the string
666+
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
646667
}
647-
648-
// Use single quotes and escape any single quotes in the string
649-
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
650668
}
651669

652670
// hasCommand checks if a command is available in PATH

0 commit comments

Comments
 (0)