-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathparse.go
36 lines (30 loc) · 878 Bytes
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package procs
import (
"log"
"os"
"strings"
shlex "github.com/flynn/go-shlex"
)
// SplitCommand parses a command and splits it into lexical arguments
// like a shell, returning a []string that can be used as arguments to
// exec.Command.
func SplitCommand(cmd string) []string {
return SplitCommandEnv(cmd, nil)
}
// SplitCommandEnv parses a command and splits it into lexical
// arguments like a shell, returning a []string that can be used as
// arguments to exec.Command. It also allows providing an expansion
// function that will be used when expanding values within the parsed
// arguments.
func SplitCommandEnv(cmd string, getenv func(key string) string) []string {
parts, err := shlex.Split(strings.TrimSpace(cmd))
if err != nil {
log.Fatal(err)
}
if getenv != nil {
for i, p := range parts {
parts[i] = os.Expand(p, getenv)
}
}
return parts
}