Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Parsing string args correctly #59

Merged
merged 2 commits into from
Feb 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bufio"
"bytes"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -41,8 +43,12 @@ func NewBuilder(c config) (*Bob, error) {
return nil, err
}

parseCmd := func(cmd string) []string {
c := strings.Split(cmd, " ")
parseCmd := func(cmd string) (c []string) {
s := bufio.NewScanner(strings.NewReader(cmd))
s.Split(splitFunc)
for s.Scan() {
c = append(c, s.Text())
}

// check for environment variables inside script
if strings.Contains(cmd, "$$") {
Expand Down Expand Up @@ -73,6 +79,39 @@ func NewBuilder(c config) (*Bob, error) {
}, nil
}

func splitFunc(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanWords(data, atEOF)
if err != nil {
return
}

if len(token) == 0 {
return
}

b := token[0]
if b != '"' && b != '\'' {
return
}

if token[len(token)-1] == b {
return
}

chunk := data[advance-1:]
i := bytes.IndexByte(chunk, b)
if i == -1 {
advance = len(data)
token = append(token, chunk...)
return
}

advance += i
token = append(token, chunk[:i+1]...)

return
}

func replaceEnv(cmds []string) {
for i, c := range cmds {
if !strings.HasPrefix(c, "$$") {
Expand Down
49 changes: 49 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,55 @@ func TestNewBuilder(t *testing.T) {
assert.Equal(t, c.IgnoredItems, b.ignoredItems)
}

func TestNewBuilder_CmdWithQuotes(t *testing.T) {
tests := []struct {
Command string
Chunks []string
}{
{ // one single quote pair
Command: `echo 'hello world' foo`,
Chunks: []string{`echo`, `'hello world'`, `foo`},
},
{ // one double quote pair
Command: `echo "hello world" foo`,
Chunks: []string{`echo`, `"hello world"`, `foo`},
},
{ // no ending double quote
Command: `echo "ga ga oh la la`,
Chunks: []string{`echo`, `"ga ga oh la la`},
},
{ // no ending single quote
Command: `echo 'ga ga oh la la`,
Chunks: []string{`echo`, `'ga ga oh la la`},
},
{ // multiple double quotes
Command: `echo "ga" "foo"`,
Chunks: []string{`echo`, `"ga"`, `"foo"`},
},
{ // double quotes inside single quotes
Command: `echo -c 'foo "bar"'`,
Chunks: []string{`echo`, `-c`, `'foo "bar"'`},
},
{ // single quotes inside double quotes
Command: `echo -c "foo 'bar'"`,
Chunks: []string{`echo`, `-c`, `"foo 'bar'"`},
},
}

for _, test := range tests {
c := config{
Build: []string{test.Command},
Run: []string{test.Command},
}

b, err := NewBuilder(c)
require.NoError(t, err)

assert.Equal(t, test.Chunks, b.buildCmds[0])
assert.Equal(t, test.Chunks, b.runCmds[0])
}
}

func TestClose(t *testing.T) {
b, err := NewBuilder(config{})
require.NoError(t, err)
Expand Down