-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import "strings" | ||
|
||
type remoteExecutionRunner struct { | ||
shell string | ||
command []string | ||
args []string | ||
directory string | ||
} | ||
|
||
func (runner *remoteExecutionRunner) run( | ||
cluster *distributedLock, | ||
setupCallback func(*remoteExecutionNode), | ||
) (*remoteExecution, error) { | ||
command := joinCommand(runner.command) | ||
|
||
if runner.shell != "" { | ||
command = wrapCommandIntoShell(command, runner.shell, runner.args) | ||
} | ||
|
||
if runner.directory != "" { | ||
command = "cd " + | ||
escapeCommandArgumentStrict(runner.directory) + " && " + | ||
"{ " + command + "; }" | ||
} | ||
|
||
return runRemoteExecution(cluster, command, setupCallback) | ||
} | ||
|
||
func wrapCommandIntoShell(command string, shell string, args []string) string { | ||
if shell == "" { | ||
return command | ||
} | ||
|
||
escapedArgs := []string{} | ||
for _, arg := range args { | ||
escapedArgs = append(escapedArgs, escapeCommandArgumentStrict(arg)) | ||
} | ||
|
||
return strings.Replace(shell, `{}`, command, -1) + | ||
" _ " + | ||
strings.Join(escapedArgs, " ") | ||
} | ||
|
||
func joinCommand(command []string) string { | ||
escapedParts := []string{} | ||
|
||
for _, part := range command { | ||
escapedParts = append(escapedParts, escapeCommandArgument(part)) | ||
} | ||
|
||
return strings.Join(escapedParts, ` `) | ||
} | ||
|
||
func escapeCommandArgument(argument string) string { | ||
argument = strings.Replace(argument, `\`, `\\`, -1) | ||
argument = strings.Replace(argument, ` `, `\ `, -1) | ||
|
||
return argument | ||
} | ||
|
||
func escapeCommandArgumentStrict(argument string) string { | ||
argument = strings.Replace(argument, `\`, `\\`, -1) | ||
argument = strings.Replace(argument, "`", "\\`", -1) | ||
argument = strings.Replace(argument, `"`, `\"`, -1) | ||
argument = strings.Replace(argument, `$`, `\$`, -1) | ||
|
||
return `"` + argument + `"` | ||
} |
Oops, something went wrong.