runtime.go 1.8 KB

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