61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package instruction
|
|
|
|
import (
|
|
"fmt"
|
|
"git.xdrm.io/go/nix-amer/internal/pkg"
|
|
"git.xdrm.io/go/nix-amer/internal/ser"
|
|
)
|
|
|
|
// T is the instruction common interface
|
|
type T interface {
|
|
// Raw input line
|
|
Raw() string
|
|
// Build the instruction with input arguments
|
|
Build(string) error
|
|
// Exec the given instruction
|
|
Exec(ExecutionContext) ([]byte, error)
|
|
}
|
|
|
|
type ExecutionContext struct {
|
|
PackageManager pkg.PackageManager
|
|
ServiceManager ser.ServiceManager
|
|
}
|
|
|
|
// CreateContext creates an execution contet with the given package-manager and service-manager
|
|
// default values are taken from each go package (pkg, ser)
|
|
func CreateContext(_pkg, _ser string) (*ExecutionContext, error) {
|
|
|
|
// 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
|
|
}
|
|
|
|
// 3. load managers
|
|
pkg, err := pkg.Load(_pkg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("package manager: %s", err)
|
|
}
|
|
ser, err := ser.Load(_ser)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("service manager: %s", err)
|
|
}
|
|
|
|
// 4. build context
|
|
return &ExecutionContext{
|
|
PackageManager: pkg,
|
|
ServiceManager: ser,
|
|
}, nil
|
|
|
|
}
|