Browse code

Helpers to parse lists, IPs, hosts, dns searches from the command line

Signed-off-by: Solomon Hykes <solomon@docker.com>

Solomon Hykes authored on 2014/08/10 10:13:44
Showing 5 changed files
... ...
@@ -1254,7 +1254,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
1254 1254
 	flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
1255 1255
 	flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
1256 1256
 
1257
-	var flFilter opts.ListOpts
1257
+	flFilter := opts.NewListOpts(nil)
1258 1258
 	cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
1259 1259
 
1260 1260
 	if err := cmd.Parse(args); err != nil {
... ...
@@ -1487,7 +1487,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
1487 1487
 	before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
1488 1488
 	last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
1489 1489
 
1490
-	var flFilter opts.ListOpts
1490
+	flFilter := opts.NewListOpts(nil)
1491 1491
 	cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>")
1492 1492
 
1493 1493
 	if err := cmd.Parse(args); err != nil {
... ...
@@ -22,7 +22,7 @@ func init() {
22 22
 var (
23 23
 	flVersion            = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
24 24
 	flDaemon             = flag.Bool([]string{"d", "-daemon"}, false, "Enable daemon mode")
25
-	flGraphOpts          opts.ListOpts
25
+	flGraphOpts          = opts.NewListOpts(nil)
26 26
 	flDebug              = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
27 27
 	flAutoRestart        = flag.Bool([]string{"r", "-restart"}, true, "Restart previously running containers")
28 28
 	bridgeName           = flag.String([]string{"b", "-bridge"}, "", "Attach containers to a pre-existing network bridge\nuse 'none' to disable container networking")
... ...
@@ -8,23 +8,51 @@ import (
8 8
 	"regexp"
9 9
 	"strings"
10 10
 
11
+	"github.com/docker/docker/api"
12
+	flag "github.com/docker/docker/pkg/mflag"
11 13
 	"github.com/docker/docker/pkg/parsers"
12 14
 )
13 15
 
16
+func ListVar(values *[]string, names []string, usage string) {
17
+	flag.Var(newListOptsRef(values, nil), names, usage)
18
+}
19
+
20
+func HostListVar(values *[]string, names []string, usage string) {
21
+	flag.Var(newListOptsRef(values, api.ValidateHost), names, usage)
22
+}
23
+
24
+func IPListVar(values *[]string, names []string, usage string) {
25
+	flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
26
+}
27
+
28
+func DnsSearchListVar(values *[]string, names []string, usage string) {
29
+	flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
30
+}
31
+
32
+func IPVar(value *net.IP, names []string, defaultValue, usage string) {
33
+	flag.Var(NewIpOpt(value, defaultValue), names, usage)
34
+}
35
+
14 36
 // ListOpts type
15 37
 type ListOpts struct {
16
-	values    []string
38
+	values    *[]string
17 39
 	validator ValidatorFctType
18 40
 }
19 41
 
20 42
 func NewListOpts(validator ValidatorFctType) ListOpts {
21
-	return ListOpts{
43
+	var values []string
44
+	return *newListOptsRef(&values, validator)
45
+}
46
+
47
+func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
48
+	return &ListOpts{
49
+		values:    values,
22 50
 		validator: validator,
23 51
 	}
24 52
 }
25 53
 
26 54
 func (opts *ListOpts) String() string {
27
-	return fmt.Sprintf("%v", []string(opts.values))
55
+	return fmt.Sprintf("%v", []string((*opts.values)))
28 56
 }
29 57
 
30 58
 // Set validates if needed the input value and add it to the
... ...
@@ -37,15 +65,15 @@ func (opts *ListOpts) Set(value string) error {
37 37
 		}
38 38
 		value = v
39 39
 	}
40
-	opts.values = append(opts.values, value)
40
+	(*opts.values) = append((*opts.values), value)
41 41
 	return nil
42 42
 }
43 43
 
44 44
 // Delete remove the given element from the slice.
45 45
 func (opts *ListOpts) Delete(key string) {
46
-	for i, k := range opts.values {
46
+	for i, k := range *opts.values {
47 47
 		if k == key {
48
-			opts.values = append(opts.values[:i], opts.values[i+1:]...)
48
+			(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
49 49
 			return
50 50
 		}
51 51
 	}
... ...
@@ -56,7 +84,7 @@ func (opts *ListOpts) Delete(key string) {
56 56
 // FIXME: can we remove this?
57 57
 func (opts *ListOpts) GetMap() map[string]struct{} {
58 58
 	ret := make(map[string]struct{})
59
-	for _, k := range opts.values {
59
+	for _, k := range *opts.values {
60 60
 		ret[k] = struct{}{}
61 61
 	}
62 62
 	return ret
... ...
@@ -65,12 +93,12 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
65 65
 // GetAll returns the values' slice.
66 66
 // FIXME: Can we remove this?
67 67
 func (opts *ListOpts) GetAll() []string {
68
-	return opts.values
68
+	return (*opts.values)
69 69
 }
70 70
 
71 71
 // Get checks the existence of the given key.
72 72
 func (opts *ListOpts) Get(key string) bool {
73
-	for _, k := range opts.values {
73
+	for _, k := range *opts.values {
74 74
 		if k == key {
75 75
 			return true
76 76
 		}
... ...
@@ -80,7 +108,7 @@ func (opts *ListOpts) Get(key string) bool {
80 80
 
81 81
 // Len returns the amount of element in the slice.
82 82
 func (opts *ListOpts) Len() int {
83
-	return len(opts.values)
83
+	return len((*opts.values))
84 84
 }
85 85
 
86 86
 // Validators
... ...
@@ -27,6 +27,12 @@ func TestValidateIPAddress(t *testing.T) {
27 27
 
28 28
 }
29 29
 
30
+func TestListOpts(t *testing.T) {
31
+	o := NewListOpts(nil)
32
+	o.Set("foo")
33
+	o.String()
34
+}
35
+
30 36
 func TestValidateDnsSearch(t *testing.T) {
31 37
 	valid := []string{
32 38
 		`.`,
... ...
@@ -45,15 +45,15 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
45 45
 		flEnv     = opts.NewListOpts(opts.ValidateEnv)
46 46
 		flDevices = opts.NewListOpts(opts.ValidatePath)
47 47
 
48
-		flPublish     opts.ListOpts
49
-		flExpose      opts.ListOpts
48
+		flPublish     = opts.NewListOpts(nil)
49
+		flExpose      = opts.NewListOpts(nil)
50 50
 		flDns         = opts.NewListOpts(opts.ValidateIPAddress)
51 51
 		flDnsSearch   = opts.NewListOpts(opts.ValidateDnsSearch)
52
-		flVolumesFrom opts.ListOpts
53
-		flLxcOpts     opts.ListOpts
54
-		flEnvFile     opts.ListOpts
55
-		flCapAdd      opts.ListOpts
56
-		flCapDrop     opts.ListOpts
52
+		flVolumesFrom = opts.NewListOpts(nil)
53
+		flLxcOpts     = opts.NewListOpts(nil)
54
+		flEnvFile     = opts.NewListOpts(nil)
55
+		flCapAdd      = opts.NewListOpts(nil)
56
+		flCapDrop     = opts.NewListOpts(nil)
57 57
 
58 58
 		flAutoRemove      = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
59 59
 		flDetach          = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID")