2021-11-17 09:10:11 +08:00
|
|
|
package ncutils
|
|
|
|
|
|
|
|
import (
|
2021-12-13 07:04:41 +08:00
|
|
|
"embed"
|
2021-11-18 10:57:27 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-11-17 09:10:11 +08:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2021-11-18 10:57:27 +08:00
|
|
|
"syscall"
|
2022-03-21 20:00:10 +08:00
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-11-17 09:10:11 +08:00
|
|
|
)
|
|
|
|
|
2021-12-13 07:04:41 +08:00
|
|
|
//go:embed windowsdaemon/winsw.exe
|
|
|
|
var winswContent embed.FS
|
|
|
|
|
2021-11-17 09:10:11 +08:00
|
|
|
// RunCmd - runs a local command
|
|
|
|
func RunCmd(command string, printerr bool) (string, error) {
|
|
|
|
args := strings.Fields(command)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
cmd.Wait()
|
2021-11-18 10:57:27 +08:00
|
|
|
//cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/C \"" + command + "\""}
|
2021-11-17 09:10:11 +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 09:10:11 +08:00
|
|
|
}
|
|
|
|
return string(out), err
|
|
|
|
}
|
2021-11-18 10:57:27 +08:00
|
|
|
|
|
|
|
// RunCmd - runs a local command
|
|
|
|
func RunCmdFormatted(command string, printerr bool) (string, error) {
|
|
|
|
var comSpec = os.Getenv("COMSPEC")
|
|
|
|
if comSpec == "" {
|
|
|
|
comSpec = os.Getenv("SystemRoot") + "\\System32\\cmd.exe"
|
|
|
|
}
|
|
|
|
cmd := exec.Command(comSpec)
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: "/C \"" + command + "\""}
|
|
|
|
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"))
|
2021-11-18 10:57:27 +08:00
|
|
|
}
|
|
|
|
return string(out), err
|
|
|
|
}
|
|
|
|
|
2021-12-13 07:04:41 +08:00
|
|
|
// GetEmbedded - Gets the Windows daemon creator
|
|
|
|
func GetEmbedded() error {
|
|
|
|
data, err := winswContent.ReadFile("windowsdaemon/winsw.exe")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileName := fmt.Sprintf("%swinsw.exe", GetNetclientPathSpecific())
|
|
|
|
err = os.WriteFile(fileName, data, 0700)
|
|
|
|
if err != nil {
|
2022-03-21 19:30:56 +08:00
|
|
|
logger.Log(0, "could not mount winsw.exe")
|
2021-12-13 07:04:41 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|