|
|
|
@ -13,18 +13,18 @@ import (
|
|
|
|
|
// allows a value to be set on the existing parsed flags.
|
|
|
|
|
type FlagInputSourceExtension interface {
|
|
|
|
|
cli.Flag
|
|
|
|
|
ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error
|
|
|
|
|
ApplyInputSourceValue(cCtx *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 {
|
|
|
|
|
func ApplyInputSourceValues(cCtx *cli.Context, inputSourceContext InputSourceContext, flags []cli.Flag) error {
|
|
|
|
|
for _, f := range flags {
|
|
|
|
|
inputSourceExtendedFlag, isType := f.(FlagInputSourceExtension)
|
|
|
|
|
if isType {
|
|
|
|
|
err := inputSourceExtendedFlag.ApplyInputSourceValue(context, inputSourceContext)
|
|
|
|
|
err := inputSourceExtendedFlag.ApplyInputSourceValue(cCtx, inputSourceContext)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
@ -38,34 +38,34 @@ func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSource
|
|
|
|
|
// 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
|
|
|
|
|
func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
|
|
|
|
|
return func(context *cli.Context) error {
|
|
|
|
|
return func(cCtx *cli.Context) error {
|
|
|
|
|
inputSource, err := createInputSource()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ApplyInputSourceValues(context, inputSource, flags)
|
|
|
|
|
return ApplyInputSourceValues(cCtx, inputSource, flags)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
|
|
|
|
|
return func(context *cli.Context) error {
|
|
|
|
|
inputSource, err := createInputSource(context)
|
|
|
|
|
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(cCtx *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
|
|
|
|
|
return func(cCtx *cli.Context) error {
|
|
|
|
|
inputSource, err := createInputSource(cCtx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ApplyInputSourceValues(context, inputSource, flags)
|
|
|
|
|
return ApplyInputSourceValues(cCtx, inputSource, flags)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
|
|
|
|
func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *GenericFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
if !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
value, err := isc.Generic(f.GenericFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -82,9 +82,9 @@ func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
|
|
|
|
func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *StringSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
if !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
value, err := isc.StringSlice(f.StringSliceFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -104,9 +104,9 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a IntSlice value if required
|
|
|
|
|
func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
if !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
value, err := isc.IntSlice(f.IntSliceFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -126,9 +126,9 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
|
|
|
|
func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
if !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
|
|
|
|
value, err := isc.Bool(f.BoolFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -144,9 +144,9 @@ func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a String value to the flagSet if required
|
|
|
|
|
func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *StringFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
if !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
value, err := isc.String(f.StringFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -162,9 +162,9 @@ func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSource
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Path value to the flagSet if required
|
|
|
|
|
func (f *PathFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *PathFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
if !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
value, err := isc.String(f.PathFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -190,9 +190,9 @@ func (f *PathFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a int value to the flagSet if required
|
|
|
|
|
func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
if !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
value, err := isc.Int(f.IntFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -208,9 +208,9 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
|
|
|
|
func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
if !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
value, err := isc.Duration(f.DurationFlag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
@ -226,9 +226,9 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
|
|
|
|
func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
func (f *Float64Flag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
|
|
|
|
|
if f.set != nil {
|
|
|
|
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
if !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
|
|
|
|
value, err := isc.Float64(f.Float64Flag.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|