-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexecutable.go
88 lines (70 loc) · 2.1 KB
/
executable.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pexec
import (
"io"
"os"
"os/exec"
"strings"
)
// Executable represents an executable on the $PATH.
type Executable struct {
name string
}
// NewExecutable returns an instance of an Executable given the name of that
// executable. When given simply a name, the execuable will be looked up on the
// $PATH before execution. Alternatively, when given a path, the executable
// will use that path to invoke the executable file directly.
func NewExecutable(name string) Executable {
return Executable{
name: name,
}
}
// Execute invokes the executable with a set of Execution arguments.
func (e Executable) Execute(execution Execution) error {
envPath := os.Getenv("PATH")
if execution.Env != nil {
var path string
for _, variable := range execution.Env {
if strings.HasPrefix(variable, "PATH=") {
path = strings.TrimPrefix(variable, "PATH=")
}
}
if path != "" {
os.Setenv("PATH", path)
}
}
executable, err := exec.LookPath(e.name)
if err != nil {
return err
}
os.Setenv("PATH", envPath)
cmd := exec.Command(executable, execution.Args...)
if execution.Dir != "" {
cmd.Dir = execution.Dir
}
if len(execution.Env) > 0 {
cmd.Env = execution.Env
}
cmd.Stdout = execution.Stdout
cmd.Stderr = execution.Stderr
cmd.Stdin = execution.Stdin
return cmd.Run()
}
// Execution is the set of configurable options for a given execution of the
// executable.
type Execution struct {
// Args is a list of the arguments to be passed to the executable.
Args []string
// Dir is the path to a directory from with the executable should be invoked.
// If Dir is not set, the current working directory will be used.
Dir string
// Env is the set of environment variables that make up the environment for
// the execution. If Env is not set, the existing os.Environ value will be
// used.
Env []string
// Stdout is where the output of stdout will be written during the execution.
Stdout io.Writer
// Stderr is where the output of stderr will be written during the execution.
Stderr io.Writer
// Stdin is where the input of stdin will be read during the execution.
Stdin io.Reader
}