MCPcopy Index your code
hub / github.com/docker/cli / Set

Method Set

opts/mount.go:24–150  ·  view source on GitHub ↗

Set a new mount value nolint:gocyclo

(value string)

Source from the content-addressed store, hash-verified

22//
23//nolint:gocyclo
24func (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 {

Callers 10

TestMountRelativeFunction · 0.95
TestMountOptDefaultTypeFunction · 0.95
TestMountOptReadOnlyFunction · 0.95
TestMountOptVolumeNoCopyFunction · 0.95

Calls 10

parseBoolValueFunction · 0.85
ensureBindOptionsFunction · 0.85
ensureVolumeOptionsFunction · 0.85
setValueOnMapFunction · 0.85
ensureVolumeDriverFunction · 0.85
ensureImageOptionsFunction · 0.85
ensureTmpfsOptionsFunction · 0.85
validateMountOptionsFunction · 0.85
ReadMethod · 0.45
TypeMethod · 0.45

Tested by 10

TestMountRelativeFunction · 0.76
TestMountOptDefaultTypeFunction · 0.76
TestMountOptReadOnlyFunction · 0.76
TestMountOptVolumeNoCopyFunction · 0.76