diff --git a/pkg/config/command.go b/pkg/config/command.go index fbe25792..a94b383d 100644 --- a/pkg/config/command.go +++ b/pkg/config/command.go @@ -106,15 +106,15 @@ func (cmd *Cmd) scriptCommand(inp string) string { // add environment variables envs := cmd.genEnv() - res := "sh -c \"" + res := "sh -c '" if len(envs) > 0 { - res += strings.Join(envs, " ") + " " + res += strings.Join(envs, "; ") + "; " } // add secrets as environment variables secrets := cmd.getSecrets() if len(secrets) > 0 { - res += strings.Join(secrets, " ") + " " + res += strings.Join(secrets, "; ") + "; " } elems := strings.Split(inp, "\n") @@ -129,7 +129,7 @@ func (cmd *Cmd) scriptCommand(inp string) string { } parts = append(parts, c) } - res += strings.Join(parts, "; ") + "\"" + res += strings.Join(parts, "; ") + "'" return res } @@ -198,18 +198,18 @@ func (cmd *Cmd) scriptFile(inp string) (r io.Reader) { func (cmd *Cmd) genEnv() []string { envs := make([]string, 0, len(cmd.Environment)) for k, v := range cmd.Environment { - envs = append(envs, fmt.Sprintf("%s='%s'", k, v)) + envs = append(envs, fmt.Sprintf("%s=\"%s\"", k, v)) } sort.Slice(envs, func(i, j int) bool { return envs[i] < envs[j] }) return envs } -// getSecrets returns a sorted list of secrets key from the Secrets slice (part of the command) +// getSecrets returns a sorted list of secrets key from the secrets slice (part of the command) func (cmd *Cmd) getSecrets() []string { secrets := []string{} for _, k := range cmd.Options.Secrets { if v := cmd.Secrets[k]; v != "" { - secrets = append(secrets, fmt.Sprintf("%s='%s'", k, v)) + secrets = append(secrets, fmt.Sprintf("%s=\"%s\"", k, v)) } } sort.Slice(secrets, func(i, j int) bool { return secrets[i] < secrets[j] }) diff --git a/pkg/config/command_test.go b/pkg/config/command_test.go index ec05ba09..8d3131cb 100644 --- a/pkg/config/command_test.go +++ b/pkg/config/command_test.go @@ -21,9 +21,9 @@ func TestCmd_GetScript(t *testing.T) { { name: "single line command without environment variables", cmd: &Cmd{ - Script: "echo 'Hello, World!'", + Script: "echo Hello, World!", }, - expectedScript: `sh -c "echo 'Hello, World!'"`, + expectedScript: `sh -c 'echo Hello, World!'`, expectedContents: nil, }, { @@ -91,7 +91,7 @@ export FOO='bar' "GREETING": "Hello, World!", }, }, - expectedScript: `sh -c "GREETING='Hello, World!' echo $GREETING"`, + expectedScript: `sh -c 'GREETING="Hello, World!"; echo $GREETING'`, expectedContents: nil, }, { @@ -108,8 +108,8 @@ echo $FAREWELL`, expectedContents: []string{ "#!/bin/sh", "set -e", - "export FAREWELL='Goodbye, World!'", - "export GREETING='Hello, World!'", + `export FAREWELL="Goodbye, World!"`, + `export GREETING="Hello, World!"`, "echo $GREETING", "echo $FAREWELL", }, @@ -179,7 +179,7 @@ func TestCmd_getScriptCommand(t *testing.T) { cmd := c.Tasks[0].Commands[3] assert.Equal(t, "git", cmd.Name, "name") res := cmd.scriptCommand(cmd.Script) - assert.Equal(t, `sh -c "git clone https://example.com/remark42.git /srv || true; cd /srv; git pull"`, res) + assert.Equal(t, `sh -c 'git clone https://example.com/remark42.git /srv || true; cd /srv; git pull'`, res) }) t.Run("no-script", func(t *testing.T) { @@ -193,7 +193,7 @@ func TestCmd_getScriptCommand(t *testing.T) { cmd := c.Tasks[0].Commands[4] assert.Equal(t, "docker", cmd.Name) res := cmd.scriptCommand(cmd.Script) - assert.Equal(t, `sh -c "BAR='qux' FOO='bar' docker pull umputun/remark42:latest; docker stop remark42 || true; docker rm remark42 || true; docker run -d --name remark42 -p 8080:8080 umputun/remark42:latest"`, res) + assert.Equal(t, `sh -c 'BAR="qux"; FOO="bar"; docker pull umputun/remark42:latest; docker stop remark42 || true; docker rm remark42 || true; docker run -d --name remark42 -p 8080:8080 umputun/remark42:latest'`, res) }) } @@ -218,7 +218,7 @@ func TestCmd_getScriptFile(t *testing.T) { "VAR1": "value1", }, }, - expected: "#!/bin/sh\nset -e\nexport VAR1='value1'\necho 'Hello, World!'\n", + expected: "#!/bin/sh\nset -e\nexport VAR1=\"value1\"\necho 'Hello, World!'\n", }, { name: "with multiple environment variables", @@ -229,7 +229,7 @@ func TestCmd_getScriptFile(t *testing.T) { "VAR2": "value2", }, }, - expected: "#!/bin/sh\nset -e\nexport VAR1='value1'\nexport VAR2='value2'\necho 'Hello, World!'\n", + expected: "#!/bin/sh\nset -e\nexport VAR1=\"value1\"\nexport VAR2=\"value2\"\necho 'Hello, World!'\n", }, { name: "with multiple environment variables and secrets", @@ -246,7 +246,7 @@ func TestCmd_getScriptFile(t *testing.T) { Secrets: []string{"SEC1"}, }, }, - expected: "#!/bin/sh\nset -e\nexport VAR1='value1'\nexport VAR2='value2'\nexport SEC1='secret1'\necho 'Hello, World!'\n", + expected: "#!/bin/sh\nset -e\nexport VAR1=\"value1\"\nexport VAR2=\"value2\"\nexport SEC1=\"secret1\"\necho 'Hello, World!'\n", }, { name: "with multiple secrets", @@ -261,7 +261,7 @@ func TestCmd_getScriptFile(t *testing.T) { Secrets: []string{"SEC1", "SEC2"}, }, }, - expected: "#!/bin/sh\nset -e\nexport SEC1='secret1'\nexport SEC2='secret2'\necho 'Hello, World!'\n", + expected: "#!/bin/sh\nset -e\nexport SEC1=\"secret1\"\nexport SEC2=\"secret2\"\necho 'Hello, World!'\n", }, } @@ -445,7 +445,7 @@ func TestCmd_GetWait(t *testing.T) { Command: "echo Hello, World!", }, }, - expectedCmd: `sh -c "echo Hello, World!"`, + expectedCmd: `sh -c 'echo Hello, World!'`, }, { name: "multi-line wait command",