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

Signed-off-by: Solomon Hykes <solomon@docker.com>
This commit is contained in:
Solomon Hykes 2014-08-10 01:13:44 +00:00
parent 6d59a56675
commit 6200002669
5 changed files with 54 additions and 20 deletions

View File

@ -1254,7 +1254,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format") flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format") flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
var flFilter opts.ListOpts flFilter := opts.NewListOpts(nil)
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')") cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {
@ -1487,7 +1487,7 @@ func (cli *DockerCli) CmdPs(args ...string) error {
before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.") before := cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.") last := cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
var flFilter opts.ListOpts flFilter := opts.NewListOpts(nil)
cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>") cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>")
if err := cmd.Parse(args); err != nil { if err := cmd.Parse(args); err != nil {

View File

@ -22,7 +22,7 @@ func init() {
var ( var (
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit") flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
flDaemon = flag.Bool([]string{"d", "-daemon"}, false, "Enable daemon mode") flDaemon = flag.Bool([]string{"d", "-daemon"}, false, "Enable daemon mode")
flGraphOpts opts.ListOpts flGraphOpts = opts.NewListOpts(nil)
flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode") flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
flAutoRestart = flag.Bool([]string{"r", "-restart"}, true, "Restart previously running containers") flAutoRestart = flag.Bool([]string{"r", "-restart"}, true, "Restart previously running containers")
bridgeName = flag.String([]string{"b", "-bridge"}, "", "Attach containers to a pre-existing network bridge\nuse 'none' to disable container networking") bridgeName = flag.String([]string{"b", "-bridge"}, "", "Attach containers to a pre-existing network bridge\nuse 'none' to disable container networking")

View File

@ -8,23 +8,51 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/docker/docker/api"
flag "github.com/docker/docker/pkg/mflag"
"github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/parsers"
) )
func ListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, nil), names, usage)
}
func HostListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, api.ValidateHost), names, usage)
}
func IPListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, ValidateIPAddress), names, usage)
}
func DnsSearchListVar(values *[]string, names []string, usage string) {
flag.Var(newListOptsRef(values, ValidateDnsSearch), names, usage)
}
func IPVar(value *net.IP, names []string, defaultValue, usage string) {
flag.Var(NewIpOpt(value, defaultValue), names, usage)
}
// ListOpts type // ListOpts type
type ListOpts struct { type ListOpts struct {
values []string values *[]string
validator ValidatorFctType validator ValidatorFctType
} }
func NewListOpts(validator ValidatorFctType) ListOpts { func NewListOpts(validator ValidatorFctType) ListOpts {
return ListOpts{ var values []string
return *newListOptsRef(&values, validator)
}
func newListOptsRef(values *[]string, validator ValidatorFctType) *ListOpts {
return &ListOpts{
values: values,
validator: validator, validator: validator,
} }
} }
func (opts *ListOpts) String() string { func (opts *ListOpts) String() string {
return fmt.Sprintf("%v", []string(opts.values)) return fmt.Sprintf("%v", []string((*opts.values)))
} }
// Set validates if needed the input value and add it to the // Set validates if needed the input value and add it to the
@ -37,15 +65,15 @@ func (opts *ListOpts) Set(value string) error {
} }
value = v value = v
} }
opts.values = append(opts.values, value) (*opts.values) = append((*opts.values), value)
return nil return nil
} }
// Delete remove the given element from the slice. // Delete remove the given element from the slice.
func (opts *ListOpts) Delete(key string) { func (opts *ListOpts) Delete(key string) {
for i, k := range opts.values { for i, k := range *opts.values {
if k == key { if k == key {
opts.values = append(opts.values[:i], opts.values[i+1:]...) (*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
return return
} }
} }
@ -56,7 +84,7 @@ func (opts *ListOpts) Delete(key string) {
// FIXME: can we remove this? // FIXME: can we remove this?
func (opts *ListOpts) GetMap() map[string]struct{} { func (opts *ListOpts) GetMap() map[string]struct{} {
ret := make(map[string]struct{}) ret := make(map[string]struct{})
for _, k := range opts.values { for _, k := range *opts.values {
ret[k] = struct{}{} ret[k] = struct{}{}
} }
return ret return ret
@ -65,12 +93,12 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
// GetAll returns the values' slice. // GetAll returns the values' slice.
// FIXME: Can we remove this? // FIXME: Can we remove this?
func (opts *ListOpts) GetAll() []string { func (opts *ListOpts) GetAll() []string {
return opts.values return (*opts.values)
} }
// Get checks the existence of the given key. // Get checks the existence of the given key.
func (opts *ListOpts) Get(key string) bool { func (opts *ListOpts) Get(key string) bool {
for _, k := range opts.values { for _, k := range *opts.values {
if k == key { if k == key {
return true return true
} }
@ -80,7 +108,7 @@ func (opts *ListOpts) Get(key string) bool {
// Len returns the amount of element in the slice. // Len returns the amount of element in the slice.
func (opts *ListOpts) Len() int { func (opts *ListOpts) Len() int {
return len(opts.values) return len((*opts.values))
} }
// Validators // Validators

View File

@ -27,6 +27,12 @@ func TestValidateIPAddress(t *testing.T) {
} }
func TestListOpts(t *testing.T) {
o := NewListOpts(nil)
o.Set("foo")
o.String()
}
func TestValidateDnsSearch(t *testing.T) { func TestValidateDnsSearch(t *testing.T) {
valid := []string{ valid := []string{
`.`, `.`,

View File

@ -45,15 +45,15 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flEnv = opts.NewListOpts(opts.ValidateEnv) flEnv = opts.NewListOpts(opts.ValidateEnv)
flDevices = opts.NewListOpts(opts.ValidatePath) flDevices = opts.NewListOpts(opts.ValidatePath)
flPublish opts.ListOpts flPublish = opts.NewListOpts(nil)
flExpose opts.ListOpts flExpose = opts.NewListOpts(nil)
flDns = opts.NewListOpts(opts.ValidateIPAddress) flDns = opts.NewListOpts(opts.ValidateIPAddress)
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch) flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch)
flVolumesFrom opts.ListOpts flVolumesFrom = opts.NewListOpts(nil)
flLxcOpts opts.ListOpts flLxcOpts = opts.NewListOpts(nil)
flEnvFile opts.ListOpts flEnvFile = opts.NewListOpts(nil)
flCapAdd opts.ListOpts flCapAdd = opts.NewListOpts(nil)
flCapDrop opts.ListOpts flCapDrop = opts.NewListOpts(nil)
flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)") flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID") flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID")