github.com/ggreg80/ketos@v0.0.0-20171109040536-049616f51ddb/cmd/chr/engine.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"os"
     6  )
     7  
     8  type ChrootExecutor interface {
     9  	Execute(repoPath, tagName string, userCommand []string) error
    10  }
    11  
    12  type ExecutorFunc func(repoPath, tagName string, userCommand []string) error
    13  
    14  func (f ExecutorFunc) Execute(repoPath, tagName string,
    15  	userCommand []string) error {
    16  
    17  	return f(repoPath, tagName, userCommand)
    18  }
    19  
    20  func NewChrootExecutor(engineName string) (ChrootExecutor, error) {
    21  
    22  	factory, exists := executorFactories[engineName]
    23  	if !exists {
    24  		return nil, errors.New("engine factory doesn't exists")
    25  	}
    26  
    27  	return factory(os.Environ())
    28  }
    29  
    30  type ChrootExecutorFactory func(env []string) (ChrootExecutor, error)
    31  
    32  var (
    33  	executorFactories = map[string]ChrootExecutorFactory{}
    34  )
    35  
    36  func AddExecutor(name string, factory ChrootExecutorFactory) {
    37  	executorFactories[name] = factory
    38  }