generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from dogmatiq/reusable-registry
Reusable registries
- Loading branch information
Showing
54 changed files
with
806 additions
and
488 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
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,3 @@ | ||
// Package environment is an abstraction that Ferrite uses to interact with the | ||
// environment. | ||
package environment |
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,13 @@ | ||
package environment | ||
|
||
// EqualNames returns true if the given names refer to the same environment | ||
// variable. | ||
func EqualNames(a, b string) bool { | ||
return normalizeName(a) == normalizeName(b) | ||
} | ||
|
||
// NormalizeName normalizes an environment variable name for use as a key within | ||
// a map. | ||
func NormalizeName(n string) string { | ||
return normalizeName(n) | ||
} |
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,11 @@ | ||
//go:build windows | ||
|
||
package environment | ||
|
||
import "strings" | ||
|
||
// normalizeName normalizes an environment variable name, such that it compares | ||
// as equals to other variables with the same name. | ||
func normalizeName(n string) string { | ||
return strings.ToUpper(n) | ||
} |
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,9 @@ | ||
//go:build !windows | ||
|
||
package environment | ||
|
||
// normalizeName normalizes an environment variable name, such that it compares | ||
// as equals to other variables with the same name. | ||
func normalizeName(n string) string { | ||
return n | ||
} |
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,43 @@ | ||
package environment | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// Get returns the value of the environment variable with the given name. | ||
func Get(n string) string { | ||
return os.Getenv(n) | ||
} | ||
|
||
// Set sets the value of the environment variable with the given name, or panics | ||
// if unable to do so. | ||
func Set(n, v string) { | ||
if err := os.Setenv(n, v); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Unset unsets the environment variable with the given name, or panics if | ||
// unable to do so. | ||
func Unset(n string) { | ||
if err := os.Unsetenv(n); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// Range calls fn for each environment variable. | ||
// | ||
// It stops iterating if fn returns false. It returns true if iteration | ||
// reached the end. | ||
func Range(fn func(n, v string) bool) { | ||
for _, env := range os.Environ() { | ||
i := strings.IndexByte(env, '=') | ||
n := env[:i] | ||
v := env[i+1:] | ||
|
||
if !fn(n, v) { | ||
return | ||
} | ||
} | ||
} |
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,35 @@ | ||
package environment | ||
|
||
// Snapshot is a snapshot of the environment. | ||
type Snapshot struct { | ||
variables []variable | ||
} | ||
|
||
type variable struct { | ||
Name, Value string | ||
} | ||
|
||
// TakeSnapshot takes a snapshot of the variables within the environment. | ||
func TakeSnapshot() *Snapshot { | ||
s := &Snapshot{} | ||
|
||
Range(func(name, value string) bool { | ||
s.variables = append(s.variables, variable{name, value}) | ||
return true | ||
}) | ||
|
||
return s | ||
} | ||
|
||
// RestoreSnapshot restores the environment to the state it was in when the | ||
// given snapshot was taken. | ||
func RestoreSnapshot(s *Snapshot) { | ||
Range(func(name, _ string) bool { | ||
Unset(name) | ||
return true | ||
}) | ||
|
||
for _, v := range s.variables { | ||
Set(v.Name, v.Value) | ||
} | ||
} |
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
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
Oops, something went wrong.