Skip to content

Commit 6200002

Browse files
author
Solomon Hykes
committed
Helpers to parse lists, IPs, hosts, dns searches from the command line
Signed-off-by: Solomon Hykes <[email protected]>
1 parent 6d59a56 commit 6200002

5 files changed

Lines changed: 54 additions & 20 deletions

File tree

api/client/commands.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
12541254
flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
12551255
flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
12561256

1257-
var flFilter opts.ListOpts
1257+
flFilter := opts.NewListOpts(nil)
12581258
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
12591259

12601260
if err := cmd.Parse(args); err != nil {
@@ -1487,7 +1487,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
14871487
before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
14881488
last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
14891489

1490-
var flFilter opts.ListOpts
1490+
flFilter := opts.NewListOpts(nil)
14911491
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>")
14921492

14931493
if err := cmd.Parse(args); err != nil {

docker/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func init() {
2222
var (
2323
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
2424
flDaemon = flag.Bool([]string{"d", "-daemon"}, false, "Enable daemon mode")
25-
flGraphOpts opts.ListOpts
25+
flGraphOpts = opts.NewListOpts(nil)
2626
flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
2727
flAutoRestart = flag.Bool([]string{"r", "-restart"}, true, "Restart previously running containers")
2828
bridgeName = flag.String([]string{"b", "-bridge"}, "", "Attach containers to a pre-existing network bridge\nuse 'none' to disable container networking")

opts/opts.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,51 @@ import (
88
"regexp"
99
"strings"
1010

11+
"github.com/docker/docker/api"
12+
flag "github.com/docker/docker/pkg/mflag"
1113
"github.com/docker/docker/pkg/parsers"
1214
)
1315

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+
1436
// ListOpts type
1537
type ListOpts struct {
16-
values []string
38+
values *[]string
1739
validator ValidatorFctType
1840
}
1941

2042
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,
2250
validator: validator,
2351
}
2452
}
2553

2654
func (opts *ListOpts) String() string {
27-
return fmt.Sprintf("%v", []string(opts.values))
55+
return fmt.Sprintf("%v", []string((*opts.values)))
2856
}
2957

3058
// Set validates if needed the input value and add it to the
@@ -37,15 +65,15 @@ func (opts *ListOpts) Set(value string) error {
3765
}
3866
value = v
3967
}
40-
opts.values = append(opts.values, value)
68+
(*opts.values) = append((*opts.values), value)
4169
return nil
4270
}
4371

4472
// Delete remove the given element from the slice.
4573
func (opts *ListOpts) Delete(key string) {
46-
for i, k := range opts.values {
74+
for i, k := range *opts.values {
4775
if k == key {
48-
opts.values = append(opts.values[:i], opts.values[i+1:]...)
76+
(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
4977
return
5078
}
5179
}
@@ -56,7 +84,7 @@ func (opts *ListOpts) Delete(key string) {
5684
// FIXME: can we remove this?
5785
func (opts *ListOpts) GetMap() map[string]struct{} {
5886
ret := make(map[string]struct{})
59-
for _, k := range opts.values {
87+
for _, k := range *opts.values {
6088
ret[k] = struct{}{}
6189
}
6290
return ret
@@ -65,12 +93,12 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
6593
// GetAll returns the values' slice.
6694
// FIXME: Can we remove this?
6795
func (opts *ListOpts) GetAll() []string {
68-
return opts.values
96+
return (*opts.values)
6997
}
7098

7199
// Get checks the existence of the given key.
72100
func (opts *ListOpts) Get(key string) bool {
73-
for _, k := range opts.values {
101+
for _, k := range *opts.values {
74102
if k == key {
75103
return true
76104
}
@@ -80,7 +108,7 @@ func (opts *ListOpts) Get(key string) bool {
80108

81109
// Len returns the amount of element in the slice.
82110
func (opts *ListOpts) Len() int {
83-
return len(opts.values)
111+
return len((*opts.values))
84112
}
85113

86114
// Validators

opts/opts_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ func TestValidateIPAddress(t *testing.T) {
2727

2828
}
2929

30+
func TestListOpts(t *testing.T) {
31+
o := NewListOpts(nil)
32+
o.Set("foo")
33+
o.String()
34+
}
35+
3036
func TestValidateDnsSearch(t *testing.T) {
3137
valid := []string{
3238
`.`,

runconfig/parse.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
4545
flEnv = opts.NewListOpts(opts.ValidateEnv)
4646
flDevices = opts.NewListOpts(opts.ValidatePath)
4747

48-
flPublish opts.ListOpts
49-
flExpose opts.ListOpts
48+
flPublish = opts.NewListOpts(nil)
49+
flExpose = opts.NewListOpts(nil)
5050
flDns = opts.NewListOpts(opts.ValidateIPAddress)
5151
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)
5757

5858
flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
5959
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID")

0 commit comments

Comments
 (0)