@@ -21,11 +21,13 @@ type (
21
21
env []string
22
22
dir string
23
23
shell []string
24
+ osenv bool
24
25
mux sync.RWMutex
25
26
}
26
27
27
28
TShell interface {
28
29
SetEnv (key , value string )
30
+ UseOSEnv (use bool )
29
31
SetDir (dir string )
30
32
SetShell (shell string , keys ... string ) error
31
33
CallPackageContext (ctx context.Context , out io.Writer , commands ... string ) error
36
38
37
39
func New () TShell {
38
40
v := & _shell {
41
+ osenv : true ,
39
42
env : make ([]string , 0 , 10 ),
40
43
dir : os .TempDir (),
41
44
shell : []string {"/bin/sh" , "-xec" },
@@ -50,6 +53,13 @@ func (v *_shell) SetEnv(key, value string) {
50
53
v .env = append (v .env , key + "=" + value )
51
54
}
52
55
56
+ func (v * _shell ) UseOSEnv (use bool ) {
57
+ v .mux .Lock ()
58
+ defer v .mux .Unlock ()
59
+
60
+ v .osenv = use
61
+ }
62
+
53
63
func (v * _shell ) SetDir (dir string ) {
54
64
v .mux .Lock ()
55
65
defer v .mux .Unlock ()
@@ -82,26 +92,36 @@ func (v *_shell) CallPackageContext(ctx context.Context, out io.Writer, commands
82
92
return nil
83
93
}
84
94
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 {
86
96
v .mux .RLock ()
87
97
defer v .mux .RUnlock ()
88
98
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 , " <&-" )... )
91
100
cmd .Dir = v .dir
92
101
cmd .Stdout = out
93
102
cmd .Stderr = out
94
103
104
+ if v .osenv {
105
+ cmd .Env = append (os .Environ (), v .env ... )
106
+ } else {
107
+ cmd .Env = v .env
108
+ }
109
+
95
110
return cmd .Run ()
96
111
}
97
112
98
- func (v * _shell ) Call (ctx context.Context , c string ) ([]byte , error ) {
113
+ func (v * _shell ) Call (ctx context.Context , command string ) ([]byte , error ) {
99
114
v .mux .RLock ()
100
115
defer v .mux .RUnlock ()
101
116
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 , " <&-" )... )
104
118
cmd .Dir = v .dir
105
119
120
+ if v .osenv {
121
+ cmd .Env = append (os .Environ (), v .env ... )
122
+ } else {
123
+ cmd .Env = v .env
124
+ }
125
+
106
126
return cmd .CombinedOutput ()
107
127
}
0 commit comments