MCPcopy Create free account
hub / github.com/docker/cli / Set

Method Set

opts/gpus.go:36–99  ·  view source on GitHub ↗

Set a new mount value nolint:gocyclo

(value string)

Source from the content-addressed store, hash-verified

34//
35//nolint:gocyclo
36func (o *GpuOpts) Set(value string) error {
37 csvReader := csv.NewReader(strings.NewReader(value))
38 fields, err := csvReader.Read()
39 if err != nil {
40 return err
41 }
42
43 req := container.DeviceRequest{}
44
45 seen := map[string]struct{}{}
46 // Set writable as the default
47 for _, field := range fields {
48 key, val, withValue := strings.Cut(field, "=")
49 if _, ok := seen[key]; ok {
50 return fmt.Errorf("gpu request key '%s' can be specified only once", key)
51 }
52 seen[key] = struct{}{}
53
54 if !withValue {
55 seen["count"] = struct{}{}
56 req.Count, err = parseCount(key)
57 if err != nil {
58 return err
59 }
60 continue
61 }
62
63 switch key {
64 case "driver":
65 req.Driver = val
66 case "count":
67 req.Count, err = parseCount(val)
68 if err != nil {
69 return err
70 }
71 case "device":
72 req.DeviceIDs = strings.Split(val, ",")
73 case "capabilities":
74 req.Capabilities = [][]string{append(strings.Split(val, ","), "gpu")}
75 case "options":
76 r := csv.NewReader(strings.NewReader(val))
77 optFields, err := r.Read()
78 if err != nil {
79 return fmt.Errorf("failed to read gpu options: %w", err)
80 }
81 req.Options = ConvertKVStringsToMap(optFields)
82 default:
83 return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
84 }
85 }
86
87 if _, ok := seen["count"]; !ok && req.DeviceIDs == nil {
88 req.Count = 1
89 }
90 if req.Options == nil {
91 req.Options = make(map[string]string)
92 }
93 if req.Capabilities == nil {

Callers 3

TestGpusOptAllFunction · 0.95
TestGpusOptInvalidCountFunction · 0.95
TestGpusOptsFunction · 0.95

Calls 3

parseCountFunction · 0.85
ConvertKVStringsToMapFunction · 0.85
ReadMethod · 0.45

Tested by 3

TestGpusOptAllFunction · 0.76
TestGpusOptInvalidCountFunction · 0.76
TestGpusOptsFunction · 0.76