Documentation ¶
Overview ¶
Package env provides conversion from structure to and from environment variables.
Supports converting struct fields to environment variables using field tags, handling most data types. Provides functions to serialize structs into slices of key-value pairs where the keys are derived from struct field names transformed to upper snake case by default, or specified explicitly via struct field tags.
Includes functionality to deserialize environment variables back into struct fields, handling pointers and nested structs appropriately, as well as providing shell-compatible output for environment variable definitions.
Incidentally the package also defines several case conversion functions that aid in manipulating which is useful for generating or parsing environment variables, JSON tags, or command line flags style of naming (camelCase, UPPER_SNAKE_CASE, lower-kebab-case ...)
The package leverages reflection to dynamically handle arbitrary struct types, and has 0 dependencies.
Index ¶
- func CamelCaseToLowerKebabCase(s string) string
- func CamelCaseToLowerSnakeCase(s string) string
- func CamelCaseToUpperSnakeCase(s string) string
- func SerializeValue(result *KeyValue, value interface{}) error
- func SetFrom(envLookup EnvLookup, prefix string, s interface{}) []error
- func SetFromEnv(prefix string, s interface{}) []error
- func ShellQuote(input string) (string, error)
- func SplitByCase(input string) []string
- func ToShell(kvl []KeyValue) string
- func ToShellWithPrefix(prefix string, kvl []KeyValue, skipExport bool) string
- func ToYamlWithPrefix(indent int, prefix string, kvl []KeyValue) string
- func YamlQuote(input string) string
- type EnvLookup
- type KeyValue
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CamelCaseToLowerKebabCase ¶
CamelCaseToLowerKebabCase converts a string from camelCase or CamelCase to lower-kebab-case. Handles cases like HTTPServer -> http-server. Good for command line flags for instance.
func CamelCaseToLowerSnakeCase ¶
CamelCaseToLowerSnakeCase converts a string from camelCase or CamelCase to lower_snake_case. Handles cases like HTTPServer -> http_server. Good for JSON tags for instance.
func CamelCaseToUpperSnakeCase ¶
CamelCaseToUpperSnakeCase converts a string from camelCase or CamelCase to UPPER_SNAKE_CASE. Handles cases like HTTPServer -> HTTP_SERVER and httpServer -> HTTP_SERVER. Good for environment variables.
func SerializeValue ¶
func SetFrom ¶ added in v0.2.0
Reverse of StructToEnvVars, assumes the same encoding. Using passed it lookup object that can lookup values by keys.
func SetFromEnv ¶
Reverse of StructToEnvVars, assumes the same encoding. Using the current os environment variables as source.
func ShellQuote ¶ added in v0.1.1
Escape characters such as the result string can be embedded as a single argument in a shell fragment e.g for ENV_VAR=<value> such as <value> is safe (no $(cmd...) no ` etc`). Will error out if NUL is found in the input (use []byte for that and it'll get base64 encoded/decoded).
func SplitByCase ¶
Split strings into words, using CamelCase/camelCase/CAMELCase rules.
func ToShellWithPrefix ¶
This convert the key value pairs to bourne shell syntax (vs newer bash export FOO=bar). If skipExport is true the last line export VAR1 VAR2... is omitted.
func ToYamlWithPrefix ¶ added in v0.3.0
Types ¶
type KeyValue ¶
type KeyValue struct { Key string // Must be safe (is when coming from Go struct names but could be bad with env:). ShellQuotedVal string // (Must be) Already quoted/escaped (” style). YamlQuotedVal string // (Must be) Already quoted/escaped for yaml ("" with \ style). }
Intermediate result list from StructToEnvVars(), both the Key and QuotedValue must be shell safe/non adversarial as they are emitted as is by String() with = in between. Using StructToEnvVars produces safe values even with adversarial input (length and future reoccurence of bugs like https://en.wikipedia.org/wiki/Shellshock_(software_bug) notwithstanding) So avoid or scrub external values if possible (or use []byte type which base64 encodes the values).
func StructToEnvVars ¶
StructToEnvVars converts a struct to a map of environment variables. The struct can have a `env` tag on each field. The tag should be in the format `env:"ENV_VAR_NAME"`. The tag can also be `env:"-"` to exclude the field from the map. If the field is exportable and the tag is missing we'll use the field name converted to UPPER_SNAKE_CASE (using CamelCaseToUpperSnakeCase()) as the environment variable name. []byte are encoded as base64, time.Time are formatted as RFC3339, time.Duration are in (floating point) seconds.