mirror of https://github.com/docker/cli.git
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:
parent
f34ca0a354
commit
a6487884e5
48
opts/opts.go
48
opts/opts.go
|
@ -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
|
||||||
|
|
|
@ -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{
|
||||||
`.`,
|
`.`,
|
||||||
|
|
Loading…
Reference in New Issue