nix-amer/internal/ser/loader.go

46 lines
865 B
Go

package ser
import (
"errors"
"os/exec"
)
var ErrUnknownManager = errors.New("unknown service manager")
var ErrNoCandidateInstalled = errors.New("no package-manager candidate installed")
// available service managers
// var available = []string{"systemd", "init"}
var available = []string{"systemd"}
func Load(_manager string) (ServiceManager, error) {
// 1. fail if unknown manager
known := false
for _, mgr := range available {
if _manager == mgr {
known = true
break
}
}
if !known {
return nil, ErrUnknownManager
}
// 2. Create manager accordingly
var manager ServiceManager
switch _manager {
case "systemd":
manager = new(Systemd)
default:
return nil, ErrUnknownManager
}
// 2. fail if not installed
if exec.Command("which", manager.Name()).Run() == nil {
return nil, ErrNoCandidateInstalled
}
return manager, nil
}