From f55d8241cfc8e436a1271e653adfa57a29883adf Mon Sep 17 00:00:00 2001 From: Tibor Vass Date: Tue, 5 May 2015 00:18:28 -0400 Subject: [PATCH] cli: new daemon command and new cli package This patch creates a new cli package that allows to combine both client and daemon commands (there is only one daemon command: docker daemon). The `-d` and `--daemon` top-level flags are deprecated and a special message is added to prompt the user to use `docker daemon`. Providing top-level daemon-specific flags for client commands result in an error message prompting the user to use `docker daemon`. This patch does not break any old but correct usages. This also makes `-d` and `--daemon` flags, as well as the `daemon` command illegal in client-only binaries. Signed-off-by: Tibor Vass --- opts/hosts_unix.go | 7 +++++++ opts/hosts_windows.go | 7 +++++++ opts/opts.go | 25 ++++++++++++++----------- opts/opts_test.go | 2 +- opts/ulimit.go | 13 ++++++++----- 5 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 opts/hosts_unix.go create mode 100644 opts/hosts_windows.go diff --git a/opts/hosts_unix.go b/opts/hosts_unix.go new file mode 100644 index 0000000000..a29335e605 --- /dev/null +++ b/opts/hosts_unix.go @@ -0,0 +1,7 @@ +// +build !windows + +package opts + +import "fmt" + +var DefaultHost = fmt.Sprintf("unix://%s", DefaultUnixSocket) diff --git a/opts/hosts_windows.go b/opts/hosts_windows.go new file mode 100644 index 0000000000..55eac2aaca --- /dev/null +++ b/opts/hosts_windows.go @@ -0,0 +1,7 @@ +// +build windows + +package opts + +import "fmt" + +var DefaultHost = fmt.Sprintf("tcp://%s:%d", DefaultHTTPHost, DefaultHTTPPort) diff --git a/opts/opts.go b/opts/opts.go index b739012791..6d1b2f7b99 100644 --- a/opts/opts.go +++ b/opts/opts.go @@ -32,37 +32,37 @@ var ( // ListVar Defines a flag with the specified names and usage, and put the value // list into ListOpts that will hold the values. func ListVar(values *[]string, names []string, usage string) { - flag.Var(newListOptsRef(values, nil), names, usage) + flag.Var(NewListOptsRef(values, nil), names, usage) } // MapVar Defines a flag with the specified names and usage, and put the value // map into MapOpt that will hold the values (key,value). func MapVar(values map[string]string, names []string, usage string) { - flag.Var(newMapOpt(values, nil), names, usage) + flag.Var(NewMapOpts(values, nil), names, usage) } // LogOptsVar Defines a flag with the specified names and usage for --log-opts, // and put the value map into MapOpt that will hold the values (key,value). func LogOptsVar(values map[string]string, names []string, usage string) { - flag.Var(newMapOpt(values, nil), names, usage) + flag.Var(NewMapOpts(values, nil), names, usage) } // HostListVar Defines a flag with the specified names and usage and put the // value into a ListOpts that will hold the values, validating the Host format. func HostListVar(values *[]string, names []string, usage string) { - flag.Var(newListOptsRef(values, ValidateHost), names, usage) + flag.Var(NewListOptsRef(values, ValidateHost), names, usage) } // IPListVar Defines a flag with the specified names and usage and put the // value into a ListOpts that will hold the values, validating the IP format. func IPListVar(values *[]string, names []string, usage string) { - flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage) + flag.Var(NewListOptsRef(values, ValidateIPAddress), names, usage) } // DNSSearchListVar Defines a flag with the specified names and usage and put the // value into a ListOpts that will hold the values, validating the DNS search format. func DNSSearchListVar(values *[]string, names []string, usage string) { - flag.Var(newListOptsRef(values, ValidateDNSSearch), names, usage) + flag.Var(NewListOptsRef(values, ValidateDNSSearch), names, usage) } // IPVar Defines a flag with the specified names and usage for IP and will use @@ -74,12 +74,12 @@ func IPVar(value *net.IP, names []string, defaultValue, usage string) { // LabelListVar Defines a flag with the specified names and usage and put the // value into a ListOpts that will hold the values, validating the label format. func LabelListVar(values *[]string, names []string, usage string) { - flag.Var(newListOptsRef(values, ValidateLabel), names, usage) + flag.Var(NewListOptsRef(values, ValidateLabel), names, usage) } // UlimitMapVar Defines a flag with the specified names and usage for --ulimit, // and put the value map into a UlimitOpt that will hold the values. -func UlimitMapVar(values map[string]*ulimit.Ulimit, names []string, usage string) { +func UlimitMapVar(values *map[string]*ulimit.Ulimit, names []string, usage string) { flag.Var(NewUlimitOpt(values), names, usage) } @@ -92,10 +92,10 @@ type ListOpts struct { // NewListOpts Create a new ListOpts with the specified validator. func NewListOpts(validator ValidatorFctType) ListOpts { var values []string - return *newListOptsRef(&values, validator) + return *NewListOptsRef(&values, validator) } -func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts { +func NewListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts { return &ListOpts{ values: values, validator: validator, @@ -191,7 +191,10 @@ func (opts *MapOpts) String() string { return fmt.Sprintf("%v", map[string]string((opts.values))) } -func newMapOpt(values map[string]string, validator ValidatorFctType) *MapOpts { +func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts { + if values == nil { + values = make(map[string]string) + } return &MapOpts{ values: values, validator: validator, diff --git a/opts/opts_test.go b/opts/opts_test.go index 3e639c1fa0..f08df30be6 100644 --- a/opts/opts_test.go +++ b/opts/opts_test.go @@ -32,7 +32,7 @@ func TestValidateIPAddress(t *testing.T) { func TestMapOpts(t *testing.T) { tmpMap := make(map[string]string) - o := newMapOpt(tmpMap, logOptsValidator) + o := NewMapOpts(tmpMap, logOptsValidator) o.Set("max-size=1") if o.String() != "map[max-size:1]" { t.Errorf("%s != [map[max-size:1]", o.String()) diff --git a/opts/ulimit.go b/opts/ulimit.go index 361eadf220..f8d34365f5 100644 --- a/opts/ulimit.go +++ b/opts/ulimit.go @@ -7,10 +7,13 @@ import ( ) type UlimitOpt struct { - values map[string]*ulimit.Ulimit + values *map[string]*ulimit.Ulimit } -func NewUlimitOpt(ref map[string]*ulimit.Ulimit) *UlimitOpt { +func NewUlimitOpt(ref *map[string]*ulimit.Ulimit) *UlimitOpt { + if ref == nil { + ref = &map[string]*ulimit.Ulimit{} + } return &UlimitOpt{ref} } @@ -20,14 +23,14 @@ func (o *UlimitOpt) Set(val string) error { return err } - o.values[l.Name] = l + (*o.values)[l.Name] = l return nil } func (o *UlimitOpt) String() string { var out []string - for _, v := range o.values { + for _, v := range *o.values { out = append(out, v.String()) } @@ -36,7 +39,7 @@ func (o *UlimitOpt) String() string { func (o *UlimitOpt) GetList() []*ulimit.Ulimit { var ulimits []*ulimit.Ulimit - for _, v := range o.values { + for _, v := range *o.values { ulimits = append(ulimits, v) }