nix-amer/internal/instruction/service.go

63 lines
1.1 KiB
Go
Raw Normal View History

package instruction
import (
"fmt"
"strings"
)
var actions = []string{"enable", "disable", "start", "stop", "reload", "restart"}
type service struct {
2018-11-10 12:20:10 +00:00
raw string
Action string
Services []string
}
func (d *service) Raw() string { return strings.Join([]string{"service", d.raw}, " ") }
2018-11-10 12:20:10 +00:00
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:]
2018-11-10 12:20:10 +00:00
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
}