57 lines
970 B
Go
57 lines
970 B
Go
//go:build linux
|
|
// +build linux
|
|
|
|
package run
|
|
|
|
import (
|
|
"bytes"
|
|
"os/exec"
|
|
|
|
"ems.agt/lib/log"
|
|
)
|
|
|
|
func ExecCmd(command, path string) ([]byte, error) {
|
|
log.Debug("Exec command:", command)
|
|
|
|
cmd := exec.Command("/bin/bash", "-c", command)
|
|
cmd.Dir = path
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
|
|
return out, nil
|
|
}
|
|
|
|
func ExecShell(command string) error {
|
|
in := bytes.NewBuffer(nil)
|
|
cmd := exec.Command("sh")
|
|
cmd.Stdin = in
|
|
in.WriteString(command)
|
|
in.WriteString("exit\n")
|
|
if err := cmd.Start(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ExecOsCmd(command, os string) error {
|
|
log.Debugf("Exec %s command:%s", os, command)
|
|
|
|
var cmd *exec.Cmd
|
|
switch os {
|
|
case "Linux":
|
|
cmd = exec.Command(command)
|
|
case "Windows":
|
|
cmd = exec.Command("cmd", "/C", command)
|
|
}
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
log.Tracef("Exec output: %v", string(out))
|
|
if err != nil {
|
|
log.Error("exe cmd error: ", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|