Support environment variables in commands

This commit is contained in:
Alex Yatskov 2015-06-25 12:31:25 +09:00
parent 6f211f37de
commit bcb06d99ce
2 changed files with 27 additions and 18 deletions

View File

@ -32,19 +32,25 @@ import (
type command []string type command []string
func (c *command) process(dir string, flags int) error { func (c command) expandEnv() {
if len(*c) < 1 { for index, value := range c {
c[index] = os.ExpandEnv(value)
}
}
func (c command) process(dir string, flags int) error {
if len(c) < 1 {
return fmt.Errorf("command element is invalid") return fmt.Errorf("command element is invalid")
} }
cmd := exec.Command((*c)[0], (*c)[1:]...) cmd := exec.Command(c[0], c[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(c, " "))
} }
return cmd.Run() return cmd.Run()

31
link.go
View File

@ -72,32 +72,35 @@ func createPath(loc string, flags int, mode os.FileMode) error {
return nil return nil
} }
func (l *link) parse() (string, string, os.FileMode, error) { func (l link) parse() (srcPath string, dstPath string, mode os.FileMode, err error) {
length := len(*l) length := len(l)
if length < 1 || length > 3 { if length < 1 || length > 3 {
return "", "", 0, fmt.Errorf("link element is invalid") err = fmt.Errorf("link element is invalid")
return
} }
dstPath := (*l)[0]
srcPath := dstPath
if length > 1 {
srcPath = (*l)[1]
}
var mode os.FileMode = 0755
if length > 2 { if length > 2 {
parsed, err := strconv.ParseUint((*l)[2], 0, 64) var parsed uint64
parsed, err = strconv.ParseUint(l[2], 0, 64)
if err != nil { if err != nil {
return "", "", 0, err return
} }
mode = os.FileMode(parsed) mode = os.FileMode(parsed)
} else {
mode = 0755
} }
return srcPath, dstPath, mode, nil dstPath = os.ExpandEnv(l[0])
srcPath = dstPath
if length > 1 {
srcPath = os.ExpandEnv(l[1])
}
return
} }
func (l *link) process(srcDir, dstDir string, flags int) error { func (l link) process(srcDir, dstDir string, flags int) error {
srcPath, dstPath, mode, err := l.parse() srcPath, dstPath, mode, err := l.parse()
if err != nil { if err != nil {
return err return err