2018-11-08 12:42:13 +00:00
|
|
|
package ser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
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 18:01:00 +00:00
|
|
|
// ErrUnknownManager is raised when the asked manager does not exist
|
2018-11-08 12:42:13 +00:00
|
|
|
var ErrUnknownManager = errors.New("unknown service manager")
|
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
// ErrNotInstalled is raised when the candidate service manager is not installed
|
|
|
|
// on the host system (use command-line -dry-run to avoid execution errors and just
|
|
|
|
// check your buildfile syntax)
|
|
|
|
var ErrNotInstalled = errors.New("no candidate installed")
|
|
|
|
|
|
|
|
// Load a specific service-manager by its name
|
2018-11-11 20:38:16 +00:00
|
|
|
func Load(_manager string, _exec exec.Executor) (ServiceManager, error) {
|
2018-11-08 12:42:13 +00:00
|
|
|
|
2018-11-10 11:37:31 +00:00
|
|
|
// 1. create manager (fail if unknown)
|
2018-11-08 12:42:13 +00:00
|
|
|
var manager ServiceManager
|
|
|
|
switch _manager {
|
|
|
|
case "systemd":
|
2018-11-11 18:01:00 +00:00
|
|
|
manager = new(systemd)
|
2018-11-08 12:42:13 +00:00
|
|
|
default:
|
|
|
|
return nil, ErrUnknownManager
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. fail if not installed
|
2018-11-10 11:37:31 +00:00
|
|
|
// if exec.Command("which", manager.Name()).Run() != nil {
|
|
|
|
// return nil, ErrNotInstalled
|
|
|
|
// }
|
2018-11-08 12:42:13 +00:00
|
|
|
|
2018-11-11 20:38:16 +00:00
|
|
|
manager.SetExecutor(_exec)
|
2018-11-08 12:42:13 +00:00
|
|
|
return manager, nil
|
|
|
|
|
|
|
|
}
|