Documentation ¶
Overview ¶
Package env contains environment variable helpers, enabling better tests and easier use of environment variables.
Index ¶
- Constants
- func Clear()
- func IsDev(serviceEnv string) bool
- func IsDevlike(serviceEnv string) bool
- func IsProdlike(serviceEnv string) bool
- func IsProduction(serviceEnv string) bool
- func Restore()
- func SetEnv(vars Vars)
- func Split(s string) (key, value string)
- func WithVars(ctx context.Context, vars Vars) context.Context
- type Option
- type PairDelimiter
- type Unmarshaler
- type Vars
- func (ev Vars) Base64(envVar string, defaults ...[]byte) ([]byte, error)
- func (ev Vars) Bool(envVar string, defaults ...bool) bool
- func (ev Vars) Bytes(envVar string, defaults ...[]byte) []byte
- func (ev Vars) CSV(envVar string, defaults ...string) []string
- func (ev Vars) Delete(key string)
- func (ev Vars) DelimitedString(separator PairDelimiter) string
- func (ev Vars) Duration(envVar string, defaults ...time.Duration) (time.Duration, error)
- func (ev Vars) Expand(value string) string
- func (ev Vars) Float32(envVar string, defaults ...float32) (float32, error)
- func (ev Vars) Float64(envVar string, defaults ...float64) (float64, error)
- func (ev Vars) Get(envVar string) string
- func (ev Vars) Has(envVar string) bool
- func (ev Vars) HasAll(envVars ...string) bool
- func (ev Vars) HasAny(envVars ...string) bool
- func (ev Vars) Hostname(defaults ...string) string
- func (ev Vars) Int(envVar string, defaults ...int) (int, error)
- func (ev Vars) Int32(envVar string, defaults ...int32) (int32, error)
- func (ev Vars) Int64(envVar string, defaults ...int64) (int64, error)
- func (ev Vars) IsDev() bool
- func (ev Vars) IsDevlike() bool
- func (ev Vars) IsProdlike() bool
- func (ev Vars) IsProduction() bool
- func (ev Vars) Must(keys ...string)
- func (ev Vars) MustBase64(envVar string, defaults ...[]byte) []byte
- func (ev Vars) MustDuration(envVar string, defaults ...time.Duration) time.Duration
- func (ev Vars) MustFloat32(envVar string, defaults ...float32) float32
- func (ev Vars) MustFloat64(envVar string, defaults ...float64) float64
- func (ev Vars) MustInt(envVar string, defaults ...int) int
- func (ev Vars) MustInt32(envVar string, defaults ...int32) int32
- func (ev Vars) MustInt64(envVar string, defaults ...int64) int64
- func (ev Vars) MustUint32(envVar string, defaults ...uint32) uint32
- func (ev Vars) MustUint64(envVar string, defaults ...uint64) uint64
- func (ev Vars) Raw() []string
- func (ev Vars) ReadInto(obj interface{}) error
- func (ev Vars) Require(keys ...string) error
- func (ev Vars) Restore(key string)
- func (ev Vars) ServiceEnv(defaults ...string) string
- func (ev Vars) ServiceName(defaults ...string) string
- func (ev Vars) Set(envVar, value string)
- func (ev Vars) String(envVar string, defaults ...string) string
- func (ev Vars) Uint32(envVar string, defaults ...uint32) (uint32, error)
- func (ev Vars) Uint64(envVar string, defaults ...uint64) (uint64, error)
- func (ev Vars) Union(other Vars) Vars
- func (ev Vars) Vars() []string
- func (ev Vars) Version(defaults ...string) string
Constants ¶
const ( // VarServiceEnv is a common env var name. VarServiceEnv = "SERVICE_ENV" // VarServiceName is a common env var name. VarServiceName = "SERVICE_NAME" // VarProjectName is a common env var name. VarProjectName = "PROJECT_NAME" // VarServiceSecret is a common env var name. VarServiceSecret = "SERVICE_SECRET" // VarPort is a common env var name. VarPort = "PORT" // VarHostname is a common env var name. VarHostname = "HOSTNAME" // VarVersion is a common env var name. VarVersion = "VERSION" // VarGitRef is a common env var name. VarGitRef = "GIT_REF" // VarSecurePort is a common env var name. VarSecurePort = "SECURE_PORT" // VarTLSCertPath is a common env var name. VarTLSCertPath = "TLS_CERT_PATH" // VarTLSKeyPath is a common env var name. VarTLSKeyPath = "TLS_KEY_PATH" // VarTLSCert is a common env var name. VarTLSCert = "TLS_CERT" // VarTLSKey is a common env var name. VarTLSKey = "TLS_KEY" // VarPGIdleConns is a common env var name. VarPGIdleConns = "PG_IDLE_CONNS" // VarPGMaxConns is a common env var name. VarPGMaxConns = "PG_MAX_CONNS" // ServiceEnvTest is a service environment. ServiceEnvTest = "test" // ServiceEnvSandbox is a service environment. ServiceEnvSandbox = "sandbox" // ServiceEnvDev is a service environment. ServiceEnvDev = "dev" // ServiceEnvCI is a service environment. ServiceEnvCI = "ci" // ServiceEnvPreprod is a service environment. ServiceEnvPreprod = "preprod" // ServiceEnvBeta is a service environment. ServiceEnvBeta = "beta" // ServiceEnvProd is a service environment. ServiceEnvProd = "prod" // DefaultServiceEnv is the default service env to use for configs. DefaultServiceEnv = ServiceEnvDev // TagName is the reflection tag name. ReflectTagName = "env" )
Service specific constants
Variables ¶
This section is empty.
Functions ¶
func IsDevlike ¶ added in v1.20201204.1
IsDevlike returns if the environment is development. It is strictly the inverse of `IsProdlike`.
func IsProdlike ¶ added in v1.20201204.1
IsProdlike returns if the environment is prodlike.
func IsProduction ¶ added in v1.20201204.1
IsProduction returns if the environment is production.
Types ¶
type Option ¶ added in v1.20201204.1
type Option func(Vars)
Option is a mutator for the options set.
func OptEnviron ¶ added in v1.20210116.3
OptEnviron parses the output of `os.Environ()`
type PairDelimiter ¶ added in v1.20201204.1
type PairDelimiter = rune
PairDelimiter is a type of delimiter that separates different env var key-value pairs
const ( // PairDelimiterSemicolon (";") is a delimiter between key-value pairs PairDelimiterSemicolon PairDelimiter = ';' // PairDelimiterComma (",") is a delimiter betewen key-value pairs PairDelimiterComma PairDelimiter = ',' )
type Unmarshaler ¶
Unmarshaler is a type that implements `UnmarshalEnv`.
type Vars ¶
Vars is a set of environment variables.
func New ¶ added in v1.20201204.1
New returns a new env var set.
By default, it is empty. In order to populate it with the current
runtime environment variables, you need to pass in options:
vars := env.New(env.OptEnviron(os.Environ()...))
func Parse ¶ added in v1.20201204.1
Parse uses a state machine to parse an input string into the `Vars` type. It uses a default pair delimiter of ';'.
func ParsePairDelimiter ¶ added in v1.20201204.1
func ParsePairDelimiter(s string, pairDelimiter PairDelimiter) (Vars, error)
ParsePairDelimiter uses a state machine to parse an input string into the `Vars` type. The user can choose which delimiter to use between key-value pairs.
An example of this format:
ENV_VAR_1=VALUE_1;ENV_VAR_2=VALUE_2;
We define the grammar as such (in BNF notation): <expr> ::= (<pair> <sep>)* <pair> <sep> ::= ';'
| ','
<pair> ::= <term> = <term> <term> ::= <literal>
| "[<literal>|<space>|<escape_quote>]*"
<literal> ::= [-A-Za-z_0-9]+ <space> ::= ' ' <escape_quote> ::= '\"'
func (Vars) Base64 ¶
Base64 returns a []byte value for a given key whose value is encoded in base64.
func (Vars) Bool ¶
Bool returns a boolean value for a key, defaulting to false. Valid "truthy" values are `true`, `yes`, and `1`. Everything else is false, including `REEEEEEEEEEEEEEE`.
func (Vars) DelimitedString ¶ added in v1.20201204.1
func (ev Vars) DelimitedString(separator PairDelimiter) string
DelimitedString converts environment variables to a particular string representation, allowing the user to specify which delimiter to use between different environment variable pairs.
func (Vars) Expand ¶ added in v1.20201204.1
Expand calls os.Expand with the variable set as the environment value resolver.
func (Vars) Hostname ¶ added in v1.20210103.1
Hostname is a common environment variable for the machine's hostname.
func (Vars) IsDev ¶ added in v1.20201204.1
IsDev returns if the ServiceEnv is the local development environment.
func (Vars) IsDevlike ¶ added in v1.20201204.1
IsDevlike returns if the ServiceEnv is strictly the inverse of `IsProdlike`.
func (Vars) IsProdlike ¶
IsProdlike returns if the ServiceEnv is "prodlike".
func (Vars) IsProduction ¶
IsProduction returns if the ServiceEnv is a production environment.
func (Vars) Must ¶
Must enforces that a given set of environment variables are present and panics if they're not present.
func (Vars) MustBase64 ¶
MustBase64 returns a []byte value for a given key encoded with base64, and panics if malformed.
func (Vars) MustDuration ¶
MustDuration returnss a duration value for a given key and panics if malformed.
func (Vars) MustFloat32 ¶ added in v1.20201204.1
MustFloat32 returns an float64 value for a given key and panics if it is malformed.
func (Vars) MustFloat64 ¶
MustFloat64 returns an float64 value for a given key and panics if it is malformed.
func (Vars) MustInt ¶
MustInt returns an integer value for a given key and panics if it is malformed.
func (Vars) MustInt32 ¶
MustInt32 returns an integer value for a given key and panics if it is malformed.
func (Vars) MustInt64 ¶
MustInt64 returns an int64 value for a given key and panics if it is malformed.
func (Vars) MustUint32 ¶
MustUint32 returns an uint32 value for a given key and panics if it is malformed.
func (Vars) MustUint64 ¶
MustUint64 returns an uint64 value for a given key and panics if it is malformed.
func (Vars) ServiceEnv ¶
ServiceEnv is a common environment variable for the services environment. Common values include "dev", "ci", "sandbox", "preprod", "beta", and "prod".
func (Vars) ServiceName ¶
ServiceName is a common environment variable for the service's name.