Skip to content

Commit

Permalink
use more comparible quoting around env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 12, 2023
1 parent ca206af commit c371670
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
14 changes: 7 additions & 7 deletions pkg/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}

Expand Down Expand Up @@ -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] })
Expand Down
24 changes: 12 additions & 12 deletions pkg/config/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
{
Expand Down Expand Up @@ -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,
},
{
Expand All @@ -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",
},
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
}

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
},
}

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit c371670

Please sign in to comment.