/
option.go
84 lines (71 loc) · 1.44 KB
/
option.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
package shell
import (
"io"
"os"
"path/filepath"
"runtime"
)
type commandOptions struct {
environment map[string]string
directory string
verbose bool
stdin io.Reader
stderr io.Writer
arguments []string
}
func buildOptions(options []Option) *commandOptions {
commandOptions := &commandOptions{
stdin: os.Stdin,
stderr: os.Stderr,
}
for _, option := range options {
option(commandOptions)
}
return commandOptions
}
type Option func(*commandOptions)
func Env(environment map[string]string) Option {
return func(options *commandOptions) {
options.environment = environment
}
}
func Dir(directory string) Option {
return func(options *commandOptions) {
options.directory = directory
}
}
func CurrentDir() Option {
_, name, _, ok := runtime.Caller(1)
var directory string
if ok {
directory = filepath.Dir(name)
}
return func(options *commandOptions) {
options.directory = directory
}
}
func Verbose() Option {
return func(options *commandOptions) {
options.verbose = true
}
}
func Stdin(stdin io.Reader) Option {
return func(options *commandOptions) {
options.stdin = stdin
}
}
func Stderr(stderr io.Writer) Option {
return func(options *commandOptions) {
options.stderr = stderr
}
}
func StderrSilent() Option {
return func(options *commandOptions) {
options.stderr = nil
}
}
func Args(arguments ...string) Option {
return func(options *commandOptions) {
options.arguments = arguments
}
}