pkg: move to more generic StringsFlag

This commit is contained in:
Jonathan Boulle
2014-11-04 15:57:38 -08:00
parent f71c247d87
commit 5de9d38cc6
4 changed files with 52 additions and 95 deletions

View File

@@ -1,55 +0,0 @@
/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package flags
import (
"errors"
)
const (
ProxyValueOff = "off"
ProxyValueReadonly = "readonly"
ProxyValueOn = "on"
)
var (
ProxyValues = []string{
ProxyValueOff,
ProxyValueReadonly,
ProxyValueOn,
}
)
// ProxyFlag implements the flag.Value interface.
type Proxy string
// Set verifies the argument to be a valid member of proxyFlagValues
// before setting the underlying flag value.
func (pf *Proxy) Set(s string) error {
for _, v := range ProxyValues {
if s == v {
*pf = Proxy(s)
return nil
}
}
return errors.New("invalid value")
}
func (pf *Proxy) String() string {
return string(*pf)
}

View File

@@ -16,38 +16,33 @@
package flags
import (
"errors"
)
import "errors"
const (
FallbackExit = "exit"
FallbackProxy = "proxy"
)
// NewStringsFlag creates a new string flag for which any one of the given
// strings is a valid value, and any other value is an error.
func NewStringsFlag(valids ...string) *StringsFlag {
return &StringsFlag{Values: valids}
}
var (
FallbackValues = []string{
FallbackExit,
FallbackProxy,
}
)
// StringsFlag implements the flag.Value interface.
type StringsFlag struct {
Values []string
val string
}
// FallbackFlag implements the flag.Value interface.
type Fallback string
// Set verifies the argument to be a valid member of FallbackFlagValues
// Set verifies the argument to be a valid member of the allowed values
// before setting the underlying flag value.
func (fb *Fallback) Set(s string) error {
for _, v := range FallbackValues {
func (ss *StringsFlag) Set(s string) error {
for _, v := range ss.Values {
if s == v {
*fb = Fallback(s)
ss.val = s
return nil
}
}
return errors.New("invalid value")
}
func (fb *Fallback) String() string {
return string(*fb)
// String returns the set value (if any) of the StringsFlag
func (ss *StringsFlag) String() string {
return ss.val
}

View File

@@ -20,23 +20,26 @@ import (
"testing"
)
func TestProxySet(t *testing.T) {
func TestStringsSet(t *testing.T) {
tests := []struct {
vals []string
val string
pass bool
}{
// known values
{"on", true},
{"off", true},
{[]string{"abc", "def"}, "abc", true},
{[]string{"on", "off", "false"}, "on", true},
// unrecognized values
{"foo", false},
{"", false},
{[]string{"abc", "def"}, "ghi", false},
{[]string{"on", "off"}, "", false},
{[]string{}, "asdf", false},
}
for i, tt := range tests {
pf := new(Proxy)
err := pf.Set(tt.val)
sf := NewStringsFlag(tt.vals...)
err := sf.Set(tt.val)
if tt.pass != (err == nil) {
t.Errorf("#%d: want pass=%t, but got err=%v", i, tt.pass, err)
}