Skip to content

Commit

Permalink
add QuoteCommand to quote a complete command
Browse files Browse the repository at this point in the history
* adds support for quoting a complete command comprised of a program
name and arguments
  • Loading branch information
alexcb authored and alessio committed Sep 25, 2020
1 parent 13a8654 commit effa65e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions shellescape.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ func Quote(s string) string {

return s
}

// QuoteCommand returns a shell-escaped version of the slice of strings.
// The returned value is a string that can safely be used as shell command arguments.
func QuoteCommand(args []string) string {
var l []string
for _, s := range args {
l = append(l, Quote(s))
}
return strings.Join(l, " ")
}
6 changes: 6 additions & 0 deletions shellescape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ func TestCleanString(t *testing.T) {
expected := `foo.example.com`
assertEqual(t, s, expected)
}

func TestQuoteCommand(t *testing.T) {
s := shellescape.QuoteCommand([]string{"ls", "-l", "file with space"})
expected := `ls -l 'file with space'`
assertEqual(t, s, expected)
}

0 comments on commit effa65e

Please sign in to comment.