mirror of
https://github.com/davegallant/vpngate.git
synced 2025-08-05 08:13:41 +00:00
Add initial prototype (#1)
* Add survey library to select server * Add speedtest * Add --random flag to connect * Add list command * Cache server list * Tail the openvpn logs so that it appears in vpngate logs * Add goreleaser action * Add golangci-lint action
This commit is contained in:
43
pkg/exec/run.go
Normal file
43
pkg/exec/run.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/juju/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Run executes anycommand in workDir and returns stdout and error.
|
||||
func Run(path string, workDir string, args ...string) (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
|
||||
stdout := &bytes.Buffer{}
|
||||
stderr := &bytes.Buffer{}
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
log.Debug().Msgf("Executing " + strings.Join(cmd.Args, " "))
|
||||
err = cmd.Run()
|
||||
output := strings.TrimSpace(stdout.String())
|
||||
errOut := strings.TrimSpace(stderr.String())
|
||||
if output != "" {
|
||||
log.Debug().Msgf(output)
|
||||
}
|
||||
if errOut != "" {
|
||||
log.Debug().Msgf(errOut)
|
||||
}
|
||||
if _, ok := err.(*exec.ExitError); !ok {
|
||||
return output, errors.Trace(err)
|
||||
}
|
||||
if err != nil {
|
||||
return output, errors.Annotatef(err, path, cmd.Args, errOut)
|
||||
}
|
||||
return output, nil
|
||||
}
|
Reference in New Issue
Block a user