nix-amer/internal/instruction/service.go

63 lines
1.1 KiB
Go

package instruction
import (
"fmt"
"strings"
)
var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"}
type service struct {
raw string
Action string
Services []string
}
func (d *service) Raw() string { return d.raw }
func (d *service) Build(_args string) error {
// 1. extract action (sub command)
split := strings.Split(_args, " ")
// 2. check action
if len(split) < 2 {
return fmt.Errorf("invalid syntax, missing action or unit")
}
// 3. fail if unknown action
known := false
for _, act := range actions {
if split[0] == act {
known = true
break
}
}
if !known {
return fmt.Errorf("unknown action '%s'", split[0])
}
d.Action = split[0]
// 4. Store services
d.Services = split[1:]
d.raw = _args
return nil
}
func (d service) Exec(ctx ExecutionContext) ([]byte, error) {
// delete all packages
for _, service := range d.Services {
if err := ctx.ServiceManager.Exec(d.Action, service); err != nil {
return nil, fmt.Errorf("cannot %s '%s' | %s", d.Action, service, err)
}
}
return nil, nil
}