mirror of https://github.com/hashicorp/consul
Exports config functions from base.
parent
a48ecf56a9
commit
e37f66d0b2
|
@ -36,10 +36,10 @@ type Command struct {
|
||||||
hidden *flag.FlagSet
|
hidden *flag.FlagSet
|
||||||
|
|
||||||
// These are the options which correspond to the HTTP API options
|
// These are the options which correspond to the HTTP API options
|
||||||
httpAddr stringValue
|
httpAddr StringValue
|
||||||
token stringValue
|
token StringValue
|
||||||
datacenter stringValue
|
datacenter StringValue
|
||||||
stale boolValue
|
stale BoolValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPClient returns a client with the parsed flags. It panics if the command
|
// HTTPClient returns a client with the parsed flags. It panics if the command
|
||||||
|
|
|
@ -20,32 +20,32 @@ import (
|
||||||
|
|
||||||
// configDecodeHook should be passed to mapstructure in order to decode into
|
// configDecodeHook should be passed to mapstructure in order to decode into
|
||||||
// the *Value objects here.
|
// the *Value objects here.
|
||||||
var configDecodeHook = mapstructure.ComposeDecodeHookFunc(
|
var ConfigDecodeHook = mapstructure.ComposeDecodeHookFunc(
|
||||||
boolToBoolValueFunc(),
|
BoolToBoolValueFunc(),
|
||||||
stringToDurationValueFunc(),
|
StringToDurationValueFunc(),
|
||||||
stringToStringValueFunc(),
|
StringToStringValueFunc(),
|
||||||
float64ToUintValueFunc(),
|
Float64ToUintValueFunc(),
|
||||||
)
|
)
|
||||||
|
|
||||||
// boolValue provides a flag value that's aware if it has been set.
|
// BoolValue provides a flag value that's aware if it has been set.
|
||||||
type boolValue struct {
|
type BoolValue struct {
|
||||||
v *bool
|
v *bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (b *boolValue) IsBoolFlag() bool {
|
func (b *BoolValue) IsBoolFlag() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge will overlay this value if it has been set.
|
// Merge will overlay this value if it has been set.
|
||||||
func (b *boolValue) Merge(onto *bool) {
|
func (b *BoolValue) Merge(onto *bool) {
|
||||||
if b.v != nil {
|
if b.v != nil {
|
||||||
*onto = *(b.v)
|
*onto = *(b.v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (b *boolValue) Set(v string) error {
|
func (b *BoolValue) Set(v string) error {
|
||||||
if b.v == nil {
|
if b.v == nil {
|
||||||
b.v = new(bool)
|
b.v = new(bool)
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ func (b *boolValue) Set(v string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (b *boolValue) String() string {
|
func (b *BoolValue) String() string {
|
||||||
var current bool
|
var current bool
|
||||||
if b.v != nil {
|
if b.v != nil {
|
||||||
current = *(b.v)
|
current = *(b.v)
|
||||||
|
@ -63,9 +63,9 @@ func (b *boolValue) String() string {
|
||||||
return fmt.Sprintf("%v", current)
|
return fmt.Sprintf("%v", current)
|
||||||
}
|
}
|
||||||
|
|
||||||
// boolToBoolValueFunc is a mapstructure hook that looks for an incoming bool
|
// BoolToBoolValueFunc is a mapstructure hook that looks for an incoming bool
|
||||||
// mapped to a boolValue and does the translation.
|
// mapped to a BoolValue and does the translation.
|
||||||
func boolToBoolValueFunc() mapstructure.DecodeHookFunc {
|
func BoolToBoolValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return func(
|
return func(
|
||||||
f reflect.Type,
|
f reflect.Type,
|
||||||
t reflect.Type,
|
t reflect.Type,
|
||||||
|
@ -74,7 +74,7 @@ func boolToBoolValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
val := boolValue{}
|
val := BoolValue{}
|
||||||
if t != reflect.TypeOf(val) {
|
if t != reflect.TypeOf(val) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
@ -85,20 +85,20 @@ func boolToBoolValueFunc() mapstructure.DecodeHookFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// durationValue provides a flag value that's aware if it has been set.
|
// DurationValue provides a flag value that's aware if it has been set.
|
||||||
type durationValue struct {
|
type DurationValue struct {
|
||||||
v *time.Duration
|
v *time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge will overlay this value if it has been set.
|
// Merge will overlay this value if it has been set.
|
||||||
func (d *durationValue) Merge(onto *time.Duration) {
|
func (d *DurationValue) Merge(onto *time.Duration) {
|
||||||
if d.v != nil {
|
if d.v != nil {
|
||||||
*onto = *(d.v)
|
*onto = *(d.v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (d *durationValue) Set(v string) error {
|
func (d *DurationValue) Set(v string) error {
|
||||||
if d.v == nil {
|
if d.v == nil {
|
||||||
d.v = new(time.Duration)
|
d.v = new(time.Duration)
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ func (d *durationValue) Set(v string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (d *durationValue) String() string {
|
func (d *DurationValue) String() string {
|
||||||
var current time.Duration
|
var current time.Duration
|
||||||
if d.v != nil {
|
if d.v != nil {
|
||||||
current = *(d.v)
|
current = *(d.v)
|
||||||
|
@ -116,9 +116,9 @@ func (d *durationValue) String() string {
|
||||||
return current.String()
|
return current.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// stringToDurationValueFunc is a mapstructure hook that looks for an incoming
|
// StringToDurationValueFunc is a mapstructure hook that looks for an incoming
|
||||||
// string mapped to a durationValue and does the translation.
|
// string mapped to a DurationValue and does the translation.
|
||||||
func stringToDurationValueFunc() mapstructure.DecodeHookFunc {
|
func StringToDurationValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return func(
|
return func(
|
||||||
f reflect.Type,
|
f reflect.Type,
|
||||||
t reflect.Type,
|
t reflect.Type,
|
||||||
|
@ -127,7 +127,7 @@ func stringToDurationValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
val := durationValue{}
|
val := DurationValue{}
|
||||||
if t != reflect.TypeOf(val) {
|
if t != reflect.TypeOf(val) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
@ -138,20 +138,20 @@ func stringToDurationValueFunc() mapstructure.DecodeHookFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// stringValue provides a flag value that's aware if it has been set.
|
// StringValue provides a flag value that's aware if it has been set.
|
||||||
type stringValue struct {
|
type StringValue struct {
|
||||||
v *string
|
v *string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge will overlay this value if it has been set.
|
// Merge will overlay this value if it has been set.
|
||||||
func (s *stringValue) Merge(onto *string) {
|
func (s *StringValue) Merge(onto *string) {
|
||||||
if s.v != nil {
|
if s.v != nil {
|
||||||
*onto = *(s.v)
|
*onto = *(s.v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (s *stringValue) Set(v string) error {
|
func (s *StringValue) Set(v string) error {
|
||||||
if s.v == nil {
|
if s.v == nil {
|
||||||
s.v = new(string)
|
s.v = new(string)
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,7 @@ func (s *stringValue) Set(v string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (s *stringValue) String() string {
|
func (s *StringValue) String() string {
|
||||||
var current string
|
var current string
|
||||||
if s.v != nil {
|
if s.v != nil {
|
||||||
current = *(s.v)
|
current = *(s.v)
|
||||||
|
@ -168,9 +168,9 @@ func (s *stringValue) String() string {
|
||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
// stringToStringValueFunc is a mapstructure hook that looks for an incoming
|
// StringToStringValueFunc is a mapstructure hook that looks for an incoming
|
||||||
// string mapped to a stringValue and does the translation.
|
// string mapped to a StringValue and does the translation.
|
||||||
func stringToStringValueFunc() mapstructure.DecodeHookFunc {
|
func StringToStringValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return func(
|
return func(
|
||||||
f reflect.Type,
|
f reflect.Type,
|
||||||
t reflect.Type,
|
t reflect.Type,
|
||||||
|
@ -179,7 +179,7 @@ func stringToStringValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
val := stringValue{}
|
val := StringValue{}
|
||||||
if t != reflect.TypeOf(val) {
|
if t != reflect.TypeOf(val) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
@ -189,20 +189,20 @@ func stringToStringValueFunc() mapstructure.DecodeHookFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// uintValue provides a flag value that's aware if it has been set.
|
// UintValue provides a flag value that's aware if it has been set.
|
||||||
type uintValue struct {
|
type UintValue struct {
|
||||||
v *uint
|
v *uint
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge will overlay this value if it has been set.
|
// Merge will overlay this value if it has been set.
|
||||||
func (u *uintValue) Merge(onto *uint) {
|
func (u *UintValue) Merge(onto *uint) {
|
||||||
if u.v != nil {
|
if u.v != nil {
|
||||||
*onto = *(u.v)
|
*onto = *(u.v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (u *uintValue) Set(v string) error {
|
func (u *UintValue) Set(v string) error {
|
||||||
if u.v == nil {
|
if u.v == nil {
|
||||||
u.v = new(uint)
|
u.v = new(uint)
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ func (u *uintValue) Set(v string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See flag.Value.
|
// See flag.Value.
|
||||||
func (u *uintValue) String() string {
|
func (u *UintValue) String() string {
|
||||||
var current uint
|
var current uint
|
||||||
if u.v != nil {
|
if u.v != nil {
|
||||||
current = *(u.v)
|
current = *(u.v)
|
||||||
|
@ -220,9 +220,9 @@ func (u *uintValue) String() string {
|
||||||
return fmt.Sprintf("%v", current)
|
return fmt.Sprintf("%v", current)
|
||||||
}
|
}
|
||||||
|
|
||||||
// float64ToUintValueFunc is a mapstructure hook that looks for an incoming
|
// Float64ToUintValueFunc is a mapstructure hook that looks for an incoming
|
||||||
// float64 mapped to a uintValue and does the translation.
|
// float64 mapped to a UintValue and does the translation.
|
||||||
func float64ToUintValueFunc() mapstructure.DecodeHookFunc {
|
func Float64ToUintValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return func(
|
return func(
|
||||||
f reflect.Type,
|
f reflect.Type,
|
||||||
t reflect.Type,
|
t reflect.Type,
|
||||||
|
@ -231,7 +231,7 @@ func float64ToUintValueFunc() mapstructure.DecodeHookFunc {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
val := uintValue{}
|
val := UintValue{}
|
||||||
if t != reflect.TypeOf(val) {
|
if t != reflect.TypeOf(val) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,10 +14,10 @@ import (
|
||||||
|
|
||||||
func TestConfigUtil_Values(t *testing.T) {
|
func TestConfigUtil_Values(t *testing.T) {
|
||||||
type config struct {
|
type config struct {
|
||||||
B boolValue `mapstructure:"bool"`
|
B BoolValue `mapstructure:"bool"`
|
||||||
D durationValue `mapstructure:"duration"`
|
D DurationValue `mapstructure:"duration"`
|
||||||
S stringValue `mapstructure:"string"`
|
S StringValue `mapstructure:"string"`
|
||||||
U uintValue `mapstructure:"uint"`
|
U UintValue `mapstructure:"uint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
@ -70,7 +70,7 @@ func TestConfigUtil_Values(t *testing.T) {
|
||||||
|
|
||||||
var r config
|
var r config
|
||||||
msdec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
msdec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||||
DecodeHook: configDecodeHook,
|
DecodeHook: ConfigDecodeHook,
|
||||||
Result: &r,
|
Result: &r,
|
||||||
ErrorUnused: true,
|
ErrorUnused: true,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue