Skip to content
Permalink
Browse files

Make shellquote public package of leatherman

  • Loading branch information...
frioux committed Apr 6, 2019
1 parent 9cf2af8 commit e8b5162ba10091af7a488c9cf5ee9393dc1f3e3e
2 go.mod
@@ -3,8 +3,8 @@ module github.com/frioux/leatherman
require (
github.com/BurntSushi/toml v0.3.1
github.com/PuerkitoBio/goquery v1.5.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/erikdubbelboer/gspt v0.0.0-20190125194910-e68493906b83
github.com/frioux/shellquote v0.0.2
github.com/fsnotify/fsnotify v1.4.7
github.com/headzoo/surf v1.0.0
github.com/headzoo/ut v0.0.0-20181013193318-a13b5a7a02ca // indirect
4 go.sum
@@ -9,8 +9,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikdubbelboer/gspt v0.0.0-20190125194910-e68493906b83 h1:ngHdSomn2MyugZYKHiycad2xERwIrmMlET7A0lC0UU4=
github.com/erikdubbelboer/gspt v0.0.0-20190125194910-e68493906b83/go.mod h1:v6o7m/E9bfvm79dE1iFiF+3T7zLBnrjYjkWMa1J+Hv0=
github.com/frioux/shellquote v0.0.2 h1:CvQ1aMCS/xhhyGF4JIeA49bhE3W1s5XdBkwmhH3BceQ=
github.com/frioux/shellquote v0.0.2/go.mod h1:1VoFO5LSUNqwAKp2QjSpf9b4PZ4ff+uKXPEjonuyJh8=
github.com/frioux/yaml v0.0.0-20180422015826-390d32f04db5 h1:YMX30YzQ7HSrUMJV8QnQ5toCxoS4lsxD7DjDMUvoa/k=
github.com/frioux/yaml v0.0.0-20180422015826-390d32f04db5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
@@ -45,8 +43,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/spf13/afero v1.2.0 h1:O9FblXGxoTc51M+cqr74Bm2Tmt4PvkA5iu/j8HrkNuY=
github.com/spf13/afero v1.2.0/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190123085648-057139ce5d2b h1:Elez2XeF2p9uyVj0yEUDqQ56NFcDtcBNkYP7yv8YbUE=
@@ -6,7 +6,7 @@ import (
"io"
"os"

"github.com/frioux/shellquote"
"github.com/frioux/leatherman/pkg/shellquote"
"github.com/pkg/errors"
)

@@ -5,7 +5,7 @@ import (
"io"
"os"

"github.com/frioux/shellquote"
"github.com/frioux/leatherman/pkg/shellquote"
"github.com/pkg/errors"
)

@@ -0,0 +1,21 @@
package shellquote_test

import (
"fmt"
"os"

"github.com/frioux/leatherman/pkg/shellquote"
)

func Example() {
fmt.Println("#!/bin/sh")
fmt.Println("")
quoted, err := shellquote.Quote(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't quote input: %s\n", err)
os.Exit(1)
}
// error won't happen if the first input didn't error
doublequoted, _ := shellquote.Quote([]string{quoted})
fmt.Println("ssh superserver", doublequoted)
}
@@ -0,0 +1,90 @@
/*
shellquote quotes strings for shell scripts.
Sometimes you get strings from the internet and need to quote them for security,
other times you'll need to quote your own strings because doing it by hand is
just too much work.
Another option is http://github.com/kballard/go-shellquote. The quoting algorithms are
completely different and the results vary as well, but both produce working
results in my brief testing. See
https://github.com/frioux/go-scraps/tree/master/cmd/quotetest for a tool that
shows the results of quoting with each package.
*/
package shellquote

import (
"errors"
"regexp"
"strings"
)

// ErrNull will be returned from Quote if any of the strings contains a null
// byte.
var ErrNull = errors.New("No way to quote string containing null bytes")

// Quote will return a shell quoted string for the passed tokens.
func Quote(in []string) (string, error) {
tmp := make([]string, len(in))
var sawNonEqual bool
for i, x := range in {
if x == "" {
tmp[i] = `''`
continue
}
if strings.Contains(x, "\x00") {
return "", ErrNull
}

var escape bool
hasEqual := strings.Contains(x, "=")
if hasEqual {
if !sawNonEqual {
escape = true
}
} else {
sawNonEqual = true
}

toEsc := regexp.MustCompile(`[^\w!%+,\-./:=@^]`)
if !escape && toEsc.MatchString(x) {
escape = true
}

if escape || (!sawNonEqual && hasEqual) {
y := strings.Replace(x, `'`, `'\''`, -1)

simplifyRe := regexp.MustCompile(`(?:'\\''){2,}`)
y = simplifyRe.ReplaceAllStringFunc(y, func(str string) string {
var inner string
for i := 0; i < len(str)/4; i++ {
inner += "'"
}
return `'"` + inner + `"'`
})

y = `'` + y + `'`
y = strings.TrimSuffix(y, `''`)
y = strings.TrimPrefix(y, `''`)

tmp[i] = y
continue
}
tmp[i] = x
}
return strings.Join(tmp, " "), nil
}

/*
Copyright 2018 Arthur Axel fREW Schmidt
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
@@ -0,0 +1,37 @@
package shellquote

import (
"testing"

"github.com/stretchr/testify/assert"
)

func test(t *testing.T, in []string, expected string) {

ret, err := Quote(in)
if assert.Nil(t, err, "No error") {
assert.Equal(t, expected, ret)
}
}

func TestShellQuote(t *testing.T) {
test(t, []string{""}, `''`)
test(t, []string{"foo"}, `foo`)
test(t, []string{"foo", "bar"}, `foo bar`)
test(t, []string{"foo*"}, `'foo*'`)
test(t, []string{"foo bar"}, `'foo bar'`)
test(t, []string{"foo'bar"}, `'foo'\''bar'`)
test(t, []string{"'foo"}, `\''foo'`)
test(t, []string{"foo", "bar*"}, `foo 'bar*'`)
test(t, []string{"foo'foo", "bar", "baz'"}, `'foo'\''foo' bar 'baz'\'`)
test(t, []string{`\`}, `'\'`)
test(t, []string{"'"}, `\'`)
test(t, []string{`\'`}, `'\'\'`)
test(t, []string{"a''b"}, `'a'"''"'b'`)
test(t, []string{"azAZ09_!%+,-./:@^"}, `azAZ09_!%+,-./:@^`)
test(t, []string{"foo=bar", "command"}, `'foo=bar' command`)
test(t, []string{"foo=bar", "baz=quux", "command"}, `'foo=bar' 'baz=quux' command`)

_, err := Quote([]string{"\x00"})
assert.Equal(t, err, ErrNull)
}

0 comments on commit e8b5162

Please sign in to comment.
You can’t perform that action at this time.