urfave / cli

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

Custom Environment Variables Setup #1818

Closed kudla closed 9 months ago

kudla commented 12 months ago

Hi Similar to how command args are provided controlled into Run call how to inject custom environment setup instead of using OS provided?

Thx.

dearchap commented 11 months ago

@kudla not sure I understand. What exactly do you want to do ? You can define flag values in a file and have urfave/cli read from there. You can set the ZZZFlag.FilePath

kudla commented 11 months ago
    &cli.StringFlag{
        Name:    "some-flag",
        EnvVars: []string{"FLAG_ENV"},
    },

Similar to the way we explicitly provide args into the app

app.Run([]string{"command", "--some-flag", "some-value"})

Could we inject into the app env variables set to be parsed (e.g. FLAG_ENV=some-value) instead of implicit read form os.Environ

I.e. could app be ran in kind of virtual sandbox (args + envs) regardless OS level setup for the process.

dearchap commented 10 months ago

@kudla Yes you can use values from files https://cli.urfave.org/v2/examples/flags/#values-from-files . If you dont want to pass them through the outer sandbox environment but still pass them to the flag your app can do os.Setenv(name, value) and let the flag pick it up that way