summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2018-09-11 09:07:36 +1000
committerGitHub <noreply@github.com>2018-09-11 09:07:36 +1000
commit7d862785073599b7887332a07d37f0ca4b867f7a (patch)
tree8385dd17ad40ce7ca2cef96e03171ca257e97675
parent7f4371ad71247d64249299b390725f073e7fbfad (diff)
parent985196f5aa2fd0ef23e6a6aee93f7600f5aef63d (diff)
Merge pull request #274 from sascha-andres/masterv0.2.2
fix: escape quote character on Linux
-rw-r--r--pkg/commands/os.go17
-rw-r--r--pkg/commands/os_default_platform.go11
-rw-r--r--pkg/commands/os_test.go26
-rw-r--r--pkg/commands/os_windows.go9
4 files changed, 48 insertions, 15 deletions
diff --git a/pkg/commands/os.go b/pkg/commands/os.go
index 834c45376..c8ca40f29 100644
--- a/pkg/commands/os.go
+++ b/pkg/commands/os.go
@@ -17,11 +17,12 @@ import (
// Platform stores the os state
type Platform struct {
- os string
- shell string
- shellArg string
- escapedQuote string
- openCommand string
+ os string
+ shell string
+ shellArg string
+ escapedQuote string
+ openCommand string
+ fallbackEscapedQuote string
}
// OSCommand holds all the os commands
@@ -140,7 +141,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex
// Quote wraps a message in platform-specific quotation marks
func (c *OSCommand) Quote(message string) string {
message = strings.Replace(message, "`", "\\`", -1)
- return c.Platform.escapedQuote + message + c.Platform.escapedQuote
+ escapedQuote := c.Platform.escapedQuote
+ if strings.Contains(message, c.Platform.escapedQuote) {
+ escapedQuote = c.Platform.fallbackEscapedQuote
+ }
+ return escapedQuote + message + escapedQuote
}
// Unquote removes wrapping quotations marks if they are present
diff --git a/pkg/commands/os_default_platform.go b/pkg/commands/os_default_platform.go
index f106bbd62..7b063417b 100644
--- a/pkg/commands/os_default_platform.go
+++ b/pkg/commands/os_default_platform.go
@@ -8,10 +8,11 @@ import (
func getPlatform() *Platform {
return &Platform{
- os: runtime.GOOS,
- shell: "bash",
- shellArg: "-c",
- escapedQuote: "\"",
- openCommand: "open {{filename}}",
+ os: runtime.GOOS,
+ shell: "bash",
+ shellArg: "-c",
+ escapedQuote: "'",
+ openCommand: "open {{filename}}",
+ fallbackEscapedQuote: "\"",
}
}
diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go
index 01173fb15..aeef4a6e5 100644
--- a/pkg/commands/os_test.go
+++ b/pkg/commands/os_test.go
@@ -265,6 +265,32 @@ func TestOSCommandQuote(t *testing.T) {
assert.EqualValues(t, expected, actual)
}
+// TestOSCommandQuoteSingleQuote tests the quote function with ' quotes explicitly for Linux
+func TestOSCommandQuoteSingleQuote(t *testing.T) {
+ osCommand := newDummyOSCommand()
+
+ osCommand.Platform.os = "linux"
+
+ actual := osCommand.Quote("hello 'test'")
+
+ expected := osCommand.Platform.fallbackEscapedQuote + "hello 'test'" + osCommand.Platform.fallbackEscapedQuote
+
+ assert.EqualValues(t, expected, actual)
+}
+
+// TestOSCommandQuoteSingleQuote tests the quote function with " quotes explicitly for Linux
+func TestOSCommandQuoteDoubleQuote(t *testing.T) {
+ osCommand := newDummyOSCommand()
+
+ osCommand.Platform.os = "linux"
+
+ actual := osCommand.Quote(`hello "test"`)
+
+ expected := osCommand.Platform.escapedQuote + "hello \"test\"" + osCommand.Platform.escapedQuote
+
+ assert.EqualValues(t, expected, actual)
+}
+
func TestOSCommandUnquote(t *testing.T) {
osCommand := newDummyOSCommand()
diff --git a/pkg/commands/os_windows.go b/pkg/commands/os_windows.go
index 1658e5f36..8fa9ce1c2 100644
--- a/pkg/commands/os_windows.go
+++ b/pkg/commands/os_windows.go
@@ -2,9 +2,10 @@ package commands
func getPlatform() *Platform {
return &Platform{
- os: "windows",
- shell: "cmd",
- shellArg: "/c",
- escapedQuote: `\"`,
+ os: "windows",
+ shell: "cmd",
+ shellArg: "/c",
+ escapedQuote: `\"`,
+ fallbackEscapedQuote: "\\'",
}
}