2018-02-05 21:05:59 +00:00
|
|
|
package opts // import "github.com/docker/docker/opts"
|
2016-05-23 21:49:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-05-23 21:49:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RuntimeOpt defines a map of Runtimes
|
|
|
|
type RuntimeOpt struct {
|
2016-06-20 19:14:27 +00:00
|
|
|
name string
|
|
|
|
stockRuntimeName string
|
|
|
|
values *map[string]types.Runtime
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewNamedRuntimeOpt creates a new RuntimeOpt
|
2016-06-20 19:14:27 +00:00
|
|
|
func NewNamedRuntimeOpt(name string, ref *map[string]types.Runtime, stockRuntime string) *RuntimeOpt {
|
2016-05-23 21:49:50 +00:00
|
|
|
if ref == nil {
|
|
|
|
ref = &map[string]types.Runtime{}
|
|
|
|
}
|
2016-06-20 19:14:27 +00:00
|
|
|
return &RuntimeOpt{name: name, values: ref, stockRuntimeName: stockRuntime}
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the NamedListOpts in the configuration.
|
|
|
|
func (o *RuntimeOpt) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set validates and updates the list of Runtimes
|
|
|
|
func (o *RuntimeOpt) Set(val string) error {
|
2022-11-01 10:13:45 +00:00
|
|
|
k, v, ok := strings.Cut(val, "=")
|
|
|
|
if !ok {
|
2016-05-23 21:49:50 +00:00
|
|
|
return fmt.Errorf("invalid runtime argument: %s", val)
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:13:45 +00:00
|
|
|
// TODO(thaJeztah): this should not accept spaces.
|
|
|
|
k = strings.TrimSpace(k)
|
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
if k == "" || v == "" {
|
2016-05-23 21:49:50 +00:00
|
|
|
return fmt.Errorf("invalid runtime argument: %s", val)
|
|
|
|
}
|
|
|
|
|
2022-11-01 10:13:45 +00:00
|
|
|
// TODO(thaJeztah): this should not be case-insensitive.
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
if k == o.stockRuntimeName {
|
2016-06-20 19:14:27 +00:00
|
|
|
return fmt.Errorf("runtime name '%s' is reserved", o.stockRuntimeName)
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:13:45 +00:00
|
|
|
if _, ok := (*o.values)[k]; ok {
|
|
|
|
return fmt.Errorf("runtime '%s' was already defined", k)
|
2016-05-23 21:49:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 10:13:45 +00:00
|
|
|
(*o.values)[k] = types.Runtime{Path: v}
|
2016-05-23 21:49:50 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns Runtime values as a string.
|
|
|
|
func (o *RuntimeOpt) String() string {
|
|
|
|
var out []string
|
|
|
|
for k := range *o.values {
|
|
|
|
out = append(out, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMap returns a map of Runtimes (name: path)
|
|
|
|
func (o *RuntimeOpt) GetMap() map[string]types.Runtime {
|
|
|
|
if o.values != nil {
|
|
|
|
return *o.values
|
|
|
|
}
|
|
|
|
|
|
|
|
return map[string]types.Runtime{}
|
|
|
|
}
|
2016-06-21 20:42:47 +00:00
|
|
|
|
|
|
|
// Type returns the type of the option
|
|
|
|
func (o *RuntimeOpt) Type() string {
|
|
|
|
return "runtime"
|
|
|
|
}
|