Files
vpngate/pkg/exec/run.go
renovate[bot] fb9c4800ae Update golangci/golangci-lint-action action to v9 (#161)
* Update golangci/golangci-lint-action action to v9

* Bump golangci-lint to 2.6.2

* Refactor to pass golangcli-lint

* Fix tests

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Dave Gallant <dave.gallant@gmail.com>
Co-authored-by: Dave Gallant <davegallant@proton.me>
2025-11-23 09:13:17 -05:00

48 lines
1.1 KiB
Go

package exec
import (
"bufio"
"os"
"os/exec"
"strings"
"github.com/rs/zerolog/log"
)
// Run executes a command in workDir and returns stdout and error.
// The spawned process will exit upon termination of this application
// to ensure a clean exit
func Run(path string, workDir string, args ...string) error {
_, err := exec.LookPath(path)
if err != nil {
log.Error().Msgf("%s is required, please install it", path)
os.Exit(1)
}
cmd := exec.Command(path, args...)
cmd.Dir = workDir
log.Debug().Msg("Executing " + strings.Join(cmd.Args, " "))
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal().Msgf("Failed to get stdout pipe: %v", err)
}
if err := cmd.Start(); err != nil {
log.Fatal().Msgf("Failed to start command: %v", err)
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
log.Debug().Msg(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal().Msgf("Error reading stdout: %v", err)
}
if err := cmd.Wait(); err != nil {
log.Fatal().Msgf("Command finished with error: %v", err)
return err
}
return nil
}