github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/opts/runtime.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/docker/docker/api/types"
     8  )
     9  
    10  // RuntimeOpt defines a map of Runtimes
    11  type RuntimeOpt struct {
    12  	name             string
    13  	stockRuntimeName string
    14  	values           *map[string]types.Runtime
    15  }
    16  
    17  // NewNamedRuntimeOpt creates a new RuntimeOpt
    18  func NewNamedRuntimeOpt(name string, ref *map[string]types.Runtime, stockRuntime string) *RuntimeOpt {
    19  	if ref == nil {
    20  		ref = &map[string]types.Runtime{}
    21  	}
    22  	return &RuntimeOpt{name: name, values: ref, stockRuntimeName: stockRuntime}
    23  }
    24  
    25  // Name returns the name of the NamedListOpts in the configuration.
    26  func (o *RuntimeOpt) Name() string {
    27  	return o.name
    28  }
    29  
    30  // Set validates and updates the list of Runtimes
    31  func (o *RuntimeOpt) Set(val string) error {
    32  	parts := strings.SplitN(val, "=", 2)
    33  	if len(parts) != 2 {
    34  		return fmt.Errorf("invalid runtime argument: %s", val)
    35  	}
    36  
    37  	parts[0] = strings.TrimSpace(parts[0])
    38  	parts[1] = strings.TrimSpace(parts[1])
    39  	if parts[0] == "" || parts[1] == "" {
    40  		return fmt.Errorf("invalid runtime argument: %s", val)
    41  	}
    42  
    43  	parts[0] = strings.ToLower(parts[0])
    44  	if parts[0] == o.stockRuntimeName {
    45  		return fmt.Errorf("runtime name '%s' is reserved", o.stockRuntimeName)
    46  	}
    47  
    48  	if _, ok := (*o.values)[parts[0]]; ok {
    49  		return fmt.Errorf("runtime '%s' was already defined", parts[0])
    50  	}
    51  
    52  	(*o.values)[parts[0]] = types.Runtime{Path: parts[1]}
    53  
    54  	return nil
    55  }
    56  
    57  // String returns Runtime values as a string.
    58  func (o *RuntimeOpt) String() string {
    59  	var out []string
    60  	for k := range *o.values {
    61  		out = append(out, k)
    62  	}
    63  
    64  	return fmt.Sprintf("%v", out)
    65  }
    66  
    67  // GetMap returns a map of Runtimes (name: path)
    68  func (o *RuntimeOpt) GetMap() map[string]types.Runtime {
    69  	if o.values != nil {
    70  		return *o.values
    71  	}
    72  
    73  	return map[string]types.Runtime{}
    74  }
    75  
    76  // Type returns the type of the option
    77  func (o *RuntimeOpt) Type() string {
    78  	return "runtime"
    79  }