Skip to content

Commit a9f691b

Browse files
authored
fix shell (#5)
1 parent 9d78a2f commit a9f691b

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

shell/shell.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ type (
2121
env []string
2222
dir string
2323
shell []string
24+
osenv bool
2425
mux sync.RWMutex
2526
}
2627

2728
TShell interface {
2829
SetEnv(key, value string)
30+
UseOSEnv(use bool)
2931
SetDir(dir string)
3032
SetShell(shell string, keys ...string) error
3133
CallPackageContext(ctx context.Context, out io.Writer, commands ...string) error
@@ -36,6 +38,7 @@ type (
3638

3739
func New() TShell {
3840
v := &_shell{
41+
osenv: true,
3942
env: make([]string, 0, 10),
4043
dir: os.TempDir(),
4144
shell: []string{"/bin/sh", "-xec"},
@@ -50,6 +53,13 @@ func (v *_shell) SetEnv(key, value string) {
5053
v.env = append(v.env, key+"="+value)
5154
}
5255

56+
func (v *_shell) UseOSEnv(use bool) {
57+
v.mux.Lock()
58+
defer v.mux.Unlock()
59+
60+
v.osenv = use
61+
}
62+
5363
func (v *_shell) SetDir(dir string) {
5464
v.mux.Lock()
5565
defer v.mux.Unlock()
@@ -82,26 +92,36 @@ func (v *_shell) CallPackageContext(ctx context.Context, out io.Writer, commands
8292
return nil
8393
}
8494

85-
func (v *_shell) CallContext(ctx context.Context, out io.Writer, c string) error {
95+
func (v *_shell) CallContext(ctx context.Context, out io.Writer, command string) error {
8696
v.mux.RLock()
8797
defer v.mux.RUnlock()
8898

89-
cmd := exec.CommandContext(ctx, v.shell[0], append(v.shell[1:], c, " <&-")...)
90-
cmd.Env = append(os.Environ(), v.env...)
99+
cmd := exec.CommandContext(ctx, v.shell[0], append(v.shell[1:], command, " <&-")...)
91100
cmd.Dir = v.dir
92101
cmd.Stdout = out
93102
cmd.Stderr = out
94103

104+
if v.osenv {
105+
cmd.Env = append(os.Environ(), v.env...)
106+
} else {
107+
cmd.Env = v.env
108+
}
109+
95110
return cmd.Run()
96111
}
97112

98-
func (v *_shell) Call(ctx context.Context, c string) ([]byte, error) {
113+
func (v *_shell) Call(ctx context.Context, command string) ([]byte, error) {
99114
v.mux.RLock()
100115
defer v.mux.RUnlock()
101116

102-
cmd := exec.CommandContext(ctx, v.shell[0], append(v.shell[1:], c, " <&-")...)
103-
cmd.Env = append(os.Environ(), v.env...)
117+
cmd := exec.CommandContext(ctx, v.shell[0], append(v.shell[1:], command, " <&-")...)
104118
cmd.Dir = v.dir
105119

120+
if v.osenv {
121+
cmd.Env = append(os.Environ(), v.env...)
122+
} else {
123+
cmd.Env = v.env
124+
}
125+
106126
return cmd.CombinedOutput()
107127
}

0 commit comments

Comments
 (0)