2018-11-08 12:42:13 +00:00
|
|
|
package ser
|
|
|
|
|
|
|
|
import (
|
2018-11-13 12:10:55 +00:00
|
|
|
"github.com/xdrm-brackets/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
|
|
|
|
var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"}
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|