netmaker/netclient/ncutils/netclientutils_freebsd.go
Matthew R. Kasun e88d34ac96 update RunCmd
2022-02-08 23:12:53 -05:00

45 lines
1 KiB
Go

package ncutils
import (
"context"
"log"
"os/exec"
"strings"
"syscall"
"time"
)
// RunCmdFormatted - run a command formatted for freebsd
func RunCmdFormatted(command string, printerr bool) (string, error) {
return "", nil
}
// GetEmbedded - if files required for freebsd, put here
func GetEmbedded() error {
return nil
}
// 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() {
select {
case <-ctx.Done():
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
case <-time.After(time.Second * 2):
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
}()
cmd.Wait()
out, err := cmd.CombinedOutput()
if err != nil && printerr {
log.Println("error running command:", command)
log.Println(strings.TrimSuffix(string(out), "\n"))
}
return string(out), err
}