2016-04-05 16:59:52 -05:00
|
|
|
// Copyright 2016 Tim Shannon. All rights reserved.
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-22 16:25:33 -05:00
|
|
|
"os"
|
2016-04-05 16:59:52 -05:00
|
|
|
"os/exec"
|
2016-04-22 16:25:33 -05:00
|
|
|
"path/filepath"
|
2016-04-05 16:59:52 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-04-19 20:38:53 -05:00
|
|
|
func runCmd(cmd, dir string, env []string) ([]byte, error) {
|
2016-04-20 10:40:07 -05:00
|
|
|
s := strings.Fields(strings.Replace(cmd, "@dir", dir, -1))
|
2016-04-05 16:59:52 -05:00
|
|
|
|
2016-04-19 20:38:53 -05:00
|
|
|
for i := range env {
|
|
|
|
env[i] = strings.Replace(env[i], "@dir", dir, -1)
|
|
|
|
}
|
|
|
|
|
2016-04-05 16:59:52 -05:00
|
|
|
var args []string
|
|
|
|
|
|
|
|
if len(s) > 1 {
|
|
|
|
args = s[1:]
|
|
|
|
}
|
|
|
|
|
2016-04-22 16:25:33 -05:00
|
|
|
name := s[0]
|
|
|
|
ec := &exec.Cmd{
|
|
|
|
Path: name,
|
|
|
|
Args: append([]string{name}, args...),
|
|
|
|
Dir: dir,
|
|
|
|
Env: env,
|
|
|
|
}
|
2016-04-05 16:59:52 -05:00
|
|
|
|
2016-04-22 16:25:33 -05:00
|
|
|
if filepath.Base(name) == name {
|
|
|
|
lp, err := lookPath(name, env)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ec.Path = lp
|
|
|
|
}
|
2016-04-05 16:59:52 -05:00
|
|
|
|
|
|
|
vlog("Executing command: %s in dir %s\n", cmd, dir)
|
|
|
|
|
|
|
|
result, err := ec.CombinedOutput()
|
|
|
|
if err != nil {
|
2016-04-22 15:39:06 -05:00
|
|
|
return nil, fmt.Errorf("%s\n%s", err, result)
|
2016-04-05 16:59:52 -05:00
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
2016-04-22 16:25:33 -05:00
|
|
|
|
|
|
|
// similar to os/exec.LookPath, except it checks if the passed in
|
|
|
|
// custom environment includes a path definitions and uses that path instead
|
|
|
|
// note this probably only works on unix, that's all I care about for now
|
|
|
|
func lookPath(file string, env []string) (string, error) {
|
|
|
|
if strings.Contains(file, "/") {
|
|
|
|
err := findExecutable(file)
|
|
|
|
if err == nil {
|
|
|
|
return file, nil
|
|
|
|
}
|
2016-05-04 15:39:56 -05:00
|
|
|
return "", &exec.Error{Name: file, Err: err}
|
2016-04-22 16:25:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range env {
|
|
|
|
if strings.HasPrefix(env[i], "PATH=") {
|
|
|
|
pathenv := env[i][5:]
|
|
|
|
if pathenv == "" {
|
2016-05-04 15:39:56 -05:00
|
|
|
return "", &exec.Error{Name: file, Err: exec.ErrNotFound}
|
2016-04-22 16:25:33 -05:00
|
|
|
}
|
|
|
|
for _, dir := range strings.Split(pathenv, ":") {
|
|
|
|
if dir == "" {
|
|
|
|
// Unix shell semantics: path element "" means "."
|
|
|
|
dir = "."
|
|
|
|
}
|
|
|
|
path := dir + "/" + file
|
|
|
|
if err := findExecutable(path); err == nil {
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 15:39:56 -05:00
|
|
|
return "", &exec.Error{Name: file, Err: exec.ErrNotFound}
|
2016-04-22 16:25:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return exec.LookPath(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findExecutable(file string) error {
|
|
|
|
d, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if m := d.Mode(); !m.IsDir() && m&0111 != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.ErrPermission
|
|
|
|
}
|