-
Notifications
You must be signed in to change notification settings - Fork 886
Pass process environment variables by reference #4702
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -152,7 +152,6 @@ func runCmd(c *cobra.Command, args []string, iopts runInputOptions) error { | |
| CNIConfigDir: iopts.CNIConfigDir, | ||
| AddCapabilities: iopts.capAdd, | ||
| DropCapabilities: iopts.capDrop, | ||
| Env: iopts.env, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The line below can be just used here, no reason to break it out. |
||
| WorkingDir: iopts.workingDir, | ||
| } | ||
|
|
||
|
|
@@ -164,6 +163,8 @@ func runCmd(c *cobra.Command, args []string, iopts runInputOptions) error { | |
| } | ||
| } | ||
|
|
||
| options.Env = buildahcli.LookupEnvVarReferences(iopts.env, os.Environ()) | ||
|
|
||
| systemContext, err := parse.SystemContextFromOptions(c) | ||
| if err != nil { | ||
| return fmt.Errorf("building system context: %w", err) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -484,3 +484,42 @@ func AliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { | |
| } | ||
| return pflag.NormalizedName(name) | ||
| } | ||
|
|
||
| // LookupEnvVarReferences returns a copy of specs with keys and values resolved | ||
| // from environ. Strings are in "key=value" form, the same as [os.Environ]. | ||
| // | ||
| // - When a string in specs lacks "=", it is treated as a key and the value | ||
| // is retrieved from environ. When the key is missing from environ, neither | ||
| // the key nor value are returned. | ||
| // | ||
| // - When a string in specs lacks "=" and ends with "*", it is treated as | ||
| // a key prefix and any keys with the same prefix in environ are returned. | ||
| // | ||
| // - When a string in specs is exactly "*", all keys and values in environ | ||
| // are returned. | ||
| func LookupEnvVarReferences(specs, environ []string) []string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you planning on making podman use this function as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no plans for that. It would be nice for Buildah and Podman to do these things consistently, but the tools satisfy different use cases. I encountered difficulty making env flags consistent within Buildah. |
||
| result := make([]string, 0, len(specs)) | ||
|
|
||
| for _, spec := range specs { | ||
| if key, _, ok := strings.Cut(spec, "="); ok { | ||
| result = append(result, spec) | ||
|
|
||
| } else if key == "*" { | ||
| result = append(result, environ...) | ||
|
|
||
| } else { | ||
| prefix := key + "=" | ||
| if strings.HasSuffix(key, "*") { | ||
| prefix = strings.TrimSuffix(key, "*") | ||
| } | ||
|
|
||
| for _, spec := range environ { | ||
| if strings.HasPrefix(spec, prefix) { | ||
| result = append(result, spec) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised this isn't being used elsewhere and that it's not complaining loudly...