49 lines
991 B
Go
49 lines
991 B
Go
package sh
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
|
|
"code.icb4dc0.de/buildr/buildr/modules"
|
|
)
|
|
|
|
type Command interface {
|
|
Run() error
|
|
Env() []string
|
|
AddEnv(vars map[string]string)
|
|
SetStdIn(reader io.Reader)
|
|
SetWorkingDir(dir string)
|
|
SetStdout(writer io.Writer)
|
|
SetStderr(writer io.Writer)
|
|
}
|
|
|
|
func PrepareCommand(ctx modules.ExecutionContext, name string, args ...string) (Command, error) {
|
|
const keyAndValue = 2
|
|
cmd := exec.CommandContext(ctx, name, args...)
|
|
cmd.Dir = ctx.WorkingDir()
|
|
|
|
currentEnv := os.Environ()
|
|
|
|
for i := range currentEnv {
|
|
if strings.HasPrefix(currentEnv[i], "PATH") {
|
|
currentPath := strings.Split(currentEnv[i], "=")
|
|
if len(currentPath) != keyAndValue {
|
|
return nil, fmt.Errorf("failed to update PATH: %s", currentEnv[i])
|
|
}
|
|
|
|
currentEnv[i] = fmt.Sprintf("PATH=%s:%s", ctx.BinariesDir(), currentPath[1])
|
|
}
|
|
}
|
|
|
|
cmd.Env = currentEnv
|
|
cmd.Stdout = ctx.StdOut()
|
|
cmd.Stderr = ctx.StdErr()
|
|
|
|
return &baseCommand{
|
|
Cmd: cmd,
|
|
}, nil
|
|
}
|