runtime.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package opts // import "github.com/docker/docker/opts"
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/api/types/system"
  6. )
  7. // RuntimeOpt defines a map of Runtimes
  8. type RuntimeOpt struct {
  9. name string
  10. stockRuntimeName string
  11. values *map[string]system.Runtime
  12. }
  13. // NewNamedRuntimeOpt creates a new RuntimeOpt
  14. func NewNamedRuntimeOpt(name string, ref *map[string]system.Runtime, stockRuntime string) *RuntimeOpt {
  15. if ref == nil {
  16. ref = &map[string]system.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. k, v, ok := strings.Cut(val, "=")
  27. if !ok {
  28. return fmt.Errorf("invalid runtime argument: %s", val)
  29. }
  30. // TODO(thaJeztah): this should not accept spaces.
  31. k = strings.TrimSpace(k)
  32. v = strings.TrimSpace(v)
  33. if k == "" || v == "" {
  34. return fmt.Errorf("invalid runtime argument: %s", val)
  35. }
  36. // TODO(thaJeztah): this should not be case-insensitive.
  37. k = strings.ToLower(k)
  38. if k == o.stockRuntimeName {
  39. return fmt.Errorf("runtime name '%s' is reserved", o.stockRuntimeName)
  40. }
  41. if _, ok := (*o.values)[k]; ok {
  42. return fmt.Errorf("runtime '%s' was already defined", k)
  43. }
  44. (*o.values)[k] = system.Runtime{Path: v}
  45. return nil
  46. }
  47. // String returns Runtime values as a string.
  48. func (o *RuntimeOpt) String() string {
  49. var out []string
  50. for k := range *o.values {
  51. out = append(out, k)
  52. }
  53. return fmt.Sprintf("%v", out)
  54. }
  55. // GetMap returns a map of Runtimes (name: path)
  56. func (o *RuntimeOpt) GetMap() map[string]system.Runtime {
  57. if o.values != nil {
  58. return *o.values
  59. }
  60. return map[string]system.Runtime{}
  61. }
  62. // Type returns the type of the option
  63. func (o *RuntimeOpt) Type() string {
  64. return "runtime"
  65. }