2015-12-09 17:15:46 +00:00
|
|
|
package altsrc
|
2015-12-06 23:43:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-08-27 23:09:14 +00:00
|
|
|
"gopkg.in/urfave/cli.v1"
|
2015-12-06 23:43:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FlagInputSourceExtension is an extension interface of cli.Flag that
|
|
|
|
// allows a value to be set on the existing parsed flags.
|
|
|
|
type FlagInputSourceExtension interface {
|
|
|
|
cli.Flag
|
2015-12-09 17:15:46 +00:00
|
|
|
ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValues iterates over all provided flags and
|
|
|
|
// executes ApplyInputSourceValue on flags implementing the
|
|
|
|
// FlagInputSourceExtension interface to initialize these flags
|
|
|
|
// to an alternate input source.
|
|
|
|
func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSourceContext, flags []cli.Flag) error {
|
|
|
|
for _, f := range flags {
|
|
|
|
inputSourceExtendedFlag, isType := f.(FlagInputSourceExtension)
|
|
|
|
if isType {
|
|
|
|
err := inputSourceExtendedFlag.ApplyInputSourceValue(context, inputSourceContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitInputSource is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
|
|
|
|
// input source based on the func provided. If there is no error it will then apply the new input source to any flags
|
|
|
|
// that are supported by the input source
|
2016-04-25 22:29:05 +00:00
|
|
|
func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
|
2016-04-27 13:12:34 +00:00
|
|
|
return func(context *cli.Context) error {
|
2015-12-09 17:15:46 +00:00
|
|
|
inputSource, err := createInputSource()
|
|
|
|
if err != nil {
|
2016-04-27 13:12:34 +00:00
|
|
|
return fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
|
2015-12-09 17:15:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
return ApplyInputSourceValues(context, inputSource, flags)
|
2015-12-09 17:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitInputSourceWithContext is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
|
|
|
|
// input source based on the func provided with potentially using existing cli.Context values to initialize itself. If there is
|
|
|
|
// no error it will then apply the new input source to any flags that are supported by the input source
|
2016-04-25 22:29:05 +00:00
|
|
|
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
|
2016-04-27 13:12:34 +00:00
|
|
|
return func(context *cli.Context) error {
|
2015-12-09 17:15:46 +00:00
|
|
|
inputSource, err := createInputSource(context)
|
|
|
|
if err != nil {
|
2016-04-27 13:12:34 +00:00
|
|
|
return fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
|
2015-12-09 17:15:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:12:34 +00:00
|
|
|
return ApplyInputSourceValues(context, inputSource, flags)
|
2015-12-09 17:15:46 +00:00
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.Generic(f.GenericFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value != nil {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, value.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
|
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.StringSlice(f.StringSliceFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value != nil {
|
|
|
|
var sliceValue cli.StringSlice = value
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
underlyingFlag := f.set.Lookup(f.Name)
|
|
|
|
if underlyingFlag != nil {
|
|
|
|
underlyingFlag.Value = &sliceValue
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a IntSlice value if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.IntSlice(f.IntSliceFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value != nil {
|
|
|
|
var sliceValue cli.IntSlice = value
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
underlyingFlag := f.set.Lookup(f.Name)
|
|
|
|
if underlyingFlag != nil {
|
|
|
|
underlyingFlag.Value = &sliceValue
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.Bool(f.BoolFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, strconv.FormatBool(value))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a BoolT value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.BoolT(f.BoolTFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if !value {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, strconv.FormatBool(value))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a String value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.String(f.StringFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value != "" {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a int value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.Int(f.IntFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value > 0 {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, strconv.FormatInt(int64(value), 10))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.Duration(f.DurationFlag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value > 0 {
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, value.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
2015-12-09 17:15:46 +00:00
|
|
|
func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
2015-12-06 23:43:18 +00:00
|
|
|
if f.set != nil {
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
2015-12-09 17:15:46 +00:00
|
|
|
value, err := isc.Float64(f.Float64Flag.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-06 23:43:18 +00:00
|
|
|
if value > 0 {
|
|
|
|
floatStr := float64ToString(value)
|
|
|
|
eachName(f.Name, func(name string) {
|
|
|
|
f.set.Set(f.Name, floatStr)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 17:15:46 +00:00
|
|
|
return nil
|
2015-12-06 23:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isEnvVarSet(envVars string) bool {
|
|
|
|
for _, envVar := range strings.Split(envVars, ",") {
|
|
|
|
envVar = strings.TrimSpace(envVar)
|
|
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
|
|
// TODO: Can't use this for bools as
|
|
|
|
// set means that it was true or false based on
|
|
|
|
// Bool flag type, should work for other types
|
|
|
|
if len(envVal) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func float64ToString(f float64) string {
|
|
|
|
return fmt.Sprintf("%v", f)
|
|
|
|
}
|
|
|
|
|
|
|
|
func eachName(longName string, fn func(string)) {
|
|
|
|
parts := strings.Split(longName, ",")
|
|
|
|
for _, name := range parts {
|
|
|
|
name = strings.Trim(name, " ")
|
|
|
|
fn(name)
|
|
|
|
}
|
|
|
|
}
|