2018-11-08 12:41:07 +00:00
|
|
|
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
|
2018-11-08 12:41:07 +00:00
|
|
|
Action string
|
|
|
|
Services []string
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:38:37 +00:00
|
|
|
func (d *service) Raw() string { return strings.Join([]string{"service", d.raw}, " ") }
|
2018-11-10 12:20:10 +00:00
|
|
|
|
2018-11-08 12:41:07 +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
|
2018-11-08 12:41:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2018-11-14 19:11:07 +00:00
|
|
|
func (d service) DryRun(ctx ExecutionContext) ([]byte, error) {
|
|
|
|
|
|
|
|
// fail if a service does not exist
|
|
|
|
for _, service := range d.Services {
|
|
|
|
|
|
|
|
if err := ctx.ServiceManager.Exec("status", service); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot find service '%s' | %s", service, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
|
|
|
}
|