Skip to content

Commit

Permalink
feat: add env methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed May 27, 2024
1 parent c4113fb commit bca9b77
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions script/contextual.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,39 @@ type Pipe struct {

// wd is the working directory for current pipe.
wd string
// env is the environment variables for current pipe.
// Non-empty value will be set to the exec.Command instance.
env []string
}

func (p *Pipe) At(dir string) *Pipe {
p.wd = dir
return p
}

// WithCurrentEnv sets the environment variables to the current process's environment.
func (p *Pipe) WithCurrentEnv() *Pipe {
return p.WithEnv(os.Environ())
}

// WithEnv sets the environment variables for the current pipe.
func (p *Pipe) WithEnv(env []string) *Pipe {
p.env = env
return p
}

// AppendEnv appends the environment variables for the current pipe.
func (p *Pipe) AppendEnv(env ...string) *Pipe {
p.env = append(p.env, env...)
return p
}

// WithEnvKV sets the environment variable key-value pair for the current pipe.
func (p *Pipe) WithEnvKV(key, value string) *Pipe {
p.env = append(p.env, key+"="+value)
return p
}

func (p *Pipe) WithStderr(w io.Writer) *Pipe {
p.stderr = w
p.Pipe = p.Pipe.WithStderr(w)
Expand Down Expand Up @@ -106,6 +132,9 @@ func (p *Pipe) execContext(
if p.wd != "" {
cmd.Dir = p.wd
}
if len(p.env) > 0 {
cmd.Env = p.env
}

return cmd
}
Expand Down

0 comments on commit bca9b77

Please sign in to comment.