buildr/internal/sh/command.go
Peter c8b34bc41d
Some checks failed
continuous-integration/drone/push Build is failing
feat: allow process start from plugins
2023-06-29 21:18:02 +02:00

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
}