-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[run] fix escaping arguments that include double-quotes
- Loading branch information
Showing
8 changed files
with
130 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"[email protected]" | ||
], | ||
"env": { | ||
"CONFIG_VAR1": "abc", | ||
"PATH": "$PATH:$PWD/dist" | ||
}, | ||
"shell": { | ||
|
@@ -23,4 +24,4 @@ | |
"nixpkgs": { | ||
"commit": "3364b5b117f65fe1ce65a3cdd5612a078a3b31e3" | ||
} | ||
} | ||
} |
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,25 @@ | ||
package sheller | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// package sheller holds utilities that enable working with unix shells | ||
|
||
// QuoteWrap will wrap the word in double-quotes and escape special characters | ||
// as per: | ||
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02_03 | ||
func QuoteWrap(word string) string { | ||
strb := strings.Builder{} | ||
strb.WriteString(`"`) | ||
for _, r := range word { | ||
switch r { | ||
// Special characters inside double quotes: | ||
case '$', '`', '"', '\\', '\n': | ||
strb.WriteRune('\\') | ||
} | ||
strb.WriteRune(r) | ||
} | ||
strb.WriteString(`"`) | ||
return strb.String() | ||
} |
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,49 @@ | ||
package sheller | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestQuoteWrap(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
expected string | ||
}{ | ||
// regular string | ||
{ | ||
input: "Hello", | ||
expected: `"Hello"`, | ||
}, | ||
// embedded double-quotes | ||
{ | ||
input: `Hello "World"`, | ||
expected: `"Hello \"World\""`, | ||
}, | ||
// embedded backtick-quotes | ||
{ | ||
input: "Hello `World`", | ||
expected: "\"Hello \\`World\\`\"", | ||
}, | ||
// embedded single-quotes, | ||
{ | ||
input: "Hello 'World'", | ||
expected: "\"Hello 'World'\"", | ||
}, | ||
// escaping $, backslash (\), and \n | ||
{ | ||
input: `A $pecial charac\ter\n`, | ||
expected: `"A \$pecial charac\\ter\\n"`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run( | ||
tc.input, func(t *testing.T) { | ||
result := QuoteWrap(tc.input) | ||
if result != tc.expected { | ||
t.Errorf("Expected %q, but got %q", tc.expected, result) | ||
} | ||
}, | ||
) | ||
} | ||
} |
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,17 @@ | ||
exec devbox run echo '$__ETC_PROFILE_NIX_SOURCED' | ||
stdout '1' | ||
|
||
exec devbox run echo "$__ETC_PROFILE_NIX_SOURCED" | ||
stdout '' | ||
|
||
|
||
-- devbox.json -- | ||
{ | ||
"packages": ["nginx@latest"], | ||
"env": { | ||
"CONFIG_VAR1": "abc", | ||
"CONFIG_VAR2": "$DEVBOX_FOO", | ||
"CONFIG_VAR3": "${PWD}", | ||
"NGINX_CONFDIR": "devbox-json-override" | ||
} | ||
} |
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,11 @@ | ||
# ensure that we escape the arguments to `devbox run` | ||
|
||
exec devbox init | ||
exec devbox run -- echo 'this is a "hello world"' | ||
stdout 'this is a "hello world"' | ||
|
||
env FOO=bar | ||
exec devbox run echo '$FOO' | ||
stdout '$FOO' | ||
exec devbox run echo "$FOO" | ||
stdout 'bar' |