Skip to content

Commit bca9b77

Browse files
committed
feat: add env methods
1 parent c4113fb commit bca9b77

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

script/contextual.go

+29
Original file line numberDiff line numberDiff 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

3437
func (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+
3965
func (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
}

0 commit comments

Comments
 (0)