github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/opts/throttledevice.go (about) 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 52 return &blkiodev.ThrottleDevice{Path: split[0], Rate: rate}, nil 53 } 54 55 // ThrottledeviceOpt defines a map of ThrottleDevices 56 type ThrottledeviceOpt struct { 57 values []*blkiodev.ThrottleDevice 58 validator ValidatorThrottleFctType 59 } 60 61 // NewThrottledeviceOpt creates a new ThrottledeviceOpt 62 func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt { 63 values := []*blkiodev.ThrottleDevice{} 64 return ThrottledeviceOpt{ 65 values: values, 66 validator: validator, 67 } 68 } 69 70 // Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt 71 func (opt *ThrottledeviceOpt) Set(val string) error { 72 var value *blkiodev.ThrottleDevice 73 if opt.validator != nil { 74 v, err := opt.validator(val) 75 if err != nil { 76 return err 77 } 78 value = v 79 } 80 (opt.values) = append((opt.values), value) 81 return nil 82 } 83 84 // String returns ThrottledeviceOpt values as a string. 85 func (opt *ThrottledeviceOpt) String() string { 86 var out []string 87 for _, v := range opt.values { 88 out = append(out, v.String()) 89 } 90 91 return fmt.Sprintf("%v", out) 92 } 93 94 // GetList returns a slice of pointers to ThrottleDevices. 95 func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice { 96 var throttledevice []*blkiodev.ThrottleDevice 97 throttledevice = append(throttledevice, opt.values...) 98 99 return throttledevice 100 } 101 102 // Type returns the option type 103 func (opt *ThrottledeviceOpt) Type() string { 104 return "list" 105 }