Set a new mount value nolint:gocyclo
(value string)
| 22 | // |
| 23 | //nolint:gocyclo |
| 24 | func (m *MountOpt) Set(value string) error { |
| 25 | value = strings.TrimSpace(value) |
| 26 | if value == "" { |
| 27 | return errors.New("value is empty") |
| 28 | } |
| 29 | |
| 30 | csvReader := csv.NewReader(strings.NewReader(value)) |
| 31 | fields, err := csvReader.Read() |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | |
| 36 | mount := mounttypes.Mount{ |
| 37 | Type: mounttypes.TypeVolume, // default to volume mounts |
| 38 | } |
| 39 | |
| 40 | for _, field := range fields { |
| 41 | key, val, hasValue := strings.Cut(field, "=") |
| 42 | if k := strings.TrimSpace(key); k != key { |
| 43 | return fmt.Errorf("invalid option '%s' in '%s': option should not have whitespace", k, field) |
| 44 | } |
| 45 | if hasValue { |
| 46 | v := strings.TrimSpace(val) |
| 47 | if v == "" { |
| 48 | return fmt.Errorf("invalid value for '%s': value is empty", key) |
| 49 | } |
| 50 | if v != val { |
| 51 | return fmt.Errorf("invalid value for '%s' in '%s': value should not have whitespace", key, field) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // TODO(thaJeztah): these options should not be case-insensitive. |
| 56 | key = strings.ToLower(key) |
| 57 | |
| 58 | if !hasValue { |
| 59 | switch key { |
| 60 | case "readonly", "ro", "volume-nocopy", "bind-nonrecursive", "bind-create-src": |
| 61 | // boolean values |
| 62 | default: |
| 63 | return fmt.Errorf("invalid field '%s' must be a key=value pair", field) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | switch key { |
| 68 | case "type": |
| 69 | mount.Type = mounttypes.Type(strings.ToLower(val)) |
| 70 | case "source", "src": |
| 71 | mount.Source = val |
| 72 | if !filepath.IsAbs(val) && strings.HasPrefix(val, ".") { |
| 73 | if abs, err := filepath.Abs(val); err == nil { |
| 74 | mount.Source = abs |
| 75 | } |
| 76 | } |
| 77 | case "target", "dst", "destination": |
| 78 | mount.Target = val |
| 79 | case "readonly", "ro": |
| 80 | mount.ReadOnly, err = parseBoolValue(key, val, hasValue) |
| 81 | if err != nil { |