2018-11-08 12:41:07 +00:00
|
|
|
package instruction
|
|
|
|
|
|
|
|
import (
|
2018-11-10 11:37:31 +00:00
|
|
|
"fmt"
|
2018-11-18 21:49:11 +00:00
|
|
|
"git.xdrm.io/go/nix-amer/internal/exec"
|
|
|
|
"git.xdrm.io/go/nix-amer/internal/pkg"
|
|
|
|
"git.xdrm.io/go/nix-amer/internal/ser"
|
2018-11-08 12:41:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// T is the instruction common interface
|
|
|
|
type T interface {
|
2018-11-10 12:20:10 +00:00
|
|
|
// Raw input line
|
|
|
|
Raw() string
|
2018-11-08 12:41:07 +00:00
|
|
|
// Build the instruction with input arguments
|
|
|
|
Build(string) error
|
|
|
|
// Exec the given instruction
|
|
|
|
Exec(ExecutionContext) ([]byte, error)
|
2018-11-14 19:11:07 +00:00
|
|
|
// DryRun checks the success of the given instruction without actually running it (non-destructive)
|
|
|
|
DryRun(ExecutionContext) ([]byte, error)
|
2018-11-08 12:41:07 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
// ExecutionContext contains system-specific drivers to manage the host
|
2018-11-08 12:41:07 +00:00
|
|
|
type ExecutionContext struct {
|
|
|
|
PackageManager pkg.PackageManager
|
|
|
|
ServiceManager ser.ServiceManager
|
2018-11-12 22:38:37 +00:00
|
|
|
Executor exec.Executor
|
|
|
|
Alias map[string]string
|
2018-11-08 12:41:07 +00:00
|
|
|
}
|
2018-11-10 11:37:31 +00:00
|
|
|
|
|
|
|
// CreateContext creates an execution contet with the given package-manager and service-manager
|
|
|
|
// default values are taken from each go package (pkg, ser)
|
2018-11-12 22:38:37 +00:00
|
|
|
// if _exec is set, it will override the default (os) executor
|
|
|
|
func CreateContext(_pkg, _ser string, _exec ...exec.Executor) (*ExecutionContext, error) {
|
2018-11-10 11:37:31 +00:00
|
|
|
|
|
|
|
// 1. fail if no value and no defaults
|
|
|
|
if len(_pkg)+len(pkg.DefaultManager) < 1 {
|
|
|
|
return nil, fmt.Errorf("missing package manager")
|
|
|
|
}
|
|
|
|
if len(_ser)+len(ser.DefaultManager) < 1 {
|
|
|
|
return nil, fmt.Errorf("missing service manager")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. set default
|
|
|
|
if len(_pkg) < 1 {
|
|
|
|
_pkg = pkg.DefaultManager
|
|
|
|
}
|
|
|
|
if len(_ser) < 1 {
|
|
|
|
_ser = ser.DefaultManager
|
|
|
|
}
|
|
|
|
|
2018-11-12 22:38:37 +00:00
|
|
|
var executor exec.Executor = new(exec.Default)
|
|
|
|
|
|
|
|
// 3. load custom executor (optional)
|
|
|
|
if len(_exec) > 0 && _exec[0] != nil {
|
|
|
|
executor = _exec[0]
|
|
|
|
}
|
2018-11-11 20:38:16 +00:00
|
|
|
|
2018-11-12 22:38:37 +00:00
|
|
|
// 4. load managers
|
2018-11-11 20:38:16 +00:00
|
|
|
pkg, err := pkg.Load(_pkg, executor)
|
2018-11-10 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("package manager: %s", err)
|
|
|
|
}
|
2018-11-11 20:38:16 +00:00
|
|
|
ser, err := ser.Load(_ser, executor)
|
2018-11-10 11:37:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("service manager: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. build context
|
|
|
|
return &ExecutionContext{
|
|
|
|
PackageManager: pkg,
|
|
|
|
ServiceManager: ser,
|
2018-11-12 22:38:37 +00:00
|
|
|
Executor: executor,
|
|
|
|
Alias: make(map[string]string),
|
2018-11-10 11:37:31 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|