github.com/rawahars/moby@v24.0.4+incompatible/opts/runtime.go (about)

     1  package opts // import "github.com/docker/docker/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  	k, v, ok := strings.Cut(val, "=")
    33  	if !ok {
    34  		return fmt.Errorf("invalid runtime argument: %s", val)
    35  	}
    36  
    37  	// TODO(thaJeztah): this should not accept spaces.
    38  	k = strings.TrimSpace(k)
    39  	v = strings.TrimSpace(v)
    40  	if k == "" || v == "" {
    41  		return fmt.Errorf("invalid runtime argument: %s", val)
    42  	}
    43  
    44  	// TODO(thaJeztah): this should not be case-insensitive.
    45  	k = strings.ToLower(k)
    46  	if k == o.stockRuntimeName {
    47  		return fmt.Errorf("runtime name '%s' is reserved", o.stockRuntimeName)
    48  	}
    49  
    50  	if _, ok := (*o.values)[k]; ok {
    51  		return fmt.Errorf("runtime '%s' was already defined", k)
    52  	}
    53  
    54  	(*o.values)[k] = types.Runtime{Path: v}
    55  
    56  	return nil
    57  }
    58  
    59  // String returns Runtime values as a string.
    60  func (o *RuntimeOpt) String() string {
    61  	var out []string
    62  	for k := range *o.values {
    63  		out = append(out, k)
    64  	}
    65  
    66  	return fmt.Sprintf("%v", out)
    67  }
    68  
    69  // GetMap returns a map of Runtimes (name: path)
    70  func (o *RuntimeOpt) GetMap() map[string]types.Runtime {
    71  	if o.values != nil {
    72  		return *o.values
    73  	}
    74  
    75  	return map[string]types.Runtime{}
    76  }
    77  
    78  // Type returns the type of the option
    79  func (o *RuntimeOpt) Type() string {
    80  	return "runtime"
    81  }