envconfig

    Dark Mode
Search:
Group by:

envconfig provides a function to get config objects from environment variables.

Usage

Examples:

from os import putEnv

import
  envconfig

type
  MyApp = object
    name, version, dir: string
    port: int
    dev: bool

putEnv("MYAPP_NAME", "envconfig")
putEnv("MYAPP_VERSION", "v1.0.0")
putEnv("MYAPP_DIR", "/opt/envconfig")
putEnv("MYAPP_PORT", "1234")
putEnv("MYAPP_DEV", "true")
let config = getEnvConfig(MyApp)
doAssert config.name == "envconfig"
doAssert config.version == "v1.0.0"
doAssert config.dir == "/opt/envconfig"
doAssert config.port == 1234
doAssert config.dev == true

Types

ValidationError = object of CatchableError

Procs

proc getEnvConfig(T: typedesc; prefix = ""; sep = ","; requires: seq[string] = @[];
                 mins: seq[tuple[name: string, val: float64]] = @[];
                 maxs: seq[tuple[name: string, val: float64]] = @[];
                 regexps: seq[tuple[name: string, val: Regex]] = @[]): T:type

Returns a object that values were set to fields by environment variables. This proc lookups environment variables with UPPERCASE object name (T) and UPPER_SNAKE_CASE object field names.

A prefix of environment variables is UPPERCASE object name (T). You set prefix if you want to change a prefix of environment variables.

This proc splits values with sep if the type of field is seq.

export FLUITS="apple,banana,orange"
# -> @["apple", "banana", "orange"]

You can validate values with requires, mins, maxs, regexps.

  • requires - Environment variables must be set and must be not empty value.
  • mins - Environment variables must not be less than val of each items of seq.
  • maxs - Environment variables must not be greater than val of each items of seq.
  • regexps - Environment variables must regex-match val of each items of seq.

Avairable field types:

  • string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float, float32, float64, char, byte
  • seq[string], seq[bool], seq[int], seq[int8], seq[int16], seq[int32], seq[int64], seq[uint], seq[uint8], seq[uint16], seq[uint32], seq[uint64], seq[float], seq[float32], seq[float64], seq[char], seq[byte]

Raises errors:

  • ValidationError - Raises this error if set value (not integer) to int field. Raises that if value type is float, uint and bool. Raises that if value range is not matched requires, mins, maxs, regexps.

Note:

  • array type is not supported.
  • ref object type is not supported.

Examples:

from os import putEnv

type
  MyApp = object
    name: string
    num: int
    devMode: bool
    fluits: seq[string]

putEnv("MYAPP_NAME", "myapp")
putEnv("MYAPP_NUM", "5")
putEnv("MYAPP_DEV_MODE", "true")
putEnv("MYAPP_FLUITS", "apple,banana,orange")
let config = getEnvConfig(MyApp, requires = @["name", "num", "devMode", "fluits"],
                       mins = @[("num", 0.0)], maxs = @[("num", 10.0)])
doAssert config.name == "myapp"
doAssert config.num == 5
doAssert config.devMode
doAssert config.fluits == @["apple", "banana", "orange"]