Skip to content

Commit

Permalink
bugfix: fix sudo -E '' with an helm cmd has single quotation (#4427)
Browse files Browse the repository at this point in the history
  • Loading branch information
daymade committed Dec 19, 2023
1 parent 258e565 commit 48f9c4b
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/ssh/sshcmd.go
Expand Up @@ -38,10 +38,15 @@ func (c *Client) Ping(host string) error {
}

func (c *Client) wrapCommands(cmds ...string) string {
cmdJoined := strings.Join(cmds, "; ")
if !c.Option.sudo || c.Option.user == defaultUsername {
return strings.Join(cmds, "; ")
return cmdJoined
}
return fmt.Sprintf("sudo -E /bin/bash -c '%s'", strings.Join(cmds, "; "))

// Escape single quotes in cmd, fix https://github.com/labring/sealos/issues/4424
// e.g. echo 'hello world' -> `sudo -E /bin/bash -c 'echo "hello world"'`
cmdEscaped := strings.ReplaceAll(cmdJoined, `'`, `"`)
return fmt.Sprintf("sudo -E /bin/bash -c '%s'", cmdEscaped)
}

func (c *Client) CmdAsyncWithContext(ctx context.Context, host string, cmds ...string) error {
Expand Down

0 comments on commit 48f9c4b

Please sign in to comment.