magus v0.4.3 is out. See what's new
¶ View generated markdown
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 for this run: its later reads and the processes it starts see it, other runs in the same magus process do not.

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 for this run, the counterpart of set.

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 for this run, as set does, 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. ↩︎

generatedreference/buzz/envmodulestdlibmagusfile
Last updated (29d8b3de)
Earlier changes on this page (7)

Full history ↗ · Blame source ↗

Glossary

Magusfile

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

Op

A single tool-native command a target composes (long form: operation); the middle of the work hierarchy (Spell to Op to Target). Op in full.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. Module in full.

Buzz

The language magusfiles are written in (the .buzz engine). Buzz in full.

Sandbox

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

CI

An ordinary target you compose yourself with ctx.needs; magus does not hardcode its stages. It is the anchor magus affected ci keys off, and ci strips the rw charm. CI in full.

Run

One target executing under one magus invocation, such as magus run test web or magus affected ci. A run keeps its captured output behind an output reference. Every magus run is a run whether or not any job asked for it; see Job for how the two relate. Run in full.

Conventions

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