Skip to content

Commit 7f14531

Browse files
committed
Add support or command as string and array
eg. exec: - type: local command: date +%s - type: local command: - date - +%s Fix also shell escaping
1 parent 2fd69c9 commit 7f14531

8 files changed

+94
-25
lines changed

sync/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
var waitGroup sync.WaitGroup
99

10-
1110
type Filter struct {
1211
Exclude []string
1312
excludeRegexp []*regexp.Regexp
@@ -62,7 +61,7 @@ type Database struct {
6261

6362
type Execution struct {
6463
Type string
65-
Command string
64+
Command YamlStringArray
6665
Workdir string
6766
When string
6867
}

sync/connection.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ import (
77
)
88

99
func (connection *Connection) CommandBuilder(command string, args ...string) []interface{} {
10+
//args = shell.QuoteValues(args...)
11+
12+
return connection.RawCommandBuilder(command, args...)
13+
}
14+
15+
func (connection *Connection) RawCommandBuilder(command string, args ...string) []interface{} {
1016
var ret []interface{}
1117

1218
if connection.WorkDir != "" {
1319
shellArgs := []string{command}
1420
shellArgs = append(shellArgs, args...)
15-
return connection.ShellCommandBuilder(shellArgs...)
21+
return connection.RawShellCommandBuilder(shellArgs...)
1622
}
1723

1824
switch connection.GetType() {
@@ -32,6 +38,11 @@ func (connection *Connection) CommandBuilder(command string, args ...string) []i
3238
}
3339

3440
func (connection *Connection) ShellCommandBuilder(args ...string) []interface{} {
41+
args = shell.QuoteValues(args...)
42+
return connection.RawShellCommandBuilder(args...)
43+
}
44+
45+
func (connection *Connection) RawShellCommandBuilder(args ...string) []interface{} {
3546
var ret []interface{}
3647

3748
inlineArgs := []string{}
@@ -40,12 +51,14 @@ func (connection *Connection) ShellCommandBuilder(args ...string) []interface{}
4051
inlineArgs = append(inlineArgs, val)
4152
}
4253

43-
inlineCommand := shell.Quote(strings.Join(inlineArgs, " "))
54+
inlineCommand := strings.Join(inlineArgs, " ")
4455

4556
if connection.WorkDir != "" {
4657
inlineCommand = fmt.Sprintf("cd %s ; %s", shell.Quote(connection.WorkDir), inlineCommand)
4758
}
4859

60+
inlineCommand = shell.Quote(inlineCommand)
61+
4962
switch connection.GetType() {
5063
case "local":
5164
ret = connection.LocalCommandBuilder("/bin/sh", "-c", inlineCommand)

sync/connection_ssh.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@ package sync
22

33
import (
44
"fmt"
5+
"strings"
6+
"github.com/webdevops/go-shell"
57
)
68

79
func (connection *Connection) SshCommandBuilder(command string, args ...string) []interface{} {
10+
remoteCmdParts := []string{command}
11+
for _, val := range args {
12+
remoteCmdParts = append(remoteCmdParts, val)
13+
}
14+
remoteCmd := shell.Quote(strings.Join(remoteCmdParts, " "))
15+
816
sshArgs := []string{
917
"-oBatchMode=yes",
1018
"-oPasswordAuthentication=no",
1119
connection.SshConnectionHostnameString(),
1220
"--",
13-
command,
14-
}
15-
16-
for _, val := range args {
17-
sshArgs = append(sshArgs, val)
21+
remoteCmd,
1822
}
1923

2024
return ShellCommandInterfaceBuilder("ssh", sshArgs...)

sync/database_remote.go

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package sync
22

33
import (
4-
"github.com/webdevops/go-shell"
54
"strings"
5+
"github.com/webdevops/go-shell"
66
)
77

88
func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, useFilter bool) []interface{} {
@@ -43,10 +43,10 @@ func (database *Database) remoteMysqldumpCmdBuilder(additionalArgs []string, use
4343
}
4444

4545
cmd := []string{"mysqldump"}
46-
cmd = append(cmd, args...)
47-
cmd = append(cmd, "|", "gzip --stdout")
46+
cmd = append(cmd, shell.QuoteValues(args...)...)
47+
cmd = append(cmd, "|", "gzip", "--stdout")
4848

49-
return database.remoteConnection.ShellCommandBuilder(cmd...)
49+
return database.remoteConnection.RawShellCommandBuilder(cmd...)
5050
}
5151

5252
func (database *Database) remoteMysqlCmdBuilder(args ...string) []interface{} {
@@ -99,11 +99,7 @@ func (database *Database) remoteMysqlCmdBuilderUncompress(args ...string) []inte
9999
args = append(args, database.Schema)
100100
}
101101

102-
for key, val := range args {
103-
args[key] = shell.Quote(val)
104-
}
105-
106-
cmd := []string{"gunzip", "--stdout", "|", "mysql", strings.Join(args, " ")}
102+
cmd := []string{"gunzip", "--stdout", "|", "mysql", strings.Join(shell.QuoteValues(args...), " ")}
107103

108-
return database.remoteConnection.ShellCommandBuilder(strings.Join(cmd, " "))
104+
return database.remoteConnection.RawShellCommandBuilder(cmd...)
109105
}

sync/execution.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ func (execution *Execution) String(server *Server) string {
1515
if execution.Workdir != "" {
1616
parts = append(parts, fmt.Sprintf("Workdir:%s", execution.Workdir))
1717
}
18-
parts = append(parts, fmt.Sprintf("Command:%s", execution.Command))
18+
parts = append(parts, fmt.Sprintf("Command:%s", execution.Command.ToString(" ")))
1919

2020
return fmt.Sprintf("Exec[%s]", strings.Join(parts[:]," "))
2121
}
2222

2323
func (execution *Execution) Execute(server *Server) {
24-
shell.Cmd(execution.commandBuilder(server)...)
24+
cmd := execution.commandBuilder(server)
25+
run := shell.Cmd(cmd...).Run()
26+
27+
Logger.Verbose(run.Stdout.String())
2528
}
2629

2730
func (execution *Execution) commandBuilder(server *Server) []interface{} {
@@ -38,7 +41,13 @@ func (execution *Execution) commandBuilder(server *Server) []interface{} {
3841
connection.WorkDir = execution.Workdir
3942
}
4043

41-
return connection.CommandBuilder(execution.Command)
44+
if len(execution.Command.Multi) >= 1 {
45+
// multi element command (use safer quoting)
46+
return connection.ShellCommandBuilder(execution.Command.Multi...)
47+
} else {
48+
// single string command (use as is)
49+
return connection.RawShellCommandBuilder(execution.Command.Single)
50+
}
4251
}
4352

4453
func (execution *Execution) GetType() string {

sync/helper.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ShellCommandInterfaceBuilder(command string, args ...string) []interface{}
1919
}
2020

2121
for _, val := range args {
22-
cmd = append(cmd, shell.Quote(val))
22+
cmd = append(cmd, val)
2323
}
2424

2525
shellCmd := make([]interface{}, len(cmd))
@@ -99,6 +99,8 @@ func ShellErrorHandler(recover interface{}) {
9999
printMessage("Stdout", process.Stdout.String())
100100
printMessage("Stderr", process.Stderr.String())
101101
printMessage("Exit code", fmt.Sprintf("%d", p))
102+
103+
os.Exit(2)
102104
}
103105
}
104106
}

sync/server_deploy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package sync
22

33
func (server *Server) Deploy() {
44
defer func() {
5-
//recover := recover()
6-
//ShellErrorHandler(recover)
5+
recover := recover()
6+
ShellErrorHandler(recover)
77
}()
88

99
server.RunExec("startup")

sync/yaml.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sync
2+
3+
import "strings"
4+
5+
type YamlStringArray struct {
6+
Multi []string
7+
Single string
8+
}
9+
10+
func (ysa *YamlStringArray) UnmarshalYAML(unmarshal func(interface{}) error) error {
11+
var multi []string
12+
err := unmarshal(&multi)
13+
if err != nil {
14+
var single string
15+
err := unmarshal(&single)
16+
if err != nil {
17+
return err
18+
}
19+
ysa.Single = single
20+
} else {
21+
ysa.Multi = multi
22+
}
23+
return nil
24+
}
25+
26+
func (ysa *YamlStringArray) String() string {
27+
return ysa.ToString(";")
28+
}
29+
30+
func (ysa *YamlStringArray) ToString(sep string) string {
31+
if len(ysa.Multi) >= 1 {
32+
return strings.Join(ysa.Multi, sep)
33+
} else {
34+
return ysa.Single
35+
}
36+
}
37+
38+
func (ysa *YamlStringArray) Array() []string {
39+
if len(ysa.Multi) >= 1 {
40+
return ysa.Multi
41+
} else if ysa.Single != "" {
42+
return []string{ysa.Single}
43+
} else {
44+
return []string{}
45+
}
46+
}

0 commit comments

Comments
 (0)