2016-12-23 14:09:12 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/blkiodev"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatorWeightFctType defines a validator function that returns a validated struct and/or an error.
|
|
|
|
type ValidatorWeightFctType func(val string) (*blkiodev.WeightDevice, error)
|
|
|
|
|
|
|
|
// ValidateWeightDevice validates that the specified string has a valid device-weight format.
|
|
|
|
func ValidateWeightDevice(val string) (*blkiodev.WeightDevice, error) {
|
2022-12-27 10:24:23 -05:00
|
|
|
k, v, ok := strings.Cut(val, ":")
|
|
|
|
if !ok || k == "" {
|
2016-12-23 14:09:12 -05:00
|
|
|
return nil, fmt.Errorf("bad format: %s", val)
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
// TODO(thaJeztah): should we really validate this on the client?
|
|
|
|
if !strings.HasPrefix(k, "/dev/") {
|
2016-12-23 14:09:12 -05:00
|
|
|
return nil, fmt.Errorf("bad format for device path: %s", val)
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
weight, err := strconv.ParseUint(v, 10, 16)
|
2016-12-23 14:09:12 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid weight for device: %s", val)
|
|
|
|
}
|
|
|
|
if weight > 0 && (weight < 10 || weight > 1000) {
|
|
|
|
return nil, fmt.Errorf("invalid weight for device: %s", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blkiodev.WeightDevice{
|
2022-12-27 10:24:23 -05:00
|
|
|
Path: k,
|
2016-12-23 14:09:12 -05:00
|
|
|
Weight: uint16(weight),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WeightdeviceOpt defines a map of WeightDevices
|
|
|
|
type WeightdeviceOpt struct {
|
|
|
|
values []*blkiodev.WeightDevice
|
|
|
|
validator ValidatorWeightFctType
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWeightdeviceOpt creates a new WeightdeviceOpt
|
|
|
|
func NewWeightdeviceOpt(validator ValidatorWeightFctType) WeightdeviceOpt {
|
|
|
|
return WeightdeviceOpt{
|
2022-12-27 10:24:23 -05:00
|
|
|
values: []*blkiodev.WeightDevice{},
|
2016-12-23 14:09:12 -05:00
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set validates a WeightDevice and sets its name as a key in WeightdeviceOpt
|
|
|
|
func (opt *WeightdeviceOpt) Set(val string) error {
|
|
|
|
var value *blkiodev.WeightDevice
|
|
|
|
if opt.validator != nil {
|
|
|
|
v, err := opt.validator(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
2022-12-27 09:59:57 -05:00
|
|
|
opt.values = append(opt.values, value)
|
2016-12-23 14:09:12 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns WeightdeviceOpt values as a string.
|
|
|
|
func (opt *WeightdeviceOpt) String() string {
|
2022-09-03 14:07:29 -04:00
|
|
|
out := make([]string, 0, len(opt.values))
|
2016-12-23 14:09:12 -05:00
|
|
|
for _, v := range opt.values {
|
|
|
|
out = append(out, v.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetList returns a slice of pointers to WeightDevices.
|
|
|
|
func (opt *WeightdeviceOpt) GetList() []*blkiodev.WeightDevice {
|
2017-06-09 17:42:16 -04:00
|
|
|
return opt.values
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the option type
|
|
|
|
func (opt *WeightdeviceOpt) Type() string {
|
Improve usage output for docker run
Commit a77f2450c70312f8c26877a18bfe2baa44d4abb9 switched `docker run`
to use the `pflags` package. Due to this change, the usage output for
the `--blkio-weight-device` and `--device-*` flags changed and now
showed `weighted-device`, and `throttled-device` as value type. As a
result, the output of `docker run --help` became a lot wider.
This patch changes the output to show `list` instead, which is
consistent with other options that allow to be set multiple times.
Output before this change;
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device weighted-device Block IO weight (relative device weight) (default [])
--device list Add a host device to the container (default [])
--device-read-bps throttled-device Limit read rate (bytes per second) from a device (default [])
--device-read-iops throttled-device Limit read rate (IO per second) from a device (default [])
--device-write-bps throttled-device Limit write rate (bytes per second) to a device (default [])
--device-write-iops throttled-device Limit write rate (IO per second) to a device (default [])
-w, --workdir string Working directory inside the container
Output after this change;
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--device list Add a host device to the container (default [])
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
-w, --workdir string Working directory inside the container
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2016-12-30 11:35:46 -05:00
|
|
|
return "list"
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|