mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
34 lines
674 B
Go
34 lines
674 B
Go
package cmds
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/v2rayA/v2rayA/pkg/util/log"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func IsCommandValid(command string) bool {
|
|
_, err := exec.LookPath(command)
|
|
return err == nil
|
|
}
|
|
|
|
func ExecCommands(commands string, stopWhenError bool) error {
|
|
lines := strings.Split(commands, "\n")
|
|
var e error
|
|
for _, line := range lines {
|
|
line = strings.TrimSpace(line)
|
|
if len(line) <= 0 || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
out, err := exec.Command("sh", "-c", line).CombinedOutput()
|
|
if err != nil {
|
|
e = fmt.Errorf("ExecCommands: %v %v: %w", line, string(out), err)
|
|
log.Trace("%v", e)
|
|
if stopWhenError {
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
return e
|
|
}
|