2014-03-10 17:10:23 -04:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-01 13:12:29 -04:00
|
|
|
"math/big"
|
2014-06-13 08:02:12 -04:00
|
|
|
"net"
|
2016-12-23 14:09:12 -05:00
|
|
|
"path"
|
2014-03-10 17:10:23 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-12-25 04:11:12 -05:00
|
|
|
units "github.com/docker/go-units"
|
2018-07-27 09:37:43 -04:00
|
|
|
"github.com/pkg/errors"
|
2014-03-10 17:10:23 -04:00
|
|
|
)
|
|
|
|
|
2014-09-15 23:30:10 -04:00
|
|
|
var (
|
2015-07-12 04:33:30 -04:00
|
|
|
alphaRegexp = regexp.MustCompile(`[a-zA-Z]`)
|
|
|
|
domainRegexp = regexp.MustCompile(`^(:?(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9]))(:?\.(:?[a-zA-Z0-9]|(:?[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])))*)\.?\s*$`)
|
2014-09-15 23:30:10 -04:00
|
|
|
)
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ListOpts holds a list of values and a validation function.
|
2014-03-10 17:10:23 -04:00
|
|
|
type ListOpts struct {
|
2014-08-09 21:13:44 -04:00
|
|
|
values *[]string
|
2014-03-10 17:10:23 -04:00
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// NewListOpts creates a new ListOpts with the specified validator.
|
2014-03-10 17:10:23 -04:00
|
|
|
func NewListOpts(validator ValidatorFctType) ListOpts {
|
2014-08-09 21:13:44 -04:00
|
|
|
var values []string
|
2015-05-05 00:18:28 -04:00
|
|
|
return *NewListOptsRef(&values, validator)
|
2014-08-09 21:13:44 -04:00
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// NewListOptsRef creates a new ListOpts with the specified values and validator.
|
2015-05-05 00:18:28 -04:00
|
|
|
func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
|
2014-08-09 21:13:44 -04:00
|
|
|
return &ListOpts{
|
|
|
|
values: values,
|
2014-03-10 17:10:23 -04:00
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *ListOpts) String() string {
|
2017-03-30 21:35:04 -04:00
|
|
|
if len(*opts.values) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v", *opts.values)
|
2014-03-10 17:10:23 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 06:57:55 -04:00
|
|
|
// Set validates if needed the input value and adds it to the
|
2014-03-10 17:10:23 -04:00
|
|
|
// internal slice.
|
|
|
|
func (opts *ListOpts) Set(value string) error {
|
|
|
|
if opts.validator != nil {
|
|
|
|
v, err := opts.validator(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
2022-12-27 09:59:57 -05:00
|
|
|
*opts.values = append(*opts.values, value)
|
2014-03-10 17:10:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// Delete removes the specified element from the slice.
|
2014-03-10 17:10:23 -04:00
|
|
|
func (opts *ListOpts) Delete(key string) {
|
2014-08-09 21:13:44 -04:00
|
|
|
for i, k := range *opts.values {
|
2014-03-10 17:10:23 -04:00
|
|
|
if k == key {
|
2022-12-27 09:59:57 -05:00
|
|
|
*opts.values = append((*opts.values)[:i], (*opts.values)[i+1:]...)
|
2014-03-10 17:10:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMap returns the content of values in a map in order to avoid
|
|
|
|
// duplicates.
|
|
|
|
func (opts *ListOpts) GetMap() map[string]struct{} {
|
|
|
|
ret := make(map[string]struct{})
|
2014-08-09 21:13:44 -04:00
|
|
|
for _, k := range *opts.values {
|
2014-03-10 17:10:23 -04:00
|
|
|
ret[k] = struct{}{}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// GetAll returns the values of slice.
|
2014-03-10 17:10:23 -04:00
|
|
|
func (opts *ListOpts) GetAll() []string {
|
2022-12-27 09:59:57 -05:00
|
|
|
return *opts.values
|
2014-03-10 17:10:23 -04:00
|
|
|
}
|
|
|
|
|
2015-11-06 17:22:48 -05:00
|
|
|
// GetAllOrEmpty returns the values of the slice
|
|
|
|
// or an empty slice when there are no values.
|
|
|
|
func (opts *ListOpts) GetAllOrEmpty() []string {
|
|
|
|
v := *opts.values
|
|
|
|
if v == nil {
|
|
|
|
return make([]string, 0)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// Get checks the existence of the specified key.
|
2014-03-10 17:10:23 -04:00
|
|
|
func (opts *ListOpts) Get(key string) bool {
|
2014-08-09 21:13:44 -04:00
|
|
|
for _, k := range *opts.values {
|
2014-03-10 17:10:23 -04:00
|
|
|
if k == key {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Len returns the amount of element in the slice.
|
|
|
|
func (opts *ListOpts) Len() int {
|
2022-12-27 09:59:57 -05:00
|
|
|
return len(*opts.values)
|
2014-03-10 17:10:23 -04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:20:29 -04:00
|
|
|
// Type returns a string name for this Option type
|
|
|
|
func (opts *ListOpts) Type() string {
|
|
|
|
return "list"
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:09:07 -04:00
|
|
|
// WithValidator returns the ListOpts with validator set.
|
|
|
|
func (opts *ListOpts) WithValidator(validator ValidatorFctType) *ListOpts {
|
|
|
|
opts.validator = validator
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// NamedOption is an interface that list and map options
|
|
|
|
// with names implement.
|
|
|
|
type NamedOption interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamedListOpts is a ListOpts with a configuration name.
|
|
|
|
// This struct is useful to keep reference to the assigned
|
|
|
|
// field name in the internal configuration struct.
|
|
|
|
type NamedListOpts struct {
|
|
|
|
name string
|
|
|
|
ListOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NamedOption = &NamedListOpts{}
|
|
|
|
|
|
|
|
// NewNamedListOptsRef creates a reference to a new NamedListOpts struct.
|
|
|
|
func NewNamedListOptsRef(name string, values *[]string, validator ValidatorFctType) *NamedListOpts {
|
|
|
|
return &NamedListOpts{
|
|
|
|
name: name,
|
|
|
|
ListOpts: *NewListOptsRef(values, validator),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the NamedListOpts in the configuration.
|
|
|
|
func (o *NamedListOpts) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
2016-08-29 06:37:14 -04:00
|
|
|
// MapOpts holds a map of values and a validation function.
|
2015-05-04 17:39:48 -04:00
|
|
|
type MapOpts struct {
|
|
|
|
values map[string]string
|
|
|
|
validator ValidatorFctType
|
|
|
|
}
|
|
|
|
|
2015-07-12 04:33:30 -04:00
|
|
|
// Set validates if needed the input value and add it to the
|
|
|
|
// internal map, by splitting on '='.
|
2015-05-04 17:39:48 -04:00
|
|
|
func (opts *MapOpts) Set(value string) error {
|
|
|
|
if opts.validator != nil {
|
|
|
|
v, err := opts.validator(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
value = v
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
k, v, _ := strings.Cut(value, "=")
|
|
|
|
opts.values[k] = v
|
2015-05-04 17:39:48 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// GetAll returns the values of MapOpts as a map.
|
2015-06-12 09:25:32 -04:00
|
|
|
func (opts *MapOpts) GetAll() map[string]string {
|
|
|
|
return opts.values
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:39:48 -04:00
|
|
|
func (opts *MapOpts) String() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
return fmt.Sprintf("%v", opts.values)
|
2015-05-04 17:39:48 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:59:48 -04:00
|
|
|
// Type returns a string name for this Option type
|
|
|
|
func (opts *MapOpts) Type() string {
|
|
|
|
return "map"
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// NewMapOpts creates a new MapOpts with the specified map of values and a validator.
|
2015-05-05 00:18:28 -04:00
|
|
|
func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
|
|
|
|
if values == nil {
|
|
|
|
values = make(map[string]string)
|
|
|
|
}
|
2015-05-04 17:39:48 -04:00
|
|
|
return &MapOpts{
|
|
|
|
values: values,
|
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:35:10 -05:00
|
|
|
// NamedMapOpts is a MapOpts struct with a configuration name.
|
|
|
|
// This struct is useful to keep reference to the assigned
|
|
|
|
// field name in the internal configuration struct.
|
|
|
|
type NamedMapOpts struct {
|
|
|
|
name string
|
|
|
|
MapOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NamedOption = &NamedMapOpts{}
|
|
|
|
|
|
|
|
// NewNamedMapOpts creates a reference to a new NamedMapOpts struct.
|
|
|
|
func NewNamedMapOpts(name string, values map[string]string, validator ValidatorFctType) *NamedMapOpts {
|
|
|
|
return &NamedMapOpts{
|
|
|
|
name: name,
|
|
|
|
MapOpts: *NewMapOpts(values, validator),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the NamedMapOpts in the configuration.
|
|
|
|
func (o *NamedMapOpts) Name() string {
|
|
|
|
return o.name
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ValidatorFctType defines a validator function that returns a validated string and/or an error.
|
2014-03-10 17:10:23 -04:00
|
|
|
type ValidatorFctType func(val string) (string, error)
|
2015-07-12 04:33:30 -04:00
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
|
2014-10-06 21:54:52 -04:00
|
|
|
type ValidatorFctListType func(val string) ([]string, error)
|
2014-03-10 17:10:23 -04:00
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ValidateIPAddress validates an Ip address.
|
2014-07-09 17:47:55 -04:00
|
|
|
func ValidateIPAddress(val string) (string, error) {
|
2022-09-29 11:21:51 -04:00
|
|
|
ip := net.ParseIP(strings.TrimSpace(val))
|
2014-06-13 08:02:12 -04:00
|
|
|
if ip != nil {
|
|
|
|
return ip.String(), nil
|
2014-03-10 17:10:23 -04:00
|
|
|
}
|
2014-06-13 08:02:12 -04:00
|
|
|
return "", fmt.Errorf("%s is not an ip address", val)
|
2014-03-10 17:10:23 -04:00
|
|
|
}
|
2014-02-07 11:48:14 -05:00
|
|
|
|
2016-12-23 14:09:12 -05:00
|
|
|
// ValidateMACAddress validates a MAC address.
|
|
|
|
func ValidateMACAddress(val string) (string, error) {
|
|
|
|
_, err := net.ParseMAC(strings.TrimSpace(val))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ValidateDNSSearch validates domain for resolvconf search configuration.
|
|
|
|
// A zero length domain is represented by a dot (.).
|
2015-07-12 04:33:30 -04:00
|
|
|
func ValidateDNSSearch(val string) (string, error) {
|
2014-06-26 07:03:23 -04:00
|
|
|
if val = strings.Trim(val, " "); val == "." {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
return validateDomain(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateDomain(val string) (string, error) {
|
2014-09-15 23:30:10 -04:00
|
|
|
if alphaRegexp.FindString(val) == "" {
|
2014-02-07 11:48:14 -05:00
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-09-15 23:30:10 -04:00
|
|
|
ns := domainRegexp.FindSubmatch([]byte(val))
|
2014-11-25 06:45:20 -05:00
|
|
|
if len(ns) > 0 && len(ns[1]) < 255 {
|
2014-02-07 11:48:14 -05:00
|
|
|
return string(ns[1]), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("%s is not a valid domain", val)
|
|
|
|
}
|
2014-07-18 14:48:19 -04:00
|
|
|
|
2015-08-27 03:33:21 -04:00
|
|
|
// ValidateLabel validates that the specified string is a valid label, and returns it.
|
Fix labels copying value from environment variables
This patch fixes a bug where labels use the same behavior as `--env`, resulting
in a value to be copied from environment variables with the same name as the
label if no value is set (i.e. a simple key, no `=` sign, no value).
An earlier pull request addressed similar cases for `docker run`;
2b17f4c8a8caad552025edb05a73db683fb8a5c6, but this did not address the
same situation for (e.g.) `docker service create`.
Digging in history for this bug, I found that use of the `ValidateEnv`
function for labels was added in the original implementation of the labels feature in
https://github.com/docker/docker/commit/abb5e9a0777469e64fe2c7ecfa66ea01083d2071#diff-ae476143d40e21ac0918630f7365ed3cR34
However, the design never intended it to expand environment variables,
and use of this function was either due to either a "copy/paste" of the
equivalent `--env` flags, or a misunderstanding (the name `ValidateEnv` does
not communicate that it also expands environment variables), and the existing
`ValidateLabel` was designed for _engine_ labels (which required a value to
be set).
Following the initial implementation, other parts of the code followed
the same (incorrect) approach, therefore leading the bug to be introduced
in services as well.
This patch:
- updates the `ValidateLabel` to match the expected validation
rules (this function is no longer used since 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a),
and the daemon has its own implementation)
- corrects various locations in the code where `ValidateEnv` was used instead of `ValidateLabel`.
Before this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker service inspect --format '{{json .Spec.Labels}}' test
{"SOME_ENV_VAR":"I_AM_SOME_ENV_VAR"}
```
After this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker container inspect --format '{{json .Config.Labels}}' test
{"SOME_ENV_VAR":""}
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-13 10:47:30 -05:00
|
|
|
//
|
|
|
|
// Labels are in the form of key=value; key must be a non-empty string, and not
|
|
|
|
// contain whitespaces. A value is optional (defaults to an empty string if omitted).
|
|
|
|
//
|
|
|
|
// Leading whitespace is removed during validation but values are kept as-is
|
|
|
|
// otherwise, so any string value is accepted for both, which includes whitespace
|
|
|
|
// (for values) and quotes (surrounding, or embedded in key or value).
|
|
|
|
//
|
|
|
|
// TODO discuss if quotes (and other special characters) should be valid or invalid for keys
|
|
|
|
// TODO discuss if leading/trailing whitespace in keys should be preserved (and valid)
|
2022-12-27 10:24:23 -05:00
|
|
|
func ValidateLabel(value string) (string, error) {
|
|
|
|
key, _, _ := strings.Cut(value, "=")
|
|
|
|
key = strings.TrimLeft(key, whiteSpaces)
|
Fix labels copying value from environment variables
This patch fixes a bug where labels use the same behavior as `--env`, resulting
in a value to be copied from environment variables with the same name as the
label if no value is set (i.e. a simple key, no `=` sign, no value).
An earlier pull request addressed similar cases for `docker run`;
2b17f4c8a8caad552025edb05a73db683fb8a5c6, but this did not address the
same situation for (e.g.) `docker service create`.
Digging in history for this bug, I found that use of the `ValidateEnv`
function for labels was added in the original implementation of the labels feature in
https://github.com/docker/docker/commit/abb5e9a0777469e64fe2c7ecfa66ea01083d2071#diff-ae476143d40e21ac0918630f7365ed3cR34
However, the design never intended it to expand environment variables,
and use of this function was either due to either a "copy/paste" of the
equivalent `--env` flags, or a misunderstanding (the name `ValidateEnv` does
not communicate that it also expands environment variables), and the existing
`ValidateLabel` was designed for _engine_ labels (which required a value to
be set).
Following the initial implementation, other parts of the code followed
the same (incorrect) approach, therefore leading the bug to be introduced
in services as well.
This patch:
- updates the `ValidateLabel` to match the expected validation
rules (this function is no longer used since 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a),
and the daemon has its own implementation)
- corrects various locations in the code where `ValidateEnv` was used instead of `ValidateLabel`.
Before this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker service inspect --format '{{json .Spec.Labels}}' test
{"SOME_ENV_VAR":"I_AM_SOME_ENV_VAR"}
```
After this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker container inspect --format '{{json .Config.Labels}}' test
{"SOME_ENV_VAR":""}
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-13 10:47:30 -05:00
|
|
|
if key == "" {
|
2022-12-27 10:24:23 -05:00
|
|
|
return "", fmt.Errorf("invalid label '%s': empty name", value)
|
Fix labels copying value from environment variables
This patch fixes a bug where labels use the same behavior as `--env`, resulting
in a value to be copied from environment variables with the same name as the
label if no value is set (i.e. a simple key, no `=` sign, no value).
An earlier pull request addressed similar cases for `docker run`;
2b17f4c8a8caad552025edb05a73db683fb8a5c6, but this did not address the
same situation for (e.g.) `docker service create`.
Digging in history for this bug, I found that use of the `ValidateEnv`
function for labels was added in the original implementation of the labels feature in
https://github.com/docker/docker/commit/abb5e9a0777469e64fe2c7ecfa66ea01083d2071#diff-ae476143d40e21ac0918630f7365ed3cR34
However, the design never intended it to expand environment variables,
and use of this function was either due to either a "copy/paste" of the
equivalent `--env` flags, or a misunderstanding (the name `ValidateEnv` does
not communicate that it also expands environment variables), and the existing
`ValidateLabel` was designed for _engine_ labels (which required a value to
be set).
Following the initial implementation, other parts of the code followed
the same (incorrect) approach, therefore leading the bug to be introduced
in services as well.
This patch:
- updates the `ValidateLabel` to match the expected validation
rules (this function is no longer used since 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a),
and the daemon has its own implementation)
- corrects various locations in the code where `ValidateEnv` was used instead of `ValidateLabel`.
Before this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker service inspect --format '{{json .Spec.Labels}}' test
{"SOME_ENV_VAR":"I_AM_SOME_ENV_VAR"}
```
After this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker container inspect --format '{{json .Config.Labels}}' test
{"SOME_ENV_VAR":""}
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-13 10:47:30 -05:00
|
|
|
}
|
|
|
|
if strings.ContainsAny(key, whiteSpaces) {
|
2019-03-18 22:17:02 -04:00
|
|
|
return "", fmt.Errorf("label '%s' contains whitespaces", key)
|
2014-11-20 13:36:05 -05:00
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
return value, nil
|
2014-11-20 13:36:05 -05:00
|
|
|
}
|
2016-03-29 08:24:28 -04:00
|
|
|
|
2016-05-07 21:36:10 -04:00
|
|
|
// ValidateSysctl validates a sysctl and returns it.
|
2016-03-29 08:24:28 -04:00
|
|
|
func ValidateSysctl(val string) (string, error) {
|
|
|
|
validSysctlMap := map[string]bool{
|
|
|
|
"kernel.msgmax": true,
|
|
|
|
"kernel.msgmnb": true,
|
|
|
|
"kernel.msgmni": true,
|
|
|
|
"kernel.sem": true,
|
|
|
|
"kernel.shmall": true,
|
|
|
|
"kernel.shmmax": true,
|
|
|
|
"kernel.shmmni": true,
|
|
|
|
"kernel.shm_rmid_forced": true,
|
|
|
|
}
|
|
|
|
validSysctlPrefixes := []string{
|
|
|
|
"net.",
|
|
|
|
"fs.mqueue.",
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
k, _, ok := strings.Cut(val, "=")
|
|
|
|
if !ok || k == "" {
|
|
|
|
return "", fmt.Errorf("sysctl '%s' is not allowed", val)
|
2016-03-29 08:24:28 -04:00
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
if validSysctlMap[k] {
|
2016-03-29 08:24:28 -04:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
for _, vp := range validSysctlPrefixes {
|
2022-12-27 10:24:23 -05:00
|
|
|
if strings.HasPrefix(k, vp) {
|
2016-03-29 08:24:28 -04:00
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
return "", fmt.Errorf("sysctl '%s' is not allowed", val)
|
2016-03-29 08:24:28 -04:00
|
|
|
}
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
// FilterOpt is a flag type for validating filters
|
|
|
|
type FilterOpt struct {
|
|
|
|
filter filters.Args
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFilterOpt returns a new FilterOpt
|
|
|
|
func NewFilterOpt() FilterOpt {
|
|
|
|
return FilterOpt{filter: filters.NewArgs()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *FilterOpt) String() string {
|
2018-07-27 09:37:43 -04:00
|
|
|
repr, err := filters.ToJSON(o.filter)
|
2016-06-13 22:52:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return "invalid filters"
|
|
|
|
}
|
|
|
|
return repr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value of the opt by parsing the command line value
|
|
|
|
func (o *FilterOpt) Set(value string) error {
|
2018-07-27 09:37:43 -04:00
|
|
|
if value == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !strings.Contains(value, "=") {
|
|
|
|
return errors.New("bad format of filter (expected name=value)")
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
name, val, _ := strings.Cut(value, "=")
|
2018-07-27 09:37:43 -04:00
|
|
|
|
2022-12-27 10:24:23 -05:00
|
|
|
// TODO(thaJeztah): these options should not be case-insensitive.
|
|
|
|
name = strings.ToLower(strings.TrimSpace(name))
|
|
|
|
val = strings.TrimSpace(val)
|
|
|
|
o.filter.Add(name, val)
|
2018-07-27 09:37:43 -04:00
|
|
|
return nil
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the option type
|
|
|
|
func (o *FilterOpt) Type() string {
|
|
|
|
return "filter"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value of this option
|
|
|
|
func (o *FilterOpt) Value() filters.Args {
|
|
|
|
return o.filter
|
|
|
|
}
|
2016-11-01 13:12:29 -04:00
|
|
|
|
|
|
|
// NanoCPUs is a type for fixed point fractional number.
|
|
|
|
type NanoCPUs int64
|
|
|
|
|
|
|
|
// String returns the string format of the number
|
|
|
|
func (c *NanoCPUs) String() string {
|
2017-03-30 21:35:04 -04:00
|
|
|
if *c == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2016-11-01 13:12:29 -04:00
|
|
|
return big.NewRat(c.Value(), 1e9).FloatString(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value of the NanoCPU by passing a string
|
|
|
|
func (c *NanoCPUs) Set(value string) error {
|
2016-10-28 17:30:20 -04:00
|
|
|
cpus, err := ParseCPUs(value)
|
|
|
|
*c = NanoCPUs(cpus)
|
|
|
|
return err
|
2016-11-01 13:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type
|
|
|
|
func (c *NanoCPUs) Type() string {
|
2016-11-08 10:06:07 -05:00
|
|
|
return "decimal"
|
2016-11-01 13:12:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value in int64
|
|
|
|
func (c *NanoCPUs) Value() int64 {
|
|
|
|
return int64(*c)
|
|
|
|
}
|
2016-10-28 17:30:20 -04:00
|
|
|
|
|
|
|
// ParseCPUs takes a string ratio and returns an integer value of nano cpus
|
|
|
|
func ParseCPUs(value string) (int64, error) {
|
|
|
|
cpu, ok := new(big.Rat).SetString(value)
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("failed to parse %v as a rational number", value)
|
|
|
|
}
|
|
|
|
nano := cpu.Mul(cpu, big.NewRat(1e9, 1))
|
|
|
|
if !nano.IsInt() {
|
linting: fmt.Errorf can be replaced with errors.New (perfsprint)
internal/test/cli.go:175:14: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("no notary client available unless defined")
^
cli/command/cli.go:318:29: fmt.Errorf can be replaced with errors.New (perfsprint)
return docker.Endpoint{}, fmt.Errorf("no context store initialized")
^
cli/command/container/attach.go:161:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(result.Error.Message)
^
cli/command/container/opts.go:577:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-period cannot be negative")
^
cli/command/container/opts.go:580:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-interval cannot be negative")
^
cli/command/container/stats.go:221:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("filtering is not supported when specifying a list of containers")
^
cli/command/container/attach_test.go:82:17: fmt.Errorf can be replaced with errors.New (perfsprint)
expectedErr = fmt.Errorf("unexpected error")
^
cli/command/container/create_test.go:234:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/container/list_test.go:150:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("error listing containers")
^
cli/command/container/rm_test.go:40:31: fmt.Errorf can be replaced with errors.New (perfsprint)
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
^
cli/command/container/run_test.go:138:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/image/pull_test.go:115:49: fmt.Errorf can be replaced with errors.New (perfsprint)
return io.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
^
cli/command/network/connect.go:88:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("invalid key/value pair format in driver options")
^
cli/command/plugin/create_test.go:96:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error creating plugin")
^
cli/command/plugin/disable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error disabling plugin")
^
cli/command/plugin/enable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("failed to enable plugin")
^
cli/command/plugin/inspect_test.go:55:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, nil, fmt.Errorf("error inspecting plugin")
^
cli/command/plugin/install_test.go:43:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("Error installing plugin")
^
cli/command/plugin/install_test.go:51:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("(image) when fetching")
^
cli/command/plugin/install_test.go:95:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("should not try to install plugin")
^
cli/command/plugin/list_test.go:35:41: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.PluginsListResponse{}, fmt.Errorf("error listing plugins")
^
cli/command/plugin/remove_test.go:27:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error removing plugin")
^
cli/command/registry/login_test.go:36:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")
^
cli/command/registry/login_test.go:44:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf(errUnknownUser)
^
cli/command/system/info.go:190:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("errors pretty printing info")
^
cli/command/system/prune.go:77:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(`ERROR: The "until" filter is not supported with "--volumes"`)
^
cli/command/system/version_test.go:19:28: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.Version{}, fmt.Errorf("no server")
^
cli/command/trust/key_load.go:112:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return []byte{}, fmt.Errorf("could not decrypt key")
^
cli/command/trust/revoke.go:44:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("cannot use a digest reference for IMAGE:TAG")
^
cli/command/trust/revoke.go:105:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("no signed tags to remove")
^
cli/command/trust/signer_add.go:56:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("releases is a reserved keyword, please use a different signer name")
^
cli/command/trust/signer_add.go:60:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("path to a public key must be provided using the `--key` flag")
^
opts/config.go:71:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/mount.go:168:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("type is required")
^
opts/mount.go:172:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("target is required")
^
opts/network.go:90:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("network name/id is not specified")
^
opts/network.go:129:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("invalid key value pair format in driver options")
^
opts/opts.go:404:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return 0, fmt.Errorf("value is too precise")
^
opts/opts.go:412:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("empty string specified for links")
^
opts/parse.go:84:37: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon")
^
opts/parse.go:89:38: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
^
opts/port.go:105:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("hostip is not supported")
^
opts/secret.go:70:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/env_test.go:57:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: =a"),
^
opts/env_test.go:93:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: ="),
^
cli-plugins/manager/error_test.go:16:11: fmt.Errorf can be replaced with errors.New (perfsprint)
inner := fmt.Errorf("testing")
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit dfec976e844b31d77defb49ed8b270487036e3ed)
Signed-off-by: Cory Snider <csnider@mirantis.com>
2024-06-10 14:47:07 -04:00
|
|
|
return 0, errors.New("value is too precise")
|
2016-10-28 17:30:20 -04:00
|
|
|
}
|
|
|
|
return nano.Num().Int64(), nil
|
|
|
|
}
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
// ParseLink parses and validates the specified string as a link format (name:alias)
|
|
|
|
func ParseLink(val string) (string, string, error) {
|
|
|
|
if val == "" {
|
linting: fmt.Errorf can be replaced with errors.New (perfsprint)
internal/test/cli.go:175:14: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("no notary client available unless defined")
^
cli/command/cli.go:318:29: fmt.Errorf can be replaced with errors.New (perfsprint)
return docker.Endpoint{}, fmt.Errorf("no context store initialized")
^
cli/command/container/attach.go:161:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(result.Error.Message)
^
cli/command/container/opts.go:577:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-period cannot be negative")
^
cli/command/container/opts.go:580:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-interval cannot be negative")
^
cli/command/container/stats.go:221:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("filtering is not supported when specifying a list of containers")
^
cli/command/container/attach_test.go:82:17: fmt.Errorf can be replaced with errors.New (perfsprint)
expectedErr = fmt.Errorf("unexpected error")
^
cli/command/container/create_test.go:234:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/container/list_test.go:150:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("error listing containers")
^
cli/command/container/rm_test.go:40:31: fmt.Errorf can be replaced with errors.New (perfsprint)
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
^
cli/command/container/run_test.go:138:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/image/pull_test.go:115:49: fmt.Errorf can be replaced with errors.New (perfsprint)
return io.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
^
cli/command/network/connect.go:88:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("invalid key/value pair format in driver options")
^
cli/command/plugin/create_test.go:96:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error creating plugin")
^
cli/command/plugin/disable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error disabling plugin")
^
cli/command/plugin/enable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("failed to enable plugin")
^
cli/command/plugin/inspect_test.go:55:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, nil, fmt.Errorf("error inspecting plugin")
^
cli/command/plugin/install_test.go:43:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("Error installing plugin")
^
cli/command/plugin/install_test.go:51:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("(image) when fetching")
^
cli/command/plugin/install_test.go:95:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("should not try to install plugin")
^
cli/command/plugin/list_test.go:35:41: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.PluginsListResponse{}, fmt.Errorf("error listing plugins")
^
cli/command/plugin/remove_test.go:27:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error removing plugin")
^
cli/command/registry/login_test.go:36:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")
^
cli/command/registry/login_test.go:44:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf(errUnknownUser)
^
cli/command/system/info.go:190:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("errors pretty printing info")
^
cli/command/system/prune.go:77:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(`ERROR: The "until" filter is not supported with "--volumes"`)
^
cli/command/system/version_test.go:19:28: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.Version{}, fmt.Errorf("no server")
^
cli/command/trust/key_load.go:112:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return []byte{}, fmt.Errorf("could not decrypt key")
^
cli/command/trust/revoke.go:44:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("cannot use a digest reference for IMAGE:TAG")
^
cli/command/trust/revoke.go:105:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("no signed tags to remove")
^
cli/command/trust/signer_add.go:56:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("releases is a reserved keyword, please use a different signer name")
^
cli/command/trust/signer_add.go:60:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("path to a public key must be provided using the `--key` flag")
^
opts/config.go:71:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/mount.go:168:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("type is required")
^
opts/mount.go:172:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("target is required")
^
opts/network.go:90:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("network name/id is not specified")
^
opts/network.go:129:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("invalid key value pair format in driver options")
^
opts/opts.go:404:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return 0, fmt.Errorf("value is too precise")
^
opts/opts.go:412:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("empty string specified for links")
^
opts/parse.go:84:37: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon")
^
opts/parse.go:89:38: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
^
opts/port.go:105:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("hostip is not supported")
^
opts/secret.go:70:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/env_test.go:57:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: =a"),
^
opts/env_test.go:93:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: ="),
^
cli-plugins/manager/error_test.go:16:11: fmt.Errorf can be replaced with errors.New (perfsprint)
inner := fmt.Errorf("testing")
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit dfec976e844b31d77defb49ed8b270487036e3ed)
Signed-off-by: Cory Snider <csnider@mirantis.com>
2024-06-10 14:47:07 -04:00
|
|
|
return "", "", errors.New("empty string specified for links")
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
// We expect two parts, but restrict to three to allow detecting invalid formats.
|
|
|
|
arr := strings.SplitN(val, ":", 3)
|
|
|
|
|
|
|
|
// TODO(thaJeztah): clean up this logic!!
|
2016-12-23 14:09:12 -05:00
|
|
|
if len(arr) > 2 {
|
linting: fmt.Errorf can be replaced with errors.New (perfsprint)
internal/test/cli.go:175:14: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("no notary client available unless defined")
^
cli/command/cli.go:318:29: fmt.Errorf can be replaced with errors.New (perfsprint)
return docker.Endpoint{}, fmt.Errorf("no context store initialized")
^
cli/command/container/attach.go:161:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(result.Error.Message)
^
cli/command/container/opts.go:577:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-period cannot be negative")
^
cli/command/container/opts.go:580:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("--health-start-interval cannot be negative")
^
cli/command/container/stats.go:221:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("filtering is not supported when specifying a list of containers")
^
cli/command/container/attach_test.go:82:17: fmt.Errorf can be replaced with errors.New (perfsprint)
expectedErr = fmt.Errorf("unexpected error")
^
cli/command/container/create_test.go:234:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/container/list_test.go:150:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("error listing containers")
^
cli/command/container/rm_test.go:40:31: fmt.Errorf can be replaced with errors.New (perfsprint)
return errdefs.NotFound(fmt.Errorf("Error: no such container: " + container))
^
cli/command/container/run_test.go:138:40: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.CreateResponse{}, fmt.Errorf("shouldn't try to pull image")
^
cli/command/image/pull_test.go:115:49: fmt.Errorf can be replaced with errors.New (perfsprint)
return io.NopCloser(strings.NewReader("")), fmt.Errorf("shouldn't try to pull image")
^
cli/command/network/connect.go:88:16: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("invalid key/value pair format in driver options")
^
cli/command/plugin/create_test.go:96:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error creating plugin")
^
cli/command/plugin/disable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error disabling plugin")
^
cli/command/plugin/enable_test.go:32:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("failed to enable plugin")
^
cli/command/plugin/inspect_test.go:55:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, nil, fmt.Errorf("error inspecting plugin")
^
cli/command/plugin/install_test.go:43:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("Error installing plugin")
^
cli/command/plugin/install_test.go:51:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("(image) when fetching")
^
cli/command/plugin/install_test.go:95:17: fmt.Errorf can be replaced with errors.New (perfsprint)
return nil, fmt.Errorf("should not try to install plugin")
^
cli/command/plugin/list_test.go:35:41: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.PluginsListResponse{}, fmt.Errorf("error listing plugins")
^
cli/command/plugin/remove_test.go:27:12: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("Error removing plugin")
^
cli/command/registry/login_test.go:36:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf("Invalid Username or Password")
^
cli/command/registry/login_test.go:44:46: fmt.Errorf can be replaced with errors.New (perfsprint)
return registrytypes.AuthenticateOKBody{}, fmt.Errorf(errUnknownUser)
^
cli/command/system/info.go:190:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("errors pretty printing info")
^
cli/command/system/prune.go:77:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf(`ERROR: The "until" filter is not supported with "--volumes"`)
^
cli/command/system/version_test.go:19:28: fmt.Errorf can be replaced with errors.New (perfsprint)
return types.Version{}, fmt.Errorf("no server")
^
cli/command/trust/key_load.go:112:22: fmt.Errorf can be replaced with errors.New (perfsprint)
return []byte{}, fmt.Errorf("could not decrypt key")
^
cli/command/trust/revoke.go:44:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("cannot use a digest reference for IMAGE:TAG")
^
cli/command/trust/revoke.go:105:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("no signed tags to remove")
^
cli/command/trust/signer_add.go:56:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("releases is a reserved keyword, please use a different signer name")
^
cli/command/trust/signer_add.go:60:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("path to a public key must be provided using the `--key` flag")
^
opts/config.go:71:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/mount.go:168:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("type is required")
^
opts/mount.go:172:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("target is required")
^
opts/network.go:90:11: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("network name/id is not specified")
^
opts/network.go:129:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("invalid key value pair format in driver options")
^
opts/opts.go:404:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return 0, fmt.Errorf("value is too precise")
^
opts/opts.go:412:18: fmt.Errorf can be replaced with errors.New (perfsprint)
return "", "", fmt.Errorf("empty string specified for links")
^
opts/parse.go:84:37: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: no policy provided before colon")
^
opts/parse.go:89:38: fmt.Errorf can be replaced with errors.New (perfsprint)
return container.RestartPolicy{}, fmt.Errorf("invalid restart policy format: maximum retry count must be an integer")
^
opts/port.go:105:13: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("hostip is not supported")
^
opts/secret.go:70:10: fmt.Errorf can be replaced with errors.New (perfsprint)
return fmt.Errorf("source is required")
^
opts/env_test.go:57:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: =a"),
^
opts/env_test.go:93:11: fmt.Errorf can be replaced with errors.New (perfsprint)
err: fmt.Errorf("invalid environment variable: ="),
^
cli-plugins/manager/error_test.go:16:11: fmt.Errorf can be replaced with errors.New (perfsprint)
inner := fmt.Errorf("testing")
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit dfec976e844b31d77defb49ed8b270487036e3ed)
Signed-off-by: Cory Snider <csnider@mirantis.com>
2024-06-10 14:47:07 -04:00
|
|
|
return "", "", errors.New("bad format for links: " + val)
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
// TODO(thaJeztah): this should trim the "/" prefix as well??
|
2016-12-23 14:09:12 -05:00
|
|
|
if len(arr) == 1 {
|
|
|
|
return val, val, nil
|
|
|
|
}
|
|
|
|
// This is kept because we can actually get a HostConfig with links
|
|
|
|
// from an already created container and the format is not `foo:bar`
|
|
|
|
// but `/foo:/c1/bar`
|
|
|
|
if strings.HasPrefix(arr[0], "/") {
|
2022-12-27 10:24:23 -05:00
|
|
|
// TODO(thaJeztah): clean up this logic!!
|
2016-12-23 14:09:12 -05:00
|
|
|
_, alias := path.Split(arr[1])
|
|
|
|
return arr[0][1:], alias, nil
|
|
|
|
}
|
|
|
|
return arr[0], arr[1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateLink validates that the specified string has a valid link format (containerName:alias).
|
|
|
|
func ValidateLink(val string) (string, error) {
|
|
|
|
_, _, err := ParseLink(val)
|
|
|
|
return val, err
|
|
|
|
}
|
2016-12-25 04:11:12 -05:00
|
|
|
|
|
|
|
// MemBytes is a type for human readable memory bytes (like 128M, 2g, etc)
|
|
|
|
type MemBytes int64
|
|
|
|
|
|
|
|
// String returns the string format of the human readable memory bytes
|
|
|
|
func (m *MemBytes) String() string {
|
2016-12-28 17:44:07 -05:00
|
|
|
// NOTE: In spf13/pflag/flag.go, "0" is considered as "zero value" while "0 B" is not.
|
|
|
|
// We return "0" in case value is 0 here so that the default value is hidden.
|
|
|
|
// (Sometimes "default 0 B" is actually misleading)
|
|
|
|
if m.Value() != 0 {
|
|
|
|
return units.BytesSize(float64(m.Value()))
|
|
|
|
}
|
|
|
|
return "0"
|
2016-12-25 04:11:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value of the MemBytes by passing a string
|
|
|
|
func (m *MemBytes) Set(value string) error {
|
|
|
|
val, err := units.RAMInBytes(value)
|
|
|
|
*m = MemBytes(val)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type
|
|
|
|
func (m *MemBytes) Type() string {
|
|
|
|
return "bytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value in int64
|
|
|
|
func (m *MemBytes) Value() int64 {
|
|
|
|
return int64(*m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON is the customized unmarshaler for MemBytes
|
|
|
|
func (m *MemBytes) UnmarshalJSON(s []byte) error {
|
|
|
|
if len(s) <= 2 || s[0] != '"' || s[len(s)-1] != '"' {
|
|
|
|
return fmt.Errorf("invalid size: %q", s)
|
|
|
|
}
|
|
|
|
val, err := units.RAMInBytes(string(s[1 : len(s)-1]))
|
|
|
|
*m = MemBytes(val)
|
|
|
|
return err
|
|
|
|
}
|
2017-03-06 16:01:04 -05:00
|
|
|
|
|
|
|
// MemSwapBytes is a type for human readable memory bytes (like 128M, 2g, etc).
|
|
|
|
// It differs from MemBytes in that -1 is valid and the default.
|
|
|
|
type MemSwapBytes int64
|
|
|
|
|
|
|
|
// Set sets the value of the MemSwapBytes by passing a string
|
|
|
|
func (m *MemSwapBytes) Set(value string) error {
|
|
|
|
if value == "-1" {
|
|
|
|
*m = MemSwapBytes(-1)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
val, err := units.RAMInBytes(value)
|
|
|
|
*m = MemSwapBytes(val)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the type
|
|
|
|
func (m *MemSwapBytes) Type() string {
|
|
|
|
return "bytes"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns the value in int64
|
|
|
|
func (m *MemSwapBytes) Value() int64 {
|
|
|
|
return int64(*m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MemSwapBytes) String() string {
|
|
|
|
b := MemBytes(*m)
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON is the customized unmarshaler for MemSwapBytes
|
|
|
|
func (m *MemSwapBytes) UnmarshalJSON(s []byte) error {
|
|
|
|
b := MemBytes(*m)
|
|
|
|
return b.UnmarshalJSON(s)
|
|
|
|
}
|