Standard library · System and tools

env

Environment variables: reading them with the right type, .env files, the run profile, and masking sensitive values.

import env
On this page
  1. Functions

Settings such as a server port, a database address and API keys belong in environment variables rather than in the code. The env package reads them with the right type and gives you a default when they are not set. It can load them from a .env file too.

The "profile" is read from SALAM_ENV and says whether the program is running in development, test or production.

Functions

Get

func Get(name: str): str
The value of a variable, or an empty string.
env-Get.salam
import env

func main:
    env.Set("PORT", "8080")
    env.Set("DEBUG", "true")
    env.Set("RATIO", "0.75")
    println env.Get("PORT"), env.Has("PORT"), env.Has("NOPE")
    println env.GetInt("PORT", 80), env.GetInt("NOPE", 80)
    println env.GetBool("DEBUG", false), env.GetFloat("RATIO", 1.0)
    println env.GetOr("HOST", "localhost")
    env.Unset("PORT")
    println env.Has("PORT")
    env.Set("API_KEY", "example-not-a-real-key")
    println env.Mask("example-not-a-real-key"), env.GetMasked("API_KEY")
    env.Set("NAME", "Salam")
    println env.Expand("${NAME} on port ${PORT:-3000}")
end
Output8080 true false 8080 80 true 0.75 localhost false ex****ey ex****ey Salam on port 3000

GetOr

func GetOr(name: str, def: str): str
The value of a variable, or the default when it is not set.

The example for this item is shown with the ones above: env-Get.salam

Has

func Has(name: str): bool
True when the variable is set.

The example for this item is shown with the ones above: env-Get.salam

GetInt

func GetInt(name: str, def: i64): i64
The value as a whole number, or the default when it is not set or is not a number.

The example for this item is shown with the ones above: env-Get.salam

GetFloat

func GetFloat(name: str, def: f64): f64
The value as a decimal number, or the default.

The example for this item is shown with the ones above: env-Get.salam

GetBool

func GetBool(name: str, def: bool): bool
The value as a boolean; true, 1 and yes count as true.

The example for this item is shown with the ones above: env-Get.salam

Set

func Set(name: str, value: str): bool
Sets a variable for this program and for the processes it starts afterwards.

The example for this item is shown with the ones above: env-Get.salam

Unset

func Unset(name: str): bool
Removes a variable.

The example for this item is shown with the ones above: env-Get.salam

Mask

func Mask(value: str): str
Masks a sensitive value for a log, showing only the first and last two characters.

The example for this item is shown with the ones above: env-Get.salam

GetMasked

func GetMasked(name: str): str
Returns a variable's value masked.

The example for this item is shown with the ones above: env-Get.salam

Profile

func Profile(): str
The name of the run profile, from SALAM_ENV.
env-Profile.salam
import env

func main:
    env.Set("SALAM_ENV", "production")
    println env.Profile(), env.IsProduction(), env.IsDevelopment(), env.IsTest(), env.IsStaging()
    env.Set("SALAM_ENV", "test")
    println env.Profile(), env.IsTest()
end
Outputproduction true false false false test true

IsProduction

func IsProduction(): bool
True in the production profile.

The example for this item is shown with the ones above: env-Profile.salam

IsDevelopment

func IsDevelopment(): bool
True in the development profile.

The example for this item is shown with the ones above: env-Profile.salam

IsTest

func IsTest(): bool
True in the test profile.

The example for this item is shown with the ones above: env-Profile.salam

IsStaging

func IsStaging(): bool
True in the staging profile.

The example for this item is shown with the ones above: env-Profile.salam

Load

func Load(path: str): int
Loads a KEY=VALUE file without changing variables that are already set. It returns how many variables were read.
env-Load.salam
import env
import io

func main:
    io.WriteFile(".env", "DB_HOST=localhost\nDB_PORT=5432\n# a comment\n")
    println env.Load(".env"), env.Get("DB_HOST")
    env.Set("DB_PORT", "1")
    env.Load(".env")
    println env.Get("DB_PORT")
    env.Overload(".env")
    println env.Get("DB_PORT")
    println env.LoadText("A=1\nB=2"), env.Get("B")
    println env.LoadDotEnv(".env")
    io.WriteFile(".env.test", "MODE=testing\n")
    env.Set("SALAM_ENV", "test")
    println env.LoadProfile("."), env.Get("MODE")
end
Output2 localhost 1 5432 2 2 2 1 testing

Overload

func Overload(path: str): int
Like Load, but it replaces existing variables with the file's values too.

The example for this item is shown with the ones above: env-Load.salam

LoadText

func LoadText(text: str): int
Does the same from text instead of a file.

The example for this item is shown with the ones above: env-Load.salam

LoadDotEnv

func LoadDotEnv(path: str): int
Loads the .env file.

The example for this item is shown with the ones above: env-Load.salam

LoadProfile

func LoadProfile(dir: str): int
Loads the .env.<profile> file from the given directory, such as .env.test in the test profile.

The example for this item is shown with the ones above: env-Load.salam

Expand

func Expand(text: str): str
Replaces ${name} and ${name:-default} in text with the values of the variables.

The example for this item is shown with the ones above: env-Get.salam