quoteCommand returns the remote command to run using the ssh connection as a single string, quoting values where needed because ssh executes these in a POSIX shell.
(commandAndArgs ...string)
| 165 | // as a single string, quoting values where needed because ssh executes |
| 166 | // these in a POSIX shell. |
| 167 | func quoteCommand(commandAndArgs ...string) (string, error) { |
| 168 | var quotedCmd string |
| 169 | for i, arg := range commandAndArgs { |
| 170 | a, err := syntax.Quote(arg, syntax.LangPOSIX) |
| 171 | if err != nil { |
| 172 | return "", fmt.Errorf("invalid argument: %w", err) |
| 173 | } |
| 174 | if i == 0 { |
| 175 | quotedCmd = a |
| 176 | continue |
| 177 | } |
| 178 | quotedCmd += " " + a |
| 179 | } |
| 180 | // each part is quoted appropriately, so now we'll have a full |
| 181 | // shell command to pass off to "ssh" |
| 182 | return quotedCmd, nil |
| 183 | } |