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
Родитель 6d59a56675
Коммит 6200002669
5 изменённых файлов: 54 добавлений и 20 удалений

Просмотреть файл

@ -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 {
@ -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 {

Просмотреть файл

@ -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")

Просмотреть файл

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

Просмотреть файл

@ -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{
`.`,

Просмотреть файл

@ -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")