Implement cmdrun config parameter

This commit is contained in:
eleksir
2025-05-19 12:19:25 +03:00
parent 757fb5e57e
commit 32d865e24e
7 changed files with 315 additions and 15 deletions

View File

@@ -1,9 +1,11 @@
package lib
import (
"bytes"
"log"
"os"
"os/exec"
"strings"
"syscall"
"time"
)
@@ -55,3 +57,41 @@ func (c *MyConfig) CleanZombies() {
time.Sleep(1 * time.Minute)
}
}
// RunProcess runs given command and returns contens of command stdout. On error prints error on stderr and returns
// empty syting.
func RunProcess(command []string) string {
var (
cmd *exec.Cmd
stdout, stderr bytes.Buffer
)
switch len(command) {
case 1:
cmd = exec.Command(command[0], "")
default:
cmd = exec.Command(command[0], command[1:]...)
}
cmd.Dir = "/"
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
if stdout.String() != "" {
log.Printf("Unable to run '%s': %s, %s", strings.Join(command, " "), err, stdout.String())
} else {
log.Printf("Unable to run '%s': %s", strings.Join(command, " "), err)
}
return ""
} else {
if stderr.String() != "" {
log.Printf("%s: %s", strings.Join(command, " "), stderr.String())
}
}
return strings.TrimRight(stdout.String(), "\n")
}