2021-03-03 04:43:38 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-06-06 05:51:34 +00:00
|
|
|
"log/slog"
|
2021-03-03 04:43:38 +00:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
stdout = "stdout"
|
|
|
|
stderr = "stderr"
|
|
|
|
)
|
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
func ExecProcessInDir(logger *slog.Logger, workdir string, command string, args ...string) (string, error) {
|
2021-03-03 04:43:38 +00:00
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
if workdir != "" {
|
|
|
|
cmd.Dir = workdir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the command
|
2024-06-06 05:51:34 +00:00
|
|
|
logger.Debug("Running command", "cmd", command, "args", args)
|
2021-03-03 04:43:38 +00:00
|
|
|
output, err := cmd.Output()
|
|
|
|
|
|
|
|
return strings.TrimSpace(string(output)), err
|
|
|
|
}
|
|
|
|
|
2024-06-06 05:51:34 +00:00
|
|
|
func ExecProcess(logger *slog.Logger, command string, args ...string) (string, error) {
|
|
|
|
return ExecProcessInDir(logger, "", command, args...)
|
2021-03-03 04:43:38 +00:00
|
|
|
}
|