netmaker/netclient/ncutils/netclientutils_freebsd.go

51 lines
1.2 KiB
Go
Raw Normal View History

2021-11-17 00:03:21 +08:00
package ncutils
import (
"context"
"os/exec"
"strings"
"syscall"
"time"
2022-03-21 19:30:56 +08:00
"github.com/gravitl/netmaker/logger"
2021-11-17 00:03:21 +08:00
)
// RunCmdFormatted - run a command formatted for freebsd
2021-11-18 13:01:05 +08:00
func RunCmdFormatted(command string, printerr bool) (string, error) {
2022-02-09 12:18:58 +08:00
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)
2022-02-09 12:24:42 +08:00
cmd.Start()
2022-02-09 12:18:58 +08:00
cmd.Wait()
out, err := cmd.CombinedOutput()
if err != nil && printerr {
2022-03-21 19:30:56 +08:00
logger.Log(0, "error running command: ", command)
logger.Log(0, strings.TrimSuffix(string(out), "\n"))
2022-02-09 12:18:58 +08:00
}
return string(out), err
2021-11-18 13:01:05 +08:00
}
// GetEmbedded - if files required for freebsd, put here
func GetEmbedded() error {
return nil
}
2021-11-17 00:03:21 +08:00
// Runs Commands for FreeBSD
func RunCmd(command string, printerr bool) (string, error) {
args := strings.Fields(command)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.Command(args[0], args[1:]...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
go func() {
2022-02-09 12:30:50 +08:00
<-ctx.Done()
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
2021-11-17 00:03:21 +08:00
}()
out, err := cmd.CombinedOutput()
if err != nil && printerr {
2022-03-21 19:30:56 +08:00
logger.Log(0, "error running command:", command)
logger.Log(0, strings.TrimSuffix(string(out), "\n"))
2021-11-17 00:03:21 +08:00
}
return string(out), err
}