env

package
v1.20210518.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2021 License: MIT Imports: 12 Imported by: 9

Documentation

Overview

Package env contains environment variable helpers, enabling better tests and easier use of environment variables.

Index

Constants

View Source
const (
	// VarRegion is a common env var name.
	VarRegion = "AWS_REGION"
	// 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 Clear added in v1.20201204.1

func Clear()

Clear sets .Env() to an empty env var set.

func IsDev added in v1.20201204.1

func IsDev(serviceEnv string) bool

IsDev returns if the environment is development.

func IsDevTest added in v1.20210201.1

func IsDevTest(serviceEnv string) bool

IsDevTest returns if the environment is a local development environment (i.e. `dev` or `test`).

func IsDevlike added in v1.20201204.1

func IsDevlike(serviceEnv string) bool

IsDevlike returns if the environment is development. It is strictly the inverse of `IsProdlike`.

func IsProdlike added in v1.20201204.1

func IsProdlike(serviceEnv string) bool

IsProdlike returns if the environment is prodlike.

func IsProduction added in v1.20201204.1

func IsProduction(serviceEnv string) bool

IsProduction returns if the environment is production.

func Restore

func Restore()

Restore sets .Env() to the current os environment.

func SetEnv

func SetEnv(vars Vars)

SetEnv sets the env vars.

func Split added in v1.20210116.3

func Split(s string) (key, value string)

Split splits an env var in the form `KEY=value`.

func WithVars added in v1.20201204.1

func WithVars(ctx context.Context, vars Vars) context.Context

WithVars adds environment variables to a context.

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

func OptEnviron(environ ...string) Option

OptEnviron parses the output of `os.Environ()`

func OptRemove added in v1.20201204.1

func OptRemove(keys ...string) Option

OptRemove removes keys from a set.

func OptSet added in v1.20201204.1

func OptSet(overides Vars) Option

OptSet overrides values in the set with a specific set of values.

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

type Unmarshaler interface {
	UnmarshalEnv(vars Vars) error
}

Unmarshaler is a type that implements `UnmarshalEnv`.

type Vars

type Vars map[string]string

Vars is a set of environment variables.

func Env

func Env() Vars

Env returns the current env var set.

func GetVars added in v1.20201204.1

func GetVars(ctx context.Context) Vars

GetVars gets environment variables from a context.

func Merge added in v1.20201204.1

func Merge(sets ...Vars) Vars

Merge merges a given set of environment variables.

func New added in v1.20201204.1

func New(opts ...Option) Vars

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

func Parse(s string) (Vars, error)

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

func (ev Vars) Base64(envVar string, defaults ...[]byte) ([]byte, error)

Base64 returns a []byte value for a given key whose value is encoded in base64.

func (Vars) Bool

func (ev Vars) Bool(envVar string, defaults ...bool) 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) Bytes

func (ev Vars) Bytes(envVar string, defaults ...[]byte) []byte

Bytes returns a []byte value for a given key.

func (Vars) CSV

func (ev Vars) CSV(envVar string, defaults ...string) []string

CSV returns a string array for a given string var.

func (Vars) Delete

func (ev Vars) Delete(key string)

Delete removes a key from the set.

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) Duration

func (ev Vars) Duration(envVar string, defaults ...time.Duration) (time.Duration, error)

Duration returns a duration value for a given key.

func (Vars) Expand added in v1.20201204.1

func (ev Vars) Expand(value string) string

Expand calls os.Expand with the variable set as the environment value resolver.

func (Vars) Float32 added in v1.20201204.1

func (ev Vars) Float32(envVar string, defaults ...float32) (float32, error)

Float32 returns an float32 value for a given key.

func (Vars) Float64

func (ev Vars) Float64(envVar string, defaults ...float64) (float64, error)

Float64 returns an float64 value for a given key.

func (Vars) Get added in v1.20201204.1

func (ev Vars) Get(envVar string) string

Get gets a variable as a string. It mirrors os.Getenv.

func (Vars) Has

func (ev Vars) Has(envVar string) bool

Has returns if a key is present in the set.

func (Vars) HasAll

func (ev Vars) HasAll(envVars ...string) bool

HasAll returns if all of the given vars are present in the set.

func (Vars) HasAny

func (ev Vars) HasAny(envVars ...string) bool

HasAny returns if any of the given vars are present in the set.

func (Vars) Hostname added in v1.20210103.1

func (ev Vars) Hostname(defaults ...string) string

Hostname is a common environment variable for the machine's hostname.

func (Vars) Int

func (ev Vars) Int(envVar string, defaults ...int) (int, error)

Int returns an integer value for a given key.

func (Vars) Int32

func (ev Vars) Int32(envVar string, defaults ...int32) (int32, error)

Int32 returns an integer value for a given key.

func (Vars) Int64

func (ev Vars) Int64(envVar string, defaults ...int64) (int64, error)

Int64 returns an int64 value for a given key.

func (Vars) IsDev added in v1.20201204.1

func (ev Vars) IsDev() bool

IsDev returns if the ServiceEnv is the local development environment.

func (Vars) IsDevlike added in v1.20201204.1

func (ev Vars) IsDevlike() bool

IsDevlike returns if the ServiceEnv is strictly the inverse of `IsProdlike`.

func (Vars) IsProdlike

func (ev Vars) IsProdlike() bool

IsProdlike returns if the ServiceEnv is "prodlike".

func (Vars) IsProduction

func (ev Vars) IsProduction() bool

IsProduction returns if the ServiceEnv is a production environment.

func (Vars) Must

func (ev Vars) Must(keys ...string)

Must enforces that a given set of environment variables are present and panics if they're not present.

func (Vars) MustBase64

func (ev Vars) MustBase64(envVar string, defaults ...[]byte) []byte

MustBase64 returns a []byte value for a given key encoded with base64, and panics if malformed.

func (Vars) MustDuration

func (ev Vars) MustDuration(envVar string, defaults ...time.Duration) time.Duration

MustDuration returnss a duration value for a given key and panics if malformed.

func (Vars) MustFloat32 added in v1.20201204.1

func (ev Vars) MustFloat32(envVar string, defaults ...float32) float32

MustFloat32 returns an float64 value for a given key and panics if it is malformed.

func (Vars) MustFloat64

func (ev Vars) MustFloat64(envVar string, defaults ...float64) float64

MustFloat64 returns an float64 value for a given key and panics if it is malformed.

func (Vars) MustInt

func (ev Vars) MustInt(envVar string, defaults ...int) int

MustInt returns an integer value for a given key and panics if it is malformed.

func (Vars) MustInt32

func (ev Vars) MustInt32(envVar string, defaults ...int32) int32

MustInt32 returns an integer value for a given key and panics if it is malformed.

func (Vars) MustInt64

func (ev Vars) MustInt64(envVar string, defaults ...int64) int64

MustInt64 returns an int64 value for a given key and panics if it is malformed.

func (Vars) MustUint32

func (ev Vars) MustUint32(envVar string, defaults ...uint32) uint32

MustUint32 returns an uint32 value for a given key and panics if it is malformed.

func (Vars) MustUint64

func (ev Vars) MustUint64(envVar string, defaults ...uint64) uint64

MustUint64 returns an uint64 value for a given key and panics if it is malformed.

func (Vars) Raw

func (ev Vars) Raw() []string

Raw returns a raw KEY=VALUE form of the vars.

func (Vars) ReadInto

func (ev Vars) ReadInto(obj interface{}) error

ReadInto sets an object based on the fields in the env vars set.

func (Vars) Require

func (ev Vars) Require(keys ...string) error

Require enforces that a given set of environment variables are present.

func (Vars) Restore

func (ev Vars) Restore(key string)

Restore resets an environment variable to it's environment value.

func (Vars) ServiceEnv

func (ev Vars) ServiceEnv(defaults ...string) string

ServiceEnv is a common environment variable for the services environment. Common values include "dev", "ci", "sandbox", "preprod", "beta", and "prod".

func (Vars) ServiceName

func (ev Vars) ServiceName(defaults ...string) string

ServiceName is a common environment variable for the service's name.

func (Vars) Set

func (ev Vars) Set(envVar, value string)

Set sets a value for a key.

func (Vars) String

func (ev Vars) String(envVar string, defaults ...string) string

String returns a string value for a given key, with an optional default vaule.

func (Vars) Uint32

func (ev Vars) Uint32(envVar string, defaults ...uint32) (uint32, error)

Uint32 returns an uint32 value for a given key.

func (Vars) Uint64

func (ev Vars) Uint64(envVar string, defaults ...uint64) (uint64, error)

Uint64 returns an uint64 value for a given key.

func (Vars) Union

func (ev Vars) Union(other Vars) Vars

Union returns the union of the two sets, other replacing conflicts.

func (Vars) Vars

func (ev Vars) Vars() []string

Vars returns all the vars stored in the env var set.

func (Vars) Version added in v1.20210103.1

func (ev Vars) Version(defaults ...string) string

Version is a common environment variable for the service version.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL