Expand environment vars for commands

This commit is contained in:
Alex Yatskov 2015-07-02 17:47:41 +09:00
parent bcb06d99ce
commit 57cee5f1e2

View File

@ -32,10 +32,13 @@ import (
type command []string type command []string
func (c command) expandEnv() { func (c command) expandEnv() []string {
for index, value := range c { var args []string
c[index] = os.ExpandEnv(value) for _, value := range c {
args = append(args, os.ExpandEnv(value))
} }
return args
} }
func (c command) process(dir string, flags int) error { func (c command) process(dir string, flags int) error {
@ -43,14 +46,16 @@ func (c command) process(dir string, flags int) error {
return fmt.Errorf("command element is invalid") return fmt.Errorf("command element is invalid")
} }
cmd := exec.Command(c[0], c[1:]...) args := c.expandEnv()
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir cmd.Dir = dir
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
if flags&flagVerbose == flagVerbose { if flags&flagVerbose == flagVerbose {
log.Printf("executing command %s", strings.Join(c, " ")) log.Printf("executing command %s", strings.Join(args, " "))
} }
return cmd.Run() return cmd.Run()