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"
2022-02-09 12:18:58 +08:00
"fmt"
2021-11-17 00:03:21 +08:00
"log"
"os/exec"
"strings"
"syscall"
"time"
)
// 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 {
Log(fmt.Sprintf("error running command: %s", command))
Log(strings.TrimSuffix(string(out), "\n"))
}
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 {
log.Println("error running command:", command)
log.Println(strings.TrimSuffix(string(out), "\n"))
}
return string(out), err
}