netclientutils_freebsd.go 925 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package ncutils
  2. import (
  3. "context"
  4. "log"
  5. "os/exec"
  6. "strings"
  7. "syscall"
  8. "time"
  9. )
  10. // RunCmdFormatted - run a command formatted for freebsd
  11. func RunCmdFormatted(command string, printerr bool) (string, error) {
  12. return "", nil
  13. }
  14. // GetEmbedded - if files required for freebsd, put here
  15. func GetEmbedded() error {
  16. return nil
  17. }
  18. // Runs Commands for FreeBSD
  19. func RunCmd(command string, printerr bool) (string, error) {
  20. args := strings.Fields(command)
  21. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  22. defer cancel()
  23. cmd := exec.Command(args[0], args[1:]...)
  24. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  25. go func() {
  26. <-ctx.Done()
  27. _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
  28. }()
  29. out, err := cmd.CombinedOutput()
  30. if err != nil && printerr {
  31. log.Println("error running command:", command)
  32. log.Println(strings.TrimSuffix(string(out), "\n"))
  33. }
  34. return string(out), err
  35. }