feat(cli/environment): use environment variables to set all flags not supplied explicitly#257
Conversation
969caad to
1c2e870
Compare
… supplied explicitly Tighter integration with the `spf13/pflag` and `spf13/cobra` libraries allows for a better approach to setting default values of the CLI flags from the environment variables: - this defaulting is done in one place (`root.go`); - all the code enabling defaulting of specific flags is reverted; - all the flags of all the commands can now be defaulted using environment variables; - names of the environment variables are calculated from the names of the flags in a consistent way; - there is no need to duplicate `pflag` code for the parsing of the non-string flags. Signed-off-by: Leonid Dubinsky <dub@podval.org>
1c2e870 to
a092b8f
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the CLI's environment variable handling for flags by centralizing the logic in a single location. Instead of manually specifying environment variables for each flag using custom wrapper functions, the new approach automatically generates environment variable names from flag names and applies them uniformly across all commands.
Key Changes
- Centralized environment variable handling in
cmd/root.gowith newinheritCommandFlagsFromEnvironmentandinheritFlagsFromEnvironmentfunctions - Removed custom flag wrapper functions (
StringVarE,BoolVarE,StringArrayVarE,StringSliceVarE) fromcmd/flags/flags.go - Renamed functions from
Propagate*toInherit*for better semantic clarity - Updated all flag definitions across commands to use standard
cobra/pflagmethods
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| cmd/root.go | Added centralized environment variable inheritance logic that automatically maps all flags to environment variables with consistent naming |
| cmd/flags/flags.go | Removed custom flag wrapper functions and environment variable helper functions; simplified to use standard pflag methods |
| cmd/up.go | Converted flag definitions from custom *VarE functions to standard pflag methods; renamed propagatedEnvironmentVariables to inheritedEnvironmentVariables |
| cmd/provider/use.go | Converted flag definitions to standard methods; hardcoded environment variable prefix from constant to literal string |
| cmd/provider/set_options.go | Converted flag definition to standard method |
| cmd/provider/add.go | Converted flag definition to standard method |
| cmd/ide/use.go | Updated function call and hardcoded environment variable prefix |
| pkg/options/options.go | Renamed PropagateFromEnvironment and PropagateOptionsFromEnvironment to InheritFromEnvironment and InheritOptionsFromEnvironment |
| pkg/options/options_test.go | Updated test function name to match the renamed function |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // do not prepend "DEVPOD_" to the environment variable name if the flag name starts with "devpod" | ||
| // (applies to one flag - "devpod-home"). |
There was a problem hiding this comment.
should we made a TODO to update devpod-home to be consistent with the other variables?
There was a problem hiding this comment.
Change it to home? I think it is ok as is - home is too generic...
There was a problem hiding this comment.
Besides, breaking people who use devpod-home flag is not worth it I think...
|
Thank you! |
Tighter integration with the
spf13/pflagandspf13/cobralibraries allows for a better approach to setting default values of the CLI flags from the environment variables:root.go);pflagcode for the parsing of the non-string flags.