This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package utils | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// UnsetEnv unsets all envars having prefix and returns a function | ||
// that restores the env. Any newly added envars having prefix are | ||
// also unset by restore. It is idiomatic to use with a defer. | ||
// | ||
// defer UnsetEnv("ACME_")() | ||
// | ||
// Note that modifying the env may have unpredictable results when | ||
// tests are run with t.Parallel. | ||
// NOTE: This is quick n' dirty from memory; write some tests for | ||
// this code. | ||
func UnsetEnv(prefix string) (restore func()) { | ||
before := map[string]string{} | ||
|
||
for _, e := range os.Environ() { | ||
if !strings.HasPrefix(e, prefix) { | ||
continue | ||
} | ||
|
||
parts := strings.SplitN(e, "=", 2) | ||
before[parts[0]] = parts[1] | ||
|
||
os.Unsetenv(parts[0]) | ||
} | ||
|
||
return func() { | ||
after := map[string]string{} | ||
|
||
for _, e := range os.Environ() { | ||
if !strings.HasPrefix(e, prefix) { | ||
continue | ||
} | ||
|
||
parts := strings.SplitN(e, "=", 2) | ||
after[parts[0]] = parts[1] | ||
|
||
// Check if the envar previously existed | ||
v, ok := before[parts[0]] | ||
if !ok { | ||
// This is a newly added envar with prefix, zap it | ||
os.Unsetenv(parts[0]) | ||
continue | ||
} | ||
|
||
if parts[1] != v { | ||
// If the envar value has changed, set it back | ||
os.Setenv(parts[0], v) | ||
} | ||
} | ||
|
||
// Still need to check if there have been any deleted envars | ||
for k, v := range before { | ||
if _, ok := after[k]; !ok { | ||
// k is not present in after, so we set it. | ||
os.Setenv(k, v) | ||
} | ||
} | ||
} | ||
} |