-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
98 lines (85 loc) · 1.57 KB
/
options.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
89
90
91
92
93
94
95
96
97
98
package tish
import (
"io"
"io/fs"
"os"
"path/filepath"
)
type ShellOption func(*Shell) error
func WithBuiltin(dir string) ShellOption {
return func(s *Shell) error {
list, err := fs.Glob(os.DirFS(dir), "*.so")
if err != nil {
return err
}
for i := range list {
list[i] = filepath.Join(dir, list[i])
}
return loadExternalBuiltins(s, list)
}
}
func WithFinder(find CommandFinder) ShellOption {
return func(s *Shell) error {
s.find = find
return nil
}
}
func WithStdin(r io.Reader) ShellOption {
return func(s *Shell) error {
s.SetIn(r)
return nil
}
}
func WithStdout(w io.Writer) ShellOption {
return func(s *Shell) error {
s.SetOut(w)
return nil
}
}
func WithStderr(w io.Writer) ShellOption {
return func(s *Shell) error {
s.SetErr(w)
return nil
}
}
func WithEcho() ShellOption {
return func(s *Shell) error {
s.echo = true
return nil
}
}
func WithVar(ident string, values ...string) ShellOption {
return func(s *Shell) error {
if s.locals == nil {
s.locals = EmptyEnv()
}
return s.locals.Define(ident, values)
}
}
func WithCwd(dir string) ShellOption {
return func(s *Shell) error {
return s.Chdir(dir)
}
}
func WithEnv(e Environment) ShellOption {
return func(s *Shell) error {
s.locals = EnclosedEnv(e)
return nil
}
}
func WithExport(vs map[string]string) ShellOption {
return func(s *Shell) error {
for k, v := range vs {
s.Export(k, v)
}
return nil
}
}
func WithAlias(vs map[string]string) ShellOption {
return func(s *Shell) error {
for k, v := range vs {
s.Alias(k, v)
}
return nil
}
}