2018-11-08 12:42:13 +00:00
|
|
|
package ser
|
|
|
|
|
|
|
|
import (
|
2018-11-18 21:49:11 +00:00
|
|
|
"git.xdrm.io/go/nix-amer/internal/exec"
|
2018-11-08 12:42:13 +00:00
|
|
|
)
|
|
|
|
|
2018-11-11 20:38:16 +00:00
|
|
|
type systemd struct{ exec exec.Executor }
|
2018-11-08 12:42:13 +00:00
|
|
|
|
|
|
|
// available actions
|
2018-11-14 19:11:07 +00:00
|
|
|
var actions = []string{"enable", "disable", "status", "start", "stop", "reload", "restart"}
|
2018-11-08 12:42:13 +00:00
|
|
|
|
2018-11-11 20:38:16 +00:00
|
|
|
func (d *systemd) SetExecutor(_exec exec.Executor) {
|
|
|
|
d.exec = _exec
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
func (d systemd) Name() string { return "systemctl" }
|
2018-11-08 12:42:13 +00:00
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
func (d systemd) Exec(action, service string) error {
|
2018-11-08 12:42:13 +00:00
|
|
|
|
|
|
|
// 1. fail if unknown action
|
|
|
|
known := false
|
|
|
|
for _, act := range actions {
|
|
|
|
if action == act {
|
|
|
|
known = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !known {
|
|
|
|
return ErrUnknownAction
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. execute command
|
2018-11-11 20:38:16 +00:00
|
|
|
return d.exec.Command("systemctl", action, service).Run()
|
2018-11-08 12:42:13 +00:00
|
|
|
|
|
|
|
}
|