magus v0.3.0 is out. See what's new
¶ View markdown source · ✎ Suggest an edit
3 min read

env

Process environment variable access.

Naming convention: import the module under its bare name (import "env"), reach members with a backslash, and call methods in camelCase: env\someMethod.

Methods

get

Return the value of name, or "" if unset. Use lookup to tell unset from set-but-empty.

Signature: env\get(name) → string1 · source

Parameter Type Optional Description
name string

Returns: string

lookup

Return (value, found); found is false when name is unset or stripped by the sandbox.

Signature: env\lookup(name) → string, bool2 · source

Parameter Type Optional Description
name string

Returns: string, bool

set

Set name to value in the current process environment.

Signature: env\set(name, value) · source

Parameter Type Optional Description
name string
value string

list

Return all environment variables as a name→value map.

Signature: env\list() → map[string]string · source

Returns: map[string]string

unset

Remove name from the current process environment.

Signature: env\unset(name) · source

Parameter Type Optional Description
name string

expand

Replace $VAR and ${VAR} references in s with their values (sandbox-stripped names expand to "").

Signature: env\expand(s) → string · source

Parameter Type Optional Description
s string

Returns: string

home

Return the current user's home directory.

Signature: env\home() → string · source

Returns: string

getOr

Return the value of name, or def when name is unset or stripped by the sandbox. Unlike get, an empty string is returned as-is - def only applies when the variable is absent.

Signature: env\getOr(name, def) → string · source

Parameter Type Optional Description
name string
def string

Returns: string

require

Return the value of name, or raise when it is unset or stripped by the sandbox. The fail-fast complement to get/lookup: a CI magusfile that needs GITHUB_TOKEN states the requirement once instead of threading a lookup-then-fatal check through every caller. A set-but-empty variable satisfies the requirement (its empty value is returned).

Signature: env\require(name) → string · source

Parameter Type Optional Description
name string

Returns: string

parseDotenv

Parse .env-format content into a name->value map. Supports KEY=VALUE, blank lines, # comments, a leading export keyword, single/double quotes (double-quoted values honor \n \t " \ escapes), and inline comments after unquoted values. Pure: it does not touch the process environment.

Signature: env\parseDotenv(content) → map[string]string · source

Parameter Type Optional Description
content string

Returns: map[string]string

readDotenv

Read a .env file and return its name->value map (parse_dotenv over the file contents). Errors if the file cannot be read.

Signature: env\readDotenv(path) → map[string]string · source

Parameter Type Optional Description
path string

Returns: map[string]string

loadDotenv

Read a .env file and set each variable in the process environment, without overwriting names already set (the dotenv convention) or names the sandbox strips. A no-op in a recording/dry-run.

Signature: env\loadDotenv(path) · source

Parameter Type Optional Description
path string

  1. env\get is also in Buzz's standard library (os.env); the magus form is sandbox-aware. ↩︎

  2. env\lookup is also in Buzz's standard library (os.env (returns null when unset)); the magus form is sandbox-aware. ↩︎

auto-generatedenvmodulestdlibmagusfile
Last updated (e0463131)
Earlier changes on this page (1)

Full history ↗ · Blame source ↗

Glossary

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. See targets.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. See the module reference.

Buzz

The language magusfiles are written in (the .buzz engine). See engines.

Sandbox

The restricted filesystem and environment a target runs in, so builds stay reproducible and side-effect-free. See sandbox.

CI

An ordinary magusfile-defined target you compose yourself with magus\needs - magus does not hardcode its stages. Magus.RunCI treats it specially only in that it strips the rw charm, it is the anchor magus affected ci keys off, and a selected scope with no project declaring it is a load error rather than a silent no-op. See targets.

Conventions

This page uses none of the site's convention markers. The full set is on the conventions page.