Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

altsrc newYamlSourceFromFlagFunc example not working. #570

Closed
jameskyle opened this issue Nov 22, 2016 · 4 comments
Closed

altsrc newYamlSourceFromFlagFunc example not working. #570

jameskyle opened this issue Nov 22, 2016 · 4 comments

Comments

@jameskyle
Copy link

If I understand the purpose of the altsrc lib, it should use the passed in config file to populate flag parameters. I took the example for altsrc, fixed the gopkg.in compile errors, and passed in a yaml that looks like:

test: 10

Then compiled with:

go main.go --load file.yaml

But the flag is never set. I modified the Action func to look like:

fmt.Println("yaml ist rad")
fmt.Println(c.Int("test"))

If I pass it on the cli, it is set. e.g.

go main.go --test 10
@jszwedko
Copy link
Contributor

Hi @jameskyle,

I'm having trouble reproducing this with the following:

package main

import (
        "fmt"
        "os"

        "gopkg.in/urfave/cli.v1"
        "gopkg.in/urfave/cli.v1/altsrc"
)

func main() {
        app := cli.NewApp()

        flags := []cli.Flag{
                altsrc.NewIntFlag(cli.IntFlag{Name: "test"}),
                cli.StringFlag{Name: "load"},
        }

        app.Action = func(c *cli.Context) error {
                fmt.Println(c.Int("test"))
                return nil
        }

        app.Before = altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("load"))
        app.Flags = flags

        app.Run(os.Args)
}

Would you be able to provide the source of main.go?

@iiezhachenko
Copy link

Here is my main.go. Flags values are not retrieved from YAML.

package main

import (
	"gopkg.in/urfave/cli.v2"
	"gopkg.in/urfave/cli.v2/altsrc"
	"os"

	"path/filepath"
	"fmt"
)

const VERSION string = "0.1.0"

func main() {
	path := filepath.Join(os.Getenv("HOME"), ".cloudflare-cli")
	os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0666)

	app := &cli.App{}
	app.Name = "cloudflare-cli"
	app.Usage = "Command line interface for CloudFlare services"
	app.Version = VERSION

	globalFlags := []cli.Flag{
		altsrc.NewBoolFlag(&cli.BoolFlag{
			Name:  "verbose",
			Usage: "Display additional output",
		}),
		altsrc.NewStringFlag(&cli.StringFlag {
			Name:    "cf-api-key",
			Aliases: []string{"k"},
			Usage:   "CloudFlare API `KEY`",
		}),
		&cli.StringFlag{
			Name: "config-file",
			Aliases: []string{"c"},
			Value: filepath.Join(os.Getenv("HOME"), ".cloudflare-cli"),
			Usage: "Application config file `PATH`",
		},
	}

	generateConfigFlags := []cli.Flag{
		&cli.BoolFlag {
			Name: "dry-run, d",
			Usage: "Print YAML to STDOUT instead of writing to file",
		},
	}

	app.Flags = globalFlags

	generateConfigCmd := cli.Command{
		Name: "generate_config",
		Usage: "Generate application config file",
		Flags: append(app.Flags, generateConfigFlags...),
		ArgsUsage: " ",
		Action: func(c *cli.Context) error {
			fmt.Printf("%v\n", c.FlagNames())
			fmt.Printf("%v\n", c.App.VisibleFlags())
			return nil
		},
	}

	app.Commands = []*cli.Command{&generateConfigCmd}

	var beforeFunc cli.BeforeFunc = func(c *cli.Context) error {
		altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("config-file"))
		return nil
	}

	app.Before = beforeFunc
	app.Run(os.Args)
}
---
cf-api-key: "key_from_config_file"
verbose: true

Output:

[dry-run]
[--verbose      Display additional output (default: false) --cf-api-key KEY, -k KEY     CloudFlare API KEY --config-file PATH, -c PATH  Application config file PATH (default: "/Users/ievgen/.cloudflare-cli") --help, -h       show help (default: false) --version, -v        print the version (default: false)]

@jszwedko
Copy link
Contributor

Hi @iiezhachenko,

Apologies for the delay.

altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("config-file"))

Actually returns a function to be called so you would need to call it in your function with something like:

return altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("config-file"))()

The typical usage of altsrc.InitInputSourceWithContext is something like:

app.Before = altsrc.InitInputSourceWithContext(globalFlags, altsrc.NewYamlSourceFromFlagFunc("config-file"))

Let me know if this helps!

@coilysiren
Copy link
Member

Hiya! There's a known issue with the current implementation of the CLI, where altsrc is generally clunky and poorly documented. My current idea is that I'm going to move all of the altsrc functionality into the main package in some way, and that's how all of the "altsrc is weird and different???" issues people are having will get resolved. I'm tracking that work here => #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants