File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -29,13 +29,39 @@ type Pipe struct {
2929
3030 // wd is the working directory for current pipe.
3131 wd string
32+ // env is the environment variables for current pipe.
33+ // Non-empty value will be set to the exec.Command instance.
34+ env []string
3235}
3336
3437func (p * Pipe ) At (dir string ) * Pipe {
3538 p .wd = dir
3639 return p
3740}
3841
42+ // WithCurrentEnv sets the environment variables to the current process's environment.
43+ func (p * Pipe ) WithCurrentEnv () * Pipe {
44+ return p .WithEnv (os .Environ ())
45+ }
46+
47+ // WithEnv sets the environment variables for the current pipe.
48+ func (p * Pipe ) WithEnv (env []string ) * Pipe {
49+ p .env = env
50+ return p
51+ }
52+
53+ // AppendEnv appends the environment variables for the current pipe.
54+ func (p * Pipe ) AppendEnv (env ... string ) * Pipe {
55+ p .env = append (p .env , env ... )
56+ return p
57+ }
58+
59+ // WithEnvKV sets the environment variable key-value pair for the current pipe.
60+ func (p * Pipe ) WithEnvKV (key , value string ) * Pipe {
61+ p .env = append (p .env , key + "=" + value )
62+ return p
63+ }
64+
3965func (p * Pipe ) WithStderr (w io.Writer ) * Pipe {
4066 p .stderr = w
4167 p .Pipe = p .Pipe .WithStderr (w )
@@ -106,6 +132,9 @@ func (p *Pipe) execContext(
106132 if p .wd != "" {
107133 cmd .Dir = p .wd
108134 }
135+ if len (p .env ) > 0 {
136+ cmd .Env = p .env
137+ }
109138
110139 return cmd
111140}
You can’t perform that action at this time.
0 commit comments