From ee4660af97a6da7fcb0a17474a9a6d70d0d7df0b Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Wed, 15 Aug 2018 23:55:55 -0400 Subject: [PATCH 1/4] #158: escapes backticks, which is a problem in shells like Bash --- pkg/commands/os.go | 3 +++ pkg/commands/os_test.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 pkg/commands/os_test.go diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 9f9819a5a..9ccdebc56 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "runtime" + "regexp" "github.com/davecgh/go-spew/spew" @@ -170,5 +171,7 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) (*e // Quote wraps a message in platform-specific quotation marks func (c *OSCommand) Quote(message string) string { + r := regexp.MustCompile("`") + message = r.ReplaceAllString(message, "\\`") return c.Platform.escapedQuote + message + c.Platform.escapedQuote } diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go new file mode 100644 index 000000000..a6bdbc1b1 --- /dev/null +++ b/pkg/commands/os_test.go @@ -0,0 +1,16 @@ +package commands + +import "testing" + +func TestQuote(t *testing.T) { + osCommand := &OSCommand { + Log: nil, + Platform: getPlatform(), + } + test := "hello `test`" + expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote + test = osCommand.Quote(test) + if test != expected { + t.Error("Expected " + expected + ", got " + test) + } +} From db94dde11475a45a5bceafd7564c5fe3b8eacb18 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Wed, 15 Aug 2018 23:58:44 -0400 Subject: [PATCH 2/4] fix formatting --- pkg/commands/os.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/commands/os.go b/pkg/commands/os.go index 9ccdebc56..c23b48c82 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -171,7 +171,7 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) (*e // Quote wraps a message in platform-specific quotation marks func (c *OSCommand) Quote(message string) string { - r := regexp.MustCompile("`") - message = r.ReplaceAllString(message, "\\`") + r := regexp.MustCompile("`") + message = r.ReplaceAllString(message, "\\`") return c.Platform.escapedQuote + message + c.Platform.escapedQuote } From a7755ab184031cca783052155d1ba4c8275b51dc Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Thu, 16 Aug 2018 07:00:13 -0400 Subject: [PATCH 3/4] reformat --- pkg/commands/os_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/commands/os_test.go b/pkg/commands/os_test.go index a6bdbc1b1..29540aff6 100644 --- a/pkg/commands/os_test.go +++ b/pkg/commands/os_test.go @@ -3,8 +3,8 @@ package commands import "testing" func TestQuote(t *testing.T) { - osCommand := &OSCommand { - Log: nil, + osCommand := &OSCommand{ + Log: nil, Platform: getPlatform(), } test := "hello `test`" From 52033b32f702d1b0c0157b9dd9ecb77e8ddf82b0 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen Date: Thu, 16 Aug 2018 17:04:39 -0400 Subject: [PATCH 4/4] Use strings.Replace instead of regexp --- pkg/commands/os.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/commands/os.go b/pkg/commands/os.go index c23b48c82..08db71fbb 100644 --- a/pkg/commands/os.go +++ b/pkg/commands/os.go @@ -5,7 +5,7 @@ import ( "os" "os/exec" "runtime" - "regexp" + "strings" "github.com/davecgh/go-spew/spew" @@ -171,7 +171,6 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) (*e // Quote wraps a message in platform-specific quotation marks func (c *OSCommand) Quote(message string) string { - r := regexp.MustCompile("`") - message = r.ReplaceAllString(message, "\\`") + message = strings.Replace(message, "`", "\\`", -1) return c.Platform.escapedQuote + message + c.Platform.escapedQuote }