These are already present in `docker/cli` 👼
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,98 +0,0 @@ |
1 |
-package opts |
|
2 |
- |
|
3 |
-import ( |
|
4 |
- "encoding/csv" |
|
5 |
- "fmt" |
|
6 |
- "os" |
|
7 |
- "strconv" |
|
8 |
- "strings" |
|
9 |
- |
|
10 |
- swarmtypes "github.com/docker/docker/api/types/swarm" |
|
11 |
-) |
|
12 |
- |
|
13 |
-// ConfigOpt is a Value type for parsing configs |
|
14 |
-type ConfigOpt struct { |
|
15 |
- values []*swarmtypes.ConfigReference |
|
16 |
-} |
|
17 |
- |
|
18 |
-// Set a new config value |
|
19 |
-func (o *ConfigOpt) Set(value string) error { |
|
20 |
- csvReader := csv.NewReader(strings.NewReader(value)) |
|
21 |
- fields, err := csvReader.Read() |
|
22 |
- if err != nil { |
|
23 |
- return err |
|
24 |
- } |
|
25 |
- |
|
26 |
- options := &swarmtypes.ConfigReference{ |
|
27 |
- File: &swarmtypes.ConfigReferenceFileTarget{ |
|
28 |
- UID: "0", |
|
29 |
- GID: "0", |
|
30 |
- Mode: 0444, |
|
31 |
- }, |
|
32 |
- } |
|
33 |
- |
|
34 |
- // support a simple syntax of --config foo |
|
35 |
- if len(fields) == 1 { |
|
36 |
- options.File.Name = fields[0] |
|
37 |
- options.ConfigName = fields[0] |
|
38 |
- o.values = append(o.values, options) |
|
39 |
- return nil |
|
40 |
- } |
|
41 |
- |
|
42 |
- for _, field := range fields { |
|
43 |
- parts := strings.SplitN(field, "=", 2) |
|
44 |
- key := strings.ToLower(parts[0]) |
|
45 |
- |
|
46 |
- if len(parts) != 2 { |
|
47 |
- return fmt.Errorf("invalid field '%s' must be a key=value pair", field) |
|
48 |
- } |
|
49 |
- |
|
50 |
- value := parts[1] |
|
51 |
- switch key { |
|
52 |
- case "source", "src": |
|
53 |
- options.ConfigName = value |
|
54 |
- case "target": |
|
55 |
- options.File.Name = value |
|
56 |
- case "uid": |
|
57 |
- options.File.UID = value |
|
58 |
- case "gid": |
|
59 |
- options.File.GID = value |
|
60 |
- case "mode": |
|
61 |
- m, err := strconv.ParseUint(value, 0, 32) |
|
62 |
- if err != nil { |
|
63 |
- return fmt.Errorf("invalid mode specified: %v", err) |
|
64 |
- } |
|
65 |
- |
|
66 |
- options.File.Mode = os.FileMode(m) |
|
67 |
- default: |
|
68 |
- return fmt.Errorf("invalid field in config request: %s", key) |
|
69 |
- } |
|
70 |
- } |
|
71 |
- |
|
72 |
- if options.ConfigName == "" { |
|
73 |
- return fmt.Errorf("source is required") |
|
74 |
- } |
|
75 |
- |
|
76 |
- o.values = append(o.values, options) |
|
77 |
- return nil |
|
78 |
-} |
|
79 |
- |
|
80 |
-// Type returns the type of this option |
|
81 |
-func (o *ConfigOpt) Type() string { |
|
82 |
- return "config" |
|
83 |
-} |
|
84 |
- |
|
85 |
-// String returns a string repr of this option |
|
86 |
-func (o *ConfigOpt) String() string { |
|
87 |
- configs := []string{} |
|
88 |
- for _, config := range o.values { |
|
89 |
- repr := fmt.Sprintf("%s -> %s", config.ConfigName, config.File.Name) |
|
90 |
- configs = append(configs, repr) |
|
91 |
- } |
|
92 |
- return strings.Join(configs, ", ") |
|
93 |
-} |
|
94 |
- |
|
95 |
-// Value returns the config requests |
|
96 |
-func (o *ConfigOpt) Value() []*swarmtypes.ConfigReference { |
|
97 |
- return o.values |
|
98 |
-} |
... | ... |
@@ -2,13 +2,11 @@ package opts |
2 | 2 |
|
3 | 3 |
import ( |
4 | 4 |
"fmt" |
5 |
- "math/big" |
|
6 | 5 |
"net" |
7 | 6 |
"path" |
8 | 7 |
"regexp" |
9 | 8 |
"strings" |
10 | 9 |
|
11 |
- "github.com/docker/docker/api/types/filters" |
|
12 | 10 |
units "github.com/docker/go-units" |
13 | 11 |
) |
14 | 12 |
|
... | ... |
@@ -236,15 +234,6 @@ func ValidateIPAddress(val string) (string, error) { |
236 | 236 |
return "", fmt.Errorf("%s is not an ip address", val) |
237 | 237 |
} |
238 | 238 |
|
239 |
-// ValidateMACAddress validates a MAC address. |
|
240 |
-func ValidateMACAddress(val string) (string, error) { |
|
241 |
- _, err := net.ParseMAC(strings.TrimSpace(val)) |
|
242 |
- if err != nil { |
|
243 |
- return "", err |
|
244 |
- } |
|
245 |
- return val, nil |
|
246 |
-} |
|
247 |
- |
|
248 | 239 |
// ValidateDNSSearch validates domain for resolvconf search configuration. |
249 | 240 |
// A zero length domain is represented by a dot (.). |
250 | 241 |
func ValidateDNSSearch(val string) (string, error) { |
... | ... |
@@ -274,114 +263,6 @@ func ValidateLabel(val string) (string, error) { |
274 | 274 |
return val, nil |
275 | 275 |
} |
276 | 276 |
|
277 |
-// ValidateSysctl validates a sysctl and returns it. |
|
278 |
-func ValidateSysctl(val string) (string, error) { |
|
279 |
- validSysctlMap := map[string]bool{ |
|
280 |
- "kernel.msgmax": true, |
|
281 |
- "kernel.msgmnb": true, |
|
282 |
- "kernel.msgmni": true, |
|
283 |
- "kernel.sem": true, |
|
284 |
- "kernel.shmall": true, |
|
285 |
- "kernel.shmmax": true, |
|
286 |
- "kernel.shmmni": true, |
|
287 |
- "kernel.shm_rmid_forced": true, |
|
288 |
- } |
|
289 |
- validSysctlPrefixes := []string{ |
|
290 |
- "net.", |
|
291 |
- "fs.mqueue.", |
|
292 |
- } |
|
293 |
- arr := strings.Split(val, "=") |
|
294 |
- if len(arr) < 2 { |
|
295 |
- return "", fmt.Errorf("sysctl '%s' is not whitelisted", val) |
|
296 |
- } |
|
297 |
- if validSysctlMap[arr[0]] { |
|
298 |
- return val, nil |
|
299 |
- } |
|
300 |
- |
|
301 |
- for _, vp := range validSysctlPrefixes { |
|
302 |
- if strings.HasPrefix(arr[0], vp) { |
|
303 |
- return val, nil |
|
304 |
- } |
|
305 |
- } |
|
306 |
- return "", fmt.Errorf("sysctl '%s' is not whitelisted", val) |
|
307 |
-} |
|
308 |
- |
|
309 |
-// FilterOpt is a flag type for validating filters |
|
310 |
-type FilterOpt struct { |
|
311 |
- filter filters.Args |
|
312 |
-} |
|
313 |
- |
|
314 |
-// NewFilterOpt returns a new FilterOpt |
|
315 |
-func NewFilterOpt() FilterOpt { |
|
316 |
- return FilterOpt{filter: filters.NewArgs()} |
|
317 |
-} |
|
318 |
- |
|
319 |
-func (o *FilterOpt) String() string { |
|
320 |
- repr, err := filters.ToParam(o.filter) |
|
321 |
- if err != nil { |
|
322 |
- return "invalid filters" |
|
323 |
- } |
|
324 |
- return repr |
|
325 |
-} |
|
326 |
- |
|
327 |
-// Set sets the value of the opt by parsing the command line value |
|
328 |
-func (o *FilterOpt) Set(value string) error { |
|
329 |
- var err error |
|
330 |
- o.filter, err = filters.ParseFlag(value, o.filter) |
|
331 |
- return err |
|
332 |
-} |
|
333 |
- |
|
334 |
-// Type returns the option type |
|
335 |
-func (o *FilterOpt) Type() string { |
|
336 |
- return "filter" |
|
337 |
-} |
|
338 |
- |
|
339 |
-// Value returns the value of this option |
|
340 |
-func (o *FilterOpt) Value() filters.Args { |
|
341 |
- return o.filter |
|
342 |
-} |
|
343 |
- |
|
344 |
-// NanoCPUs is a type for fixed point fractional number. |
|
345 |
-type NanoCPUs int64 |
|
346 |
- |
|
347 |
-// String returns the string format of the number |
|
348 |
-func (c *NanoCPUs) String() string { |
|
349 |
- if *c == 0 { |
|
350 |
- return "" |
|
351 |
- } |
|
352 |
- return big.NewRat(c.Value(), 1e9).FloatString(3) |
|
353 |
-} |
|
354 |
- |
|
355 |
-// Set sets the value of the NanoCPU by passing a string |
|
356 |
-func (c *NanoCPUs) Set(value string) error { |
|
357 |
- cpus, err := ParseCPUs(value) |
|
358 |
- *c = NanoCPUs(cpus) |
|
359 |
- return err |
|
360 |
-} |
|
361 |
- |
|
362 |
-// Type returns the type |
|
363 |
-func (c *NanoCPUs) Type() string { |
|
364 |
- return "decimal" |
|
365 |
-} |
|
366 |
- |
|
367 |
-// Value returns the value in int64 |
|
368 |
-func (c *NanoCPUs) Value() int64 { |
|
369 |
- return int64(*c) |
|
370 |
-} |
|
371 |
- |
|
372 |
-// ParseCPUs takes a string ratio and returns an integer value of nano cpus |
|
373 |
-func ParseCPUs(value string) (int64, error) { |
|
374 |
- cpu, ok := new(big.Rat).SetString(value) |
|
375 |
- if !ok { |
|
376 |
- return 0, fmt.Errorf("failed to parse %v as a rational number", value) |
|
377 |
- } |
|
378 |
- nano := cpu.Mul(cpu, big.NewRat(1e9, 1)) |
|
379 |
- if !nano.IsInt() { |
|
380 |
- return 0, fmt.Errorf("value is too precise") |
|
381 |
- } |
|
382 |
- return nano.Num().Int64(), nil |
|
383 |
-} |
|
384 |
- |
|
385 | 277 |
// ParseLink parses and validates the specified string as a link format (name:alias) |
386 | 278 |
func ParseLink(val string) (string, string, error) { |
387 | 279 |
if val == "" { |
... | ... |
@@ -404,12 +285,6 @@ func ParseLink(val string) (string, string, error) { |
404 | 404 |
return arr[0], arr[1], nil |
405 | 405 |
} |
406 | 406 |
|
407 |
-// ValidateLink validates that the specified string has a valid link format (containerName:alias). |
|
408 |
-func ValidateLink(val string) (string, error) { |
|
409 |
- _, _, err := ParseLink(val) |
|
410 |
- return val, err |
|
411 |
-} |
|
412 |
- |
|
413 | 407 |
// MemBytes is a type for human readable memory bytes (like 128M, 2g, etc) |
414 | 408 |
type MemBytes int64 |
415 | 409 |
|
... | ... |
@@ -450,39 +325,3 @@ func (m *MemBytes) UnmarshalJSON(s []byte) error { |
450 | 450 |
*m = MemBytes(val) |
451 | 451 |
return err |
452 | 452 |
} |
453 |
- |
|
454 |
-// MemSwapBytes is a type for human readable memory bytes (like 128M, 2g, etc). |
|
455 |
-// It differs from MemBytes in that -1 is valid and the default. |
|
456 |
-type MemSwapBytes int64 |
|
457 |
- |
|
458 |
-// Set sets the value of the MemSwapBytes by passing a string |
|
459 |
-func (m *MemSwapBytes) Set(value string) error { |
|
460 |
- if value == "-1" { |
|
461 |
- *m = MemSwapBytes(-1) |
|
462 |
- return nil |
|
463 |
- } |
|
464 |
- val, err := units.RAMInBytes(value) |
|
465 |
- *m = MemSwapBytes(val) |
|
466 |
- return err |
|
467 |
-} |
|
468 |
- |
|
469 |
-// Type returns the type |
|
470 |
-func (m *MemSwapBytes) Type() string { |
|
471 |
- return "bytes" |
|
472 |
-} |
|
473 |
- |
|
474 |
-// Value returns the value in int64 |
|
475 |
-func (m *MemSwapBytes) Value() int64 { |
|
476 |
- return int64(*m) |
|
477 |
-} |
|
478 |
- |
|
479 |
-func (m *MemSwapBytes) String() string { |
|
480 |
- b := MemBytes(*m) |
|
481 |
- return b.String() |
|
482 |
-} |
|
483 |
- |
|
484 |
-// UnmarshalJSON is the customized unmarshaler for MemSwapBytes |
|
485 |
-func (m *MemSwapBytes) UnmarshalJSON(s []byte) error { |
|
486 |
- b := MemBytes(*m) |
|
487 |
- return b.UnmarshalJSON(s) |
|
488 |
-} |
... | ... |
@@ -231,49 +231,6 @@ func TestNamedMapOpts(t *testing.T) { |
231 | 231 |
} |
232 | 232 |
} |
233 | 233 |
|
234 |
-func TestValidateMACAddress(t *testing.T) { |
|
235 |
- if _, err := ValidateMACAddress(`92:d0:c6:0a:29:33`); err != nil { |
|
236 |
- t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:29:33`) got %s", err) |
|
237 |
- } |
|
238 |
- |
|
239 |
- if _, err := ValidateMACAddress(`92:d0:c6:0a:33`); err == nil { |
|
240 |
- t.Fatalf("ValidateMACAddress(`92:d0:c6:0a:33`) succeeded; expected failure on invalid MAC") |
|
241 |
- } |
|
242 |
- |
|
243 |
- if _, err := ValidateMACAddress(`random invalid string`); err == nil { |
|
244 |
- t.Fatalf("ValidateMACAddress(`random invalid string`) succeeded; expected failure on invalid MAC") |
|
245 |
- } |
|
246 |
-} |
|
247 |
- |
|
248 |
-func TestValidateLink(t *testing.T) { |
|
249 |
- valid := []string{ |
|
250 |
- "name", |
|
251 |
- "dcdfbe62ecd0:alias", |
|
252 |
- "7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da", |
|
253 |
- "angry_torvalds:linus", |
|
254 |
- } |
|
255 |
- invalid := map[string]string{ |
|
256 |
- "": "empty string specified for links", |
|
257 |
- "too:much:of:it": "bad format for links: too:much:of:it", |
|
258 |
- } |
|
259 |
- |
|
260 |
- for _, link := range valid { |
|
261 |
- if _, err := ValidateLink(link); err != nil { |
|
262 |
- t.Fatalf("ValidateLink(`%q`) should succeed: error %q", link, err) |
|
263 |
- } |
|
264 |
- } |
|
265 |
- |
|
266 |
- for link, expectedError := range invalid { |
|
267 |
- if _, err := ValidateLink(link); err == nil { |
|
268 |
- t.Fatalf("ValidateLink(`%q`) should have failed validation", link) |
|
269 |
- } else { |
|
270 |
- if !strings.Contains(err.Error(), expectedError) { |
|
271 |
- t.Fatalf("ValidateLink(`%q`) error should contain %q", link, expectedError) |
|
272 |
- } |
|
273 |
- } |
|
274 |
- } |
|
275 |
-} |
|
276 |
- |
|
277 | 234 |
func TestParseLink(t *testing.T) { |
278 | 235 |
name, alias, err := ParseLink("name:alias") |
279 | 236 |
if err != nil { |
280 | 237 |
deleted file mode 100644 |
... | ... |
@@ -1,111 +0,0 @@ |
1 |
-package opts |
|
2 |
- |
|
3 |
-import ( |
|
4 |
- "fmt" |
|
5 |
- "strconv" |
|
6 |
- "strings" |
|
7 |
- |
|
8 |
- "github.com/docker/docker/api/types/blkiodev" |
|
9 |
- "github.com/docker/go-units" |
|
10 |
-) |
|
11 |
- |
|
12 |
-// ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error. |
|
13 |
-type ValidatorThrottleFctType func(val string) (*blkiodev.ThrottleDevice, error) |
|
14 |
- |
|
15 |
-// ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format. |
|
16 |
-func ValidateThrottleBpsDevice(val string) (*blkiodev.ThrottleDevice, error) { |
|
17 |
- split := strings.SplitN(val, ":", 2) |
|
18 |
- if len(split) != 2 { |
|
19 |
- return nil, fmt.Errorf("bad format: %s", val) |
|
20 |
- } |
|
21 |
- if !strings.HasPrefix(split[0], "/dev/") { |
|
22 |
- return nil, fmt.Errorf("bad format for device path: %s", val) |
|
23 |
- } |
|
24 |
- rate, err := units.RAMInBytes(split[1]) |
|
25 |
- if err != nil { |
|
26 |
- return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val) |
|
27 |
- } |
|
28 |
- if rate < 0 { |
|
29 |
- return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val) |
|
30 |
- } |
|
31 |
- |
|
32 |
- return &blkiodev.ThrottleDevice{ |
|
33 |
- Path: split[0], |
|
34 |
- Rate: uint64(rate), |
|
35 |
- }, nil |
|
36 |
-} |
|
37 |
- |
|
38 |
-// ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format. |
|
39 |
-func ValidateThrottleIOpsDevice(val string) (*blkiodev.ThrottleDevice, error) { |
|
40 |
- split := strings.SplitN(val, ":", 2) |
|
41 |
- if len(split) != 2 { |
|
42 |
- return nil, fmt.Errorf("bad format: %s", val) |
|
43 |
- } |
|
44 |
- if !strings.HasPrefix(split[0], "/dev/") { |
|
45 |
- return nil, fmt.Errorf("bad format for device path: %s", val) |
|
46 |
- } |
|
47 |
- rate, err := strconv.ParseUint(split[1], 10, 64) |
|
48 |
- if err != nil { |
|
49 |
- return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val) |
|
50 |
- } |
|
51 |
- if rate < 0 { |
|
52 |
- return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val) |
|
53 |
- } |
|
54 |
- |
|
55 |
- return &blkiodev.ThrottleDevice{ |
|
56 |
- Path: split[0], |
|
57 |
- Rate: uint64(rate), |
|
58 |
- }, nil |
|
59 |
-} |
|
60 |
- |
|
61 |
-// ThrottledeviceOpt defines a map of ThrottleDevices |
|
62 |
-type ThrottledeviceOpt struct { |
|
63 |
- values []*blkiodev.ThrottleDevice |
|
64 |
- validator ValidatorThrottleFctType |
|
65 |
-} |
|
66 |
- |
|
67 |
-// NewThrottledeviceOpt creates a new ThrottledeviceOpt |
|
68 |
-func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt { |
|
69 |
- values := []*blkiodev.ThrottleDevice{} |
|
70 |
- return ThrottledeviceOpt{ |
|
71 |
- values: values, |
|
72 |
- validator: validator, |
|
73 |
- } |
|
74 |
-} |
|
75 |
- |
|
76 |
-// Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt |
|
77 |
-func (opt *ThrottledeviceOpt) Set(val string) error { |
|
78 |
- var value *blkiodev.ThrottleDevice |
|
79 |
- if opt.validator != nil { |
|
80 |
- v, err := opt.validator(val) |
|
81 |
- if err != nil { |
|
82 |
- return err |
|
83 |
- } |
|
84 |
- value = v |
|
85 |
- } |
|
86 |
- (opt.values) = append((opt.values), value) |
|
87 |
- return nil |
|
88 |
-} |
|
89 |
- |
|
90 |
-// String returns ThrottledeviceOpt values as a string. |
|
91 |
-func (opt *ThrottledeviceOpt) String() string { |
|
92 |
- var out []string |
|
93 |
- for _, v := range opt.values { |
|
94 |
- out = append(out, v.String()) |
|
95 |
- } |
|
96 |
- |
|
97 |
- return fmt.Sprintf("%v", out) |
|
98 |
-} |
|
99 |
- |
|
100 |
-// GetList returns a slice of pointers to ThrottleDevices. |
|
101 |
-func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice { |
|
102 |
- var throttledevice []*blkiodev.ThrottleDevice |
|
103 |
- throttledevice = append(throttledevice, opt.values...) |
|
104 |
- |
|
105 |
- return throttledevice |
|
106 |
-} |
|
107 |
- |
|
108 |
-// Type returns the option type |
|
109 |
-func (opt *ThrottledeviceOpt) Type() string { |
|
110 |
- return "list" |
|
111 |
-} |