Browse code

move opts out of pkg because it's related to docker

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)

Victor Vieux authored on 2014/03/11 06:10:23
Showing 6 changed files
... ...
@@ -10,8 +10,8 @@ import (
10 10
 	"github.com/dotcloud/docker/builtins"
11 11
 	"github.com/dotcloud/docker/dockerversion"
12 12
 	"github.com/dotcloud/docker/engine"
13
+	"github.com/dotcloud/docker/opts"
13 14
 	flag "github.com/dotcloud/docker/pkg/mflag"
14
-	"github.com/dotcloud/docker/pkg/opts"
15 15
 	"github.com/dotcloud/docker/sysinit"
16 16
 	"github.com/dotcloud/docker/utils"
17 17
 )
18 18
new file mode 100644
... ...
@@ -0,0 +1,138 @@
0
+package opts
1
+
2
+import (
3
+	"fmt"
4
+	"github.com/dotcloud/docker/utils"
5
+	"os"
6
+	"path/filepath"
7
+	"regexp"
8
+	"strings"
9
+)
10
+
11
+// ListOpts type
12
+type ListOpts struct {
13
+	values    []string
14
+	validator ValidatorFctType
15
+}
16
+
17
+func NewListOpts(validator ValidatorFctType) ListOpts {
18
+	return ListOpts{
19
+		validator: validator,
20
+	}
21
+}
22
+
23
+func (opts *ListOpts) String() string {
24
+	return fmt.Sprintf("%v", []string(opts.values))
25
+}
26
+
27
+// Set validates if needed the input value and add it to the
28
+// internal slice.
29
+func (opts *ListOpts) Set(value string) error {
30
+	if opts.validator != nil {
31
+		v, err := opts.validator(value)
32
+		if err != nil {
33
+			return err
34
+		}
35
+		value = v
36
+	}
37
+	opts.values = append(opts.values, value)
38
+	return nil
39
+}
40
+
41
+// Delete remove the given element from the slice.
42
+func (opts *ListOpts) Delete(key string) {
43
+	for i, k := range opts.values {
44
+		if k == key {
45
+			opts.values = append(opts.values[:i], opts.values[i+1:]...)
46
+			return
47
+		}
48
+	}
49
+}
50
+
51
+// GetMap returns the content of values in a map in order to avoid
52
+// duplicates.
53
+// FIXME: can we remove this?
54
+func (opts *ListOpts) GetMap() map[string]struct{} {
55
+	ret := make(map[string]struct{})
56
+	for _, k := range opts.values {
57
+		ret[k] = struct{}{}
58
+	}
59
+	return ret
60
+}
61
+
62
+// GetAll returns the values' slice.
63
+// FIXME: Can we remove this?
64
+func (opts *ListOpts) GetAll() []string {
65
+	return opts.values
66
+}
67
+
68
+// Get checks the existence of the given key.
69
+func (opts *ListOpts) Get(key string) bool {
70
+	for _, k := range opts.values {
71
+		if k == key {
72
+			return true
73
+		}
74
+	}
75
+	return false
76
+}
77
+
78
+// Len returns the amount of element in the slice.
79
+func (opts *ListOpts) Len() int {
80
+	return len(opts.values)
81
+}
82
+
83
+// Validators
84
+type ValidatorFctType func(val string) (string, error)
85
+
86
+func ValidateAttach(val string) (string, error) {
87
+	if val != "stdin" && val != "stdout" && val != "stderr" {
88
+		return val, fmt.Errorf("Unsupported stream name: %s", val)
89
+	}
90
+	return val, nil
91
+}
92
+
93
+func ValidateLink(val string) (string, error) {
94
+	if _, err := utils.PartParser("name:alias", val); err != nil {
95
+		return val, err
96
+	}
97
+	return val, nil
98
+}
99
+
100
+func ValidatePath(val string) (string, error) {
101
+	var containerPath string
102
+
103
+	if strings.Count(val, ":") > 2 {
104
+		return val, fmt.Errorf("bad format for volumes: %s", val)
105
+	}
106
+
107
+	splited := strings.SplitN(val, ":", 2)
108
+	if len(splited) == 1 {
109
+		containerPath = splited[0]
110
+		val = filepath.Clean(splited[0])
111
+	} else {
112
+		containerPath = splited[1]
113
+		val = fmt.Sprintf("%s:%s", splited[0], filepath.Clean(splited[1]))
114
+	}
115
+
116
+	if !filepath.IsAbs(containerPath) {
117
+		return val, fmt.Errorf("%s is not an absolute path", containerPath)
118
+	}
119
+	return val, nil
120
+}
121
+
122
+func ValidateEnv(val string) (string, error) {
123
+	arr := strings.Split(val, "=")
124
+	if len(arr) > 1 {
125
+		return val, nil
126
+	}
127
+	return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
128
+}
129
+
130
+func ValidateIp4Address(val string) (string, error) {
131
+	re := regexp.MustCompile(`^(([0-9]+\.){3}([0-9]+))\s*$`)
132
+	var ns = re.FindSubmatch([]byte(val))
133
+	if len(ns) > 0 {
134
+		return string(ns[1]), nil
135
+	}
136
+	return "", fmt.Errorf("%s is not an ip4 address", val)
137
+}
0 138
new file mode 100644
... ...
@@ -0,0 +1,24 @@
0
+package opts
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+func TestValidateIP4(t *testing.T) {
7
+	if ret, err := ValidateIp4Address(`1.2.3.4`); err != nil || ret == "" {
8
+		t.Fatalf("ValidateIp4Address(`1.2.3.4`) got %s %s", ret, err)
9
+	}
10
+
11
+	if ret, err := ValidateIp4Address(`127.0.0.1`); err != nil || ret == "" {
12
+		t.Fatalf("ValidateIp4Address(`127.0.0.1`) got %s %s", ret, err)
13
+	}
14
+
15
+	if ret, err := ValidateIp4Address(`127`); err == nil || ret != "" {
16
+		t.Fatalf("ValidateIp4Address(`127`) got %s %s", ret, err)
17
+	}
18
+
19
+	if ret, err := ValidateIp4Address(`random invalid string`); err == nil || ret != "" {
20
+		t.Fatalf("ValidateIp4Address(`random invalid string`) got %s %s", ret, err)
21
+	}
22
+
23
+}
0 24
deleted file mode 100644
... ...
@@ -1,138 +0,0 @@
1
-package opts
2
-
3
-import (
4
-	"fmt"
5
-	"github.com/dotcloud/docker/utils"
6
-	"os"
7
-	"path/filepath"
8
-	"regexp"
9
-	"strings"
10
-)
11
-
12
-// ListOpts type
13
-type ListOpts struct {
14
-	values    []string
15
-	validator ValidatorFctType
16
-}
17
-
18
-func NewListOpts(validator ValidatorFctType) ListOpts {
19
-	return ListOpts{
20
-		validator: validator,
21
-	}
22
-}
23
-
24
-func (opts *ListOpts) String() string {
25
-	return fmt.Sprintf("%v", []string(opts.values))
26
-}
27
-
28
-// Set validates if needed the input value and add it to the
29
-// internal slice.
30
-func (opts *ListOpts) Set(value string) error {
31
-	if opts.validator != nil {
32
-		v, err := opts.validator(value)
33
-		if err != nil {
34
-			return err
35
-		}
36
-		value = v
37
-	}
38
-	opts.values = append(opts.values, value)
39
-	return nil
40
-}
41
-
42
-// Delete remove the given element from the slice.
43
-func (opts *ListOpts) Delete(key string) {
44
-	for i, k := range opts.values {
45
-		if k == key {
46
-			opts.values = append(opts.values[:i], opts.values[i+1:]...)
47
-			return
48
-		}
49
-	}
50
-}
51
-
52
-// GetMap returns the content of values in a map in order to avoid
53
-// duplicates.
54
-// FIXME: can we remove this?
55
-func (opts *ListOpts) GetMap() map[string]struct{} {
56
-	ret := make(map[string]struct{})
57
-	for _, k := range opts.values {
58
-		ret[k] = struct{}{}
59
-	}
60
-	return ret
61
-}
62
-
63
-// GetAll returns the values' slice.
64
-// FIXME: Can we remove this?
65
-func (opts *ListOpts) GetAll() []string {
66
-	return opts.values
67
-}
68
-
69
-// Get checks the existence of the given key.
70
-func (opts *ListOpts) Get(key string) bool {
71
-	for _, k := range opts.values {
72
-		if k == key {
73
-			return true
74
-		}
75
-	}
76
-	return false
77
-}
78
-
79
-// Len returns the amount of element in the slice.
80
-func (opts *ListOpts) Len() int {
81
-	return len(opts.values)
82
-}
83
-
84
-// Validators
85
-type ValidatorFctType func(val string) (string, error)
86
-
87
-func ValidateAttach(val string) (string, error) {
88
-	if val != "stdin" && val != "stdout" && val != "stderr" {
89
-		return val, fmt.Errorf("Unsupported stream name: %s", val)
90
-	}
91
-	return val, nil
92
-}
93
-
94
-func ValidateLink(val string) (string, error) {
95
-	if _, err := utils.PartParser("name:alias", val); err != nil {
96
-		return val, err
97
-	}
98
-	return val, nil
99
-}
100
-
101
-func ValidatePath(val string) (string, error) {
102
-	var containerPath string
103
-
104
-	if strings.Count(val, ":") > 2 {
105
-		return val, fmt.Errorf("bad format for volumes: %s", val)
106
-	}
107
-
108
-	splited := strings.SplitN(val, ":", 2)
109
-	if len(splited) == 1 {
110
-		containerPath = splited[0]
111
-		val = filepath.Clean(splited[0])
112
-	} else {
113
-		containerPath = splited[1]
114
-		val = fmt.Sprintf("%s:%s", splited[0], filepath.Clean(splited[1]))
115
-	}
116
-
117
-	if !filepath.IsAbs(containerPath) {
118
-		return val, fmt.Errorf("%s is not an absolute path", containerPath)
119
-	}
120
-	return val, nil
121
-}
122
-
123
-func ValidateEnv(val string) (string, error) {
124
-	arr := strings.Split(val, "=")
125
-	if len(arr) > 1 {
126
-		return val, nil
127
-	}
128
-	return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
129
-}
130
-
131
-func ValidateIp4Address(val string) (string, error) {
132
-	re := regexp.MustCompile(`^(([0-9]+\.){3}([0-9]+))\s*$`)
133
-	var ns = re.FindSubmatch([]byte(val))
134
-	if len(ns) > 0 {
135
-		return string(ns[1]), nil
136
-	}
137
-	return "", fmt.Errorf("%s is not an ip4 address", val)
138
-}
139 1
deleted file mode 100644
... ...
@@ -1,24 +0,0 @@
1
-package opts
2
-
3
-import (
4
-	"testing"
5
-)
6
-
7
-func TestValidateIP4(t *testing.T) {
8
-	if ret, err := ValidateIp4Address(`1.2.3.4`); err != nil || ret == "" {
9
-		t.Fatalf("ValidateIp4Address(`1.2.3.4`) got %s %s", ret, err)
10
-	}
11
-
12
-	if ret, err := ValidateIp4Address(`127.0.0.1`); err != nil || ret == "" {
13
-		t.Fatalf("ValidateIp4Address(`127.0.0.1`) got %s %s", ret, err)
14
-	}
15
-
16
-	if ret, err := ValidateIp4Address(`127`); err == nil || ret != "" {
17
-		t.Fatalf("ValidateIp4Address(`127`) got %s %s", ret, err)
18
-	}
19
-
20
-	if ret, err := ValidateIp4Address(`random invalid string`); err == nil || ret != "" {
21
-		t.Fatalf("ValidateIp4Address(`random invalid string`) got %s %s", ret, err)
22
-	}
23
-
24
-}
... ...
@@ -3,8 +3,8 @@ package runconfig
3 3
 import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/nat"
6
+	"github.com/dotcloud/docker/opts"
6 7
 	flag "github.com/dotcloud/docker/pkg/mflag"
7
-	"github.com/dotcloud/docker/pkg/opts"
8 8
 	"github.com/dotcloud/docker/pkg/sysinfo"
9 9
 	"github.com/dotcloud/docker/utils"
10 10
 	"io/ioutil"