mirror of https://github.com/docker/cli.git
Move ParseLink and validators into runconfig.parse where they are used.
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
bfba6ec070
commit
659fac261b
78
opts/opts.go
78
opts/opts.go
|
@ -4,13 +4,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/blkiodev"
|
"github.com/docker/docker/pkg/blkiodev"
|
||||||
"github.com/docker/docker/pkg/parsers"
|
|
||||||
"github.com/docker/go-units"
|
"github.com/docker/go-units"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -221,82 +219,6 @@ func ValidateThrottleBpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateLink validates that the specified string has a valid link format (containerName:alias).
|
|
||||||
func ValidateLink(val string) (string, error) {
|
|
||||||
if _, _, err := parsers.ParseLink(val); err != nil {
|
|
||||||
return val, err
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidDeviceMode checks if the mode for device is valid or not.
|
|
||||||
// Valid mode is a composition of r (read), w (write), and m (mknod).
|
|
||||||
func ValidDeviceMode(mode string) bool {
|
|
||||||
var legalDeviceMode = map[rune]bool{
|
|
||||||
'r': true,
|
|
||||||
'w': true,
|
|
||||||
'm': true,
|
|
||||||
}
|
|
||||||
if mode == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, c := range mode {
|
|
||||||
if !legalDeviceMode[c] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
legalDeviceMode[c] = false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateDevice validates a path for devices
|
|
||||||
// It will make sure 'val' is in the form:
|
|
||||||
// [host-dir:]container-path[:mode]
|
|
||||||
// It also validates the device mode.
|
|
||||||
func ValidateDevice(val string) (string, error) {
|
|
||||||
return validatePath(val, ValidDeviceMode)
|
|
||||||
}
|
|
||||||
|
|
||||||
func validatePath(val string, validator func(string) bool) (string, error) {
|
|
||||||
var containerPath string
|
|
||||||
var mode string
|
|
||||||
|
|
||||||
if strings.Count(val, ":") > 2 {
|
|
||||||
return val, fmt.Errorf("bad format for path: %s", val)
|
|
||||||
}
|
|
||||||
|
|
||||||
split := strings.SplitN(val, ":", 3)
|
|
||||||
if split[0] == "" {
|
|
||||||
return val, fmt.Errorf("bad format for path: %s", val)
|
|
||||||
}
|
|
||||||
switch len(split) {
|
|
||||||
case 1:
|
|
||||||
containerPath = split[0]
|
|
||||||
val = path.Clean(containerPath)
|
|
||||||
case 2:
|
|
||||||
if isValid := validator(split[1]); isValid {
|
|
||||||
containerPath = split[0]
|
|
||||||
mode = split[1]
|
|
||||||
val = fmt.Sprintf("%s:%s", path.Clean(containerPath), mode)
|
|
||||||
} else {
|
|
||||||
containerPath = split[1]
|
|
||||||
val = fmt.Sprintf("%s:%s", split[0], path.Clean(containerPath))
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
containerPath = split[1]
|
|
||||||
mode = split[2]
|
|
||||||
if isValid := validator(split[2]); !isValid {
|
|
||||||
return val, fmt.Errorf("bad mode specified: %s", mode)
|
|
||||||
}
|
|
||||||
val = fmt.Sprintf("%s:%s:%s", split[0], containerPath, mode)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !path.IsAbs(containerPath) {
|
|
||||||
return val, fmt.Errorf("%s is not an absolute path", containerPath)
|
|
||||||
}
|
|
||||||
return val, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateEnv validates an environment variable and returns it.
|
// ValidateEnv validates an environment variable and returns it.
|
||||||
// If no value is specified, it returns the current value using os.Getenv.
|
// If no value is specified, it returns the current value using os.Getenv.
|
||||||
//
|
//
|
||||||
|
|
|
@ -244,86 +244,6 @@ func TestValidateAttach(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateLink(t *testing.T) {
|
|
||||||
valid := []string{
|
|
||||||
"name",
|
|
||||||
"dcdfbe62ecd0:alias",
|
|
||||||
"7a67485460b7642516a4ad82ecefe7f57d0c4916f530561b71a50a3f9c4e33da",
|
|
||||||
"angry_torvalds:linus",
|
|
||||||
}
|
|
||||||
invalid := map[string]string{
|
|
||||||
"": "empty string specified for links",
|
|
||||||
"too:much:of:it": "bad format for links: too:much:of:it",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, link := range valid {
|
|
||||||
if _, err := ValidateLink(link); err != nil {
|
|
||||||
t.Fatalf("ValidateLink(`%q`) should succeed: error %q", link, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for link, expectedError := range invalid {
|
|
||||||
if _, err := ValidateLink(link); err == nil {
|
|
||||||
t.Fatalf("ValidateLink(`%q`) should have failed validation", link)
|
|
||||||
} else {
|
|
||||||
if !strings.Contains(err.Error(), expectedError) {
|
|
||||||
t.Fatalf("ValidateLink(`%q`) error should contain %q", link, expectedError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateDevice(t *testing.T) {
|
|
||||||
valid := []string{
|
|
||||||
"/home",
|
|
||||||
"/home:/home",
|
|
||||||
"/home:/something/else",
|
|
||||||
"/with space",
|
|
||||||
"/home:/with space",
|
|
||||||
"relative:/absolute-path",
|
|
||||||
"hostPath:/containerPath:r",
|
|
||||||
"/hostPath:/containerPath:rw",
|
|
||||||
"/hostPath:/containerPath:mrw",
|
|
||||||
}
|
|
||||||
invalid := map[string]string{
|
|
||||||
"": "bad format for path: ",
|
|
||||||
"./": "./ is not an absolute path",
|
|
||||||
"../": "../ is not an absolute path",
|
|
||||||
"/:../": "../ is not an absolute path",
|
|
||||||
"/:path": "path is not an absolute path",
|
|
||||||
":": "bad format for path: :",
|
|
||||||
"/tmp:": " is not an absolute path",
|
|
||||||
":test": "bad format for path: :test",
|
|
||||||
":/test": "bad format for path: :/test",
|
|
||||||
"tmp:": " is not an absolute path",
|
|
||||||
":test:": "bad format for path: :test:",
|
|
||||||
"::": "bad format for path: ::",
|
|
||||||
":::": "bad format for path: :::",
|
|
||||||
"/tmp:::": "bad format for path: /tmp:::",
|
|
||||||
":/tmp::": "bad format for path: :/tmp::",
|
|
||||||
"path:ro": "ro is not an absolute path",
|
|
||||||
"path:rr": "rr is not an absolute path",
|
|
||||||
"a:/b:ro": "bad mode specified: ro",
|
|
||||||
"a:/b:rr": "bad mode specified: rr",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, path := range valid {
|
|
||||||
if _, err := ValidateDevice(path); err != nil {
|
|
||||||
t.Fatalf("ValidateDevice(`%q`) should succeed: error %q", path, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for path, expectedError := range invalid {
|
|
||||||
if _, err := ValidateDevice(path); err == nil {
|
|
||||||
t.Fatalf("ValidateDevice(`%q`) should have failed validation", path)
|
|
||||||
} else {
|
|
||||||
if err.Error() != expectedError {
|
|
||||||
t.Fatalf("ValidateDevice(`%q`) error should contain %q, got %q", path, expectedError, err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestValidateEnv(t *testing.T) {
|
func TestValidateEnv(t *testing.T) {
|
||||||
valids := map[string]string{
|
valids := map[string]string{
|
||||||
"a": "a",
|
"a": "a",
|
||||||
|
|
Loading…
Reference in New Issue