urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.9k stars 1.69k forks source link

[v3] how to chain multible Sources? #1842

Closed 6543 closed 6 months ago

6543 commented 6 months ago

I did expect something like:

cli.StringFlag{
        Sources: EnvVars("APP_LANG").Or(Files("/path/to/foo")),
}

or

cli.StringFlag{
        Sources: EnvVars("APP_LANG").Chain(Files("/path/to/foo")),
}

as this is not practicable:

    &cli.StringFlag{
        Name:    "grpc-token",
        Usage:   "server-agent shared token",
        Sources: cli.ValueSourceChain{Chain: append(cli.Files(os.Getenv("WOODPECKER_AGENT_SECRET_FILE")).Chain,cli.EnvVars("WOODPECKER_AGENT_SECRET").Chain...)} ,
    },

or did I miss something ?

dearchap commented 6 months ago
&cli.StringFlag{
        Name:    "grpc-token",
        Usage:   "server-agent shared token",
        Sources: cli.ValueSourceChain{Chain: {cli.EnvVars("...."), cli.FileVars("...")},
6543 commented 6 months ago

hmm in this regards could we make this issue a feature request to add an function to .Chain() Sources ?

6543 commented 6 months ago

if you move it behind an interface you can refactor things more easy ... e.g. the internal Chain field don't have to be exported ...

or what take do you have on this matter?

6543 commented 6 months ago
&cli.StringFlag{
      Name:    "grpc-token",
      Usage:   "server-agent shared token",
      Sources: cli.ValueSourceChain{Chain: {cli.EnvVars("...."), cli.FileVars("...")},

this does not work as SourceChain gen func to return slices ... witch need to be append to return a slice else you get a slice of slices :/

6543 commented 6 months ago
&cli.StringFlag{
      Name:    "grpc-token",
      Usage:   "server-agent shared token",
      Sources: cli.ValueSourceChain{Chain: []cli.ValueSource{cli.Files("...").Chain[0], cli.EnvVars("...").Chain[0]}},

this works but I would call it an hack!

6543 commented 6 months ago

would be a pull welcome to address this?

dearchap commented 6 months ago

@6543 Sure that would be fine.

dearchap commented 6 months ago

@6543 This PR fix should allow you to do

&cli.StringFlag{
        Name:    "grpc-token",
        Usage:   "server-agent shared token",
        Sources: cli.ValueSourceChain{Chain: []cli.ValueSource{cli.Files("..."), cli.EnvVars("...")}},
dearchap commented 6 months ago

@6543 I had to withdraw the PR to think a bit more about this

6543 commented 6 months ago

I might have time and submit some pull just as draft to get some ideas going :)

6543 commented 6 months ago

thanks :)