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

Lookup of values from JSON/TOML/YAML doesn't work for root properties - only at least one level nested one #14

Open
lukasbindreiter opened this issue Jan 2, 2025 · 0 comments

Comments

@lukasbindreiter
Copy link

File sources Lookup, or more specifically the nestedVal func in altsrc.go only works if values are nested at least one level deep.

For example, assume the following TOML file:

# config.toml

host = "localhost"

[database]
url = "example.com"

trying to use it like this only works for database.url, but not the host:

cmd := &cli.Command{
	Name: "Example",
	Action: func(ctx context.Context, cmd *cli.Command) error {
		fmt.Println("Host:")
		fmt.Println(cmd.String("host"))
		fmt.Println("Database URL:")
		fmt.Println(cmd.String("database-url"))
		return nil
	},
	EnableShellCompletion: true,
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "host",
			Sources: altsrc.TOML("host", "config.toml"),
		},
		&cli.StringFlag{
			Name:    "database-url",
			Sources: altsrc.TOML("database.url", "config.toml"),
		},
	},
}

cmd.Run(context.Background(), os.Args)

will print the following (host is empty)

Host:

Database URL:
example.com

The workaround for now would be to wrap all root elements inside of a dummy container, e.g. app.host instead of just host.

But of course it would be better if this could be adressed directly by the altsrc library.

Either by not using nestedVal here in case there is no dot in the key:
https://github.com/urfave/cli-altsrc/blob/main/toml/toml_value_source.go#L40

or by updating it to also support the root level:
https://github.com/urfave/cli-altsrc/blob/main/altsrc.go#L48

Is this just a bug - or is there a reason why the root level is skipped on purpose?
In case of the former I could probably try to come up with a PR as well 😄

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

1 participant