-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorators.go
71 lines (63 loc) · 1.49 KB
/
decorators.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
package goexec
import (
"io"
"os"
"os/exec"
)
// Args allows you to define the arguments sent to the command to be run
func Args(args ...string) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
c.Args = append([]string{c.Path}, args...)
return nil
}
}
// Cwd allows you to configure the working directory of the command to be run
func Cwd(dir string) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
c.Dir = dir
return nil
}
}
// Env allows you to set the exact environment in which the command will be run
func Env(env map[string]string) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
e := []string{}
for k, v := range env {
e = append(e, k+"="+v)
}
c.Env = e
return nil
}
}
// EnvCombined will add the variables you provide to the existing environment
func EnvCombined(env map[string]string) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
e := os.Environ()
for k, v := range env {
e = append(e, k+"="+v)
}
c.Env = e
return nil
}
}
// SetIn allows you to set a custom StdIn stream
func SetIn(in io.Reader) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
c.Stdin = in
return nil
}
}
// SetOut allows you to set a custom StdOut stream
func SetOut(out io.Writer) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
c.Stdout = out
return nil
}
}
// SetErr allows you to set a custom StdErr stream
func SetErr(err io.Writer) func(*exec.Cmd) error {
return func(c *exec.Cmd) error {
c.Stderr = err
return nil
}
}