Skip to content

Commit

Permalink
Helpers to parse lists, IPs, hosts, dns searches from the command line
Browse files Browse the repository at this point in the history
Signed-off-by: Solomon Hykes <[email protected]>
  • Loading branch information
Solomon Hykes committed Aug 13, 2014
1 parent 6d59a56 commit 6200002
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 20 deletions.
4 changes: 2 additions & 2 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ func (cli *DockerCli) CmdImages(args ...string) error {
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")

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

if err := cmd.Parse(args); err != nil {
Expand Down Expand Up @@ -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.")
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>")

if err := cmd.Parse(args); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion docker/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
var (
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
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")
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")
Expand Down
48 changes: 38 additions & 10 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,51 @@ import (
"regexp"
"strings"

"github.com/docker/docker/api"
flag "github.com/docker/docker/pkg/mflag"
"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
type ListOpts struct {
values []string
values *[]string
validator ValidatorFctType
}

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,
}
}

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
Expand All @@ -37,15 +65,15 @@ func (opts *ListOpts) Set(value string) error {
}
value = v
}
opts.values = append(opts.values, value)
(*opts.values) = append((*opts.values), value)
return nil
}

// Delete remove the given element from the slice.
func (opts *ListOpts) Delete(key string) {
for i, k := range opts.values {
for i, k := range *opts.values {
if k == key {
opts.values = append(opts.values[:i], opts.values[i+1:]...)
(*opts.values) = append((*opts.values)[:i], (*opts.values)[i+1:]...)
return
}
}
Expand All @@ -56,7 +84,7 @@ func (opts *ListOpts) Delete(key string) {
// FIXME: can we remove this?
func (opts *ListOpts) GetMap() map[string]struct{} {
ret := make(map[string]struct{})
for _, k := range opts.values {
for _, k := range *opts.values {
ret[k] = struct{}{}
}
return ret
Expand All @@ -65,12 +93,12 @@ func (opts *ListOpts) GetMap() map[string]struct{} {
// GetAll returns the values' slice.
// FIXME: Can we remove this?
func (opts *ListOpts) GetAll() []string {
return opts.values
return (*opts.values)
}

// Get checks the existence of the given key.
func (opts *ListOpts) Get(key string) bool {
for _, k := range opts.values {
for _, k := range *opts.values {
if k == key {
return true
}
Expand All @@ -80,7 +108,7 @@ func (opts *ListOpts) Get(key string) bool {

// Len returns the amount of element in the slice.
func (opts *ListOpts) Len() int {
return len(opts.values)
return len((*opts.values))
}

// Validators
Expand Down
6 changes: 6 additions & 0 deletions opts/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
valid := []string{
`.`,
Expand Down
14 changes: 7 additions & 7 deletions runconfig/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flEnv = opts.NewListOpts(opts.ValidateEnv)
flDevices = opts.NewListOpts(opts.ValidatePath)

flPublish opts.ListOpts
flExpose opts.ListOpts
flPublish = opts.NewListOpts(nil)
flExpose = opts.NewListOpts(nil)
flDns = opts.NewListOpts(opts.ValidateIPAddress)
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch)
flVolumesFrom opts.ListOpts
flLxcOpts opts.ListOpts
flEnvFile opts.ListOpts
flCapAdd opts.ListOpts
flCapDrop opts.ListOpts
flVolumesFrom = opts.NewListOpts(nil)
flLxcOpts = opts.NewListOpts(nil)
flEnvFile = opts.NewListOpts(nil)
flCapAdd = opts.NewListOpts(nil)
flCapDrop = opts.NewListOpts(nil)

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")
Expand Down

0 comments on commit 6200002

Please sign in to comment.