Skip to content

Instantly share code, notes, and snippets.

@influx6
Forked from jmervine/main.go
Last active August 29, 2015 14:24
Show Gist options
  • Save influx6/0493521df075f209a1f3 to your computer and use it in GitHub Desktop.
Save influx6/0493521df075f209a1f3 to your computer and use it in GitHub Desktop.
// safeSplit handles quoting well for commands for use with github.com/jmervine/exec/v2
//
// Examples:
// > safeSplit("/bin/bash bash -l -c 'echo \"foo bar bah bin\"'")
// => []string{"/bin/bash", "-l", "-c", "echo \"foo bar bah bin\""}
// > safeSplit("docker run --rm -it some/image bash -c \"npm test\"")
// => []string{"docker", "run", "--rm", "-it", "some/image", "bash", "-c", "npm test"}
//----
// package main
// import "github.com/jmervine/exec/v2"
// import "fmt"
// action := safeSplit("docker run --rm -it some/image bash -c \"npm test\"")
// out, err := exec.Exec(action[0], action[1:]...)
// if err != nil { panic(err) }
// fmt.Println(out)
//----
import "strings"
func safeSplit(s string) []string {
split := strings.Split(s, " ")
var result []string
var inquote string
var block string
for _, i := range split {
if inquote == "" {
if strings.HasPrefix(i, "'") || strings.HasPrefix(i, "\"") {
inquote = string(i[0])
block = strings.TrimPrefix(i, inquote) + " "
} else {
result = append(result, i)
}
} else {
if !strings.HasSuffix(i, inquote) {
block += i + " "
} else {
block += strings.TrimSuffix(i, inquote)
inquote = ""
result = append(result, block)
block = ""
}
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment