platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
47
selective-vpn-api/app/dnscfg/systemd.go
Normal file
47
selective-vpn-api/app/dnscfg/systemd.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package dnscfg
|
||||
|
||||
import "strings"
|
||||
|
||||
type RunCommandFunc func(name string, args ...string) (stdout string, stderr string, exitCode int, err error)
|
||||
|
||||
type CmdResult struct {
|
||||
OK bool
|
||||
ExitCode int
|
||||
Stdout string
|
||||
Stderr string
|
||||
Message string
|
||||
}
|
||||
|
||||
func UnitState(run RunCommandFunc, unit string) string {
|
||||
if run == nil {
|
||||
return "unknown"
|
||||
}
|
||||
stdout, _, _, _ := run("systemctl", "is-active", strings.TrimSpace(unit))
|
||||
st := strings.TrimSpace(stdout)
|
||||
if st == "" {
|
||||
return "unknown"
|
||||
}
|
||||
return st
|
||||
}
|
||||
|
||||
func RunUnitAction(run RunCommandFunc, unit, action string) CmdResult {
|
||||
if run == nil {
|
||||
return CmdResult{
|
||||
OK: false,
|
||||
Message: "run command func is nil",
|
||||
}
|
||||
}
|
||||
stdout, stderr, exitCode, err := run("systemctl", strings.TrimSpace(action), strings.TrimSpace(unit))
|
||||
res := CmdResult{
|
||||
OK: err == nil && exitCode == 0,
|
||||
ExitCode: exitCode,
|
||||
Stdout: stdout,
|
||||
Stderr: stderr,
|
||||
}
|
||||
if err != nil {
|
||||
res.Message = err.Error()
|
||||
} else {
|
||||
res.Message = strings.TrimSpace(unit) + " " + strings.TrimSpace(action) + " done"
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user