Allow the use of environment variables to supply provider and ide options not supplied on the command line#256
Conversation
fd9d18d to
dbec5cd
Compare
…ons not supplied explicitly Signed-off-by: Leonid Dubinsky <dub@podval.org>
dbec5cd to
e63fdf5
Compare
There was a problem hiding this comment.
Pull request overview
This PR enables the use of environment variables to supply provider options that are not provided via command-line arguments. It introduces a reusable utility function for merging environment variables with command-line options, refactors existing Git environment variable propagation to use this utility, and extends environment variable support to provider options.
Key Changes:
- New utility function for assigning environment variables to unassigned options
- Refactored Git environment variable handling to use the new utility
- Provider options can now be supplied via environment variables with the pattern
DEVPOD_PROVIDER_<PROVIDER_NAME>_<OPTION_NAME>
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| pkg/options/options.go | New utility package with AssignUnassignedFromEnvironment function for merging environment variables with option assignments |
| cmd/up.go | Refactored to use new utility function for Git environment variable propagation; renamed variable from gitEnvironmentVariables to propagatedEnvironmentVariables |
| cmd/provider/use.go | Added environment variable support for provider options using the new utility function with provider-specific prefix |
| cmd/provider/set_options.go | Minor parameter renaming to avoid shadowing the flags package |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/options/options.go
Outdated
| func AssignUnassignedFromEnvironment( | ||
| assignments []string, | ||
| names []string, | ||
| environmentVariablePrefix string, | ||
| ) []string { | ||
| var result = assignments | ||
| for _, name := range names { | ||
| if value, exists := os.LookupEnv(environmentVariablePrefix + name); exists && !isAssigned(assignments, name) { | ||
| result = append(result, name+"="+value) | ||
| } | ||
| } | ||
| return result | ||
| } |
There was a problem hiding this comment.
The new function AssignUnassignedFromEnvironment lacks test coverage. Since the pkg/options package has comprehensive testing (as seen in resolve_test.go), this new functionality should also have tests to verify: 1) environment variables are correctly added for unassigned options, 2) already assigned options are not overridden by environment variables, 3) the environment variable prefix is correctly applied, and 4) edge cases like empty values or missing environment variables are handled properly.
pkg/options/options.go
Outdated
| func AssignUnassignedFromEnvironment( | ||
| assignments []string, | ||
| names []string, | ||
| environmentVariablePrefix string, | ||
| ) []string { |
There was a problem hiding this comment.
The exported function AssignUnassignedFromEnvironment is missing documentation. According to Go conventions, all exported functions should have a comment describing what they do. Add a comment describing that this function takes a list of assignments in KEY=VALUE format, a list of option names to check, and an environment variable prefix, and returns a new list with additional assignments from environment variables for any names not already assigned.
f203cbb to
7d0866b
Compare
| var gitEnvironmentVariables = [...]string{ | ||
| var propagatedEnvironmentVariables = []string{ |
There was a problem hiding this comment.
for generality: I am sure there are other variables that should be propagated, not just git-related...
pkg/options/options.go
Outdated
| "strings" | ||
| ) | ||
|
|
||
| func AssignUnassignedFromEnvironment( |
There was a problem hiding this comment.
Do you find the function name somewhat confusing? Based on the usage, I was thinking something along the lines of GetWorkspaceEnvironment, PropagateFromEnvironment, or InheritFromEnvironment.
There was a problem hiding this comment.
The function is used in up.go to augment the environment, but in provider/use.go - to set the options from the environment... I think PropagateFromEnvironment would do it; thanks.
pkg/options/options.go
Outdated
| var result = assignments | ||
| for _, name := range names { | ||
| if value, exists := os.LookupEnv(environmentVariablePrefix + name); exists && !isAssigned(assignments, name) { | ||
| result = append(result, name+"="+value) | ||
| } | ||
| } | ||
| return result |
There was a problem hiding this comment.
What are your thoughts on reducing the complexity from O(n x m) to O(n + m).
func PropagateFromEnvironment(
assignments []string,
names []string,
prefix string,
) []string {
assigned := assignedNames(assignments)
result := assignments
for _, name := range names {
if assigned[name] {
continue
}
if value, exists := os.LookupEnv(prefix + name); exists {
result = append(result, name+"="+value)
}
}
return result
}
func assignedNames(assignments []string) map[string]bool {
names := make(map[string]bool, len(assignments))
for _, assignment := range assignments {
if idx := strings.Index(assignment, "="); idx != -1 {
names[assignment[:idx]] = true
}
}
return names
}There was a problem hiding this comment.
Great! Stealing your code :) Thank you!
Signed-off-by: Leonid Dubinsky <dub@podval.org>
7d0866b to
73f238b
Compare
…ot supplied explicitly Signed-off-by: Leonid Dubinsky <dub@podval.org>
|
@skevetter sorry, I couldn't resist adding the environment variables support for |
|
Thank you!! |
This is the last piece of the environment variables puzzle: with this pull request, all the CLI options that it makes sense to me to supply via environment variables can be, including provider and ide options :)