package instruction import ( "fmt" "git.xdrm.io/go/nix-amer/internal/exec" "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) } // Executor is the common interface to execute commands type Executor interface { Execute(...string) error } // ExecutionContext contains system-specific drivers to manage the host type ExecutionContext struct { ExecContext Executor 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 } executor := new(exec.Default) // 3. load managers pkg, err := pkg.Load(_pkg, executor) if err != nil { return nil, fmt.Errorf("package manager: %s", err) } ser, err := ser.Load(_ser, executor) if err != nil { return nil, fmt.Errorf("service manager: %s", err) } // 4. build context return &ExecutionContext{ PackageManager: pkg, ServiceManager: ser, }, nil }