Thanks to visit codestin.com
Credit goes to github.com

Skip to content

altsrc with YAML: Flag is Never Assigned #1250

@sshaw

Description

@sshaw

How to use a YAML config file to set flags? I've read the docs and tried a couple of things; then waited a month and read them again but, nothing seems to work.

I'm using v2.3.0.

package cli

var Flags []cli.Flag

func init() {
        Flags = []cli.Flag{
                altsrc.NewStringFlag(
                        &cli.StringFlag{
                                Required: true,
                                Name:    "foo",
                        }
                )
        }
}
package main

// In main()
app := &cli.App{
        Name: "tester",
        Before: altsrc.InitInputSourceWithContext(cmd.Flags,
                func(context *cli.Context) (altsrc.InputSourceContext, error) {
                        return altsrc.NewYamlSourceFromFile(".myconfig")
                }
        )
}

But every time I run it I get an error because foo is required and not set.

I have also tried what sounds like a more verbose version of the above:

app := &cli.App{
        Name: "sdt",
        Before: func (c *cli.Context) error {
                src, err := altsrc.NewYamlSourceFromFile(".myconfig")
                if err != nil {
                        return err
                }

                fmt.Printf("%+v\n", src)

                err = altsrc.ApplyInputSourceValues(c, src, cmd.Flags)
                if err != nil {
                        return err
                }

                return nil
        },
}

The config is printed and everything is there but the flag is never assigned.

I thought it may be related to #1086 but this appears to be fixed in v2.3.0. Help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v2relates to / is being considered for v2kind/questionsomeone asking a questionstatus/triagemaintainers still need to look into thisstatus/waiting-for-responseWaiting for response from original requester

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions