environment

A tiny CLI that reads a JSON object and runs a command (or an interactive shell) with the object's keys and values injected as environment variables.

Handy when you have a JSON blob of configuration / secrets and want to spin up a process with those values exported into its environment — without writing a wrapper script.

Install

cargo install --git https://git.hatter.ink/hatter/environment.git

Build from source:

git clone https://git.hatter.ink/hatter/environment.git
cd environment
cargo build --release

Usage

environment [OPTIONS] [ARGS]...

Reads JSON from stdin (or --input <file>), converts the top-level object into KEY=value environment variables, and spawns the given command — or an interactive shell if no command is supplied.

Options

Option Description
-s, --shell <SHELL> Shell to launch when no command is given. Defaults to $SHELL, falling back to /bin/sh.
-U, --uppercase Uppercase all environment-variable keys.
--debug Print the resolved command, full environment, and exit status to stderr.
--mute Suppress the ended with exit code: N trailer.
--input <INPUT> JSON source. - (default) reads stdin; otherwise a path to a file.
-h, --help Print help.
-V, --version Print version.

--mute and --debug are mutually exclusive.

Value conversion

Only JSON objects are accepted at the top level. Each member is converted as follows:

JSON value Environment value
string the string, verbatim
number its to_string() representation
boolean true / false
array the first element, converted by the same rules (empty arrays and nested arrays/objects are ignored)
null ignored (warning printed)
object ignored (warning printed)

Examples

Run a command with variables from a JSON file:

environment --input env.json -- printenv DB_HOST DB_PORT

Pipe JSON in and drop into an interactive shell with the variables exported:

echo '{"FOO": "bar", "COUNT": 3}' | environment -s /bin/bash

Uppercase every key (handy for DATABASE_URL-style names):

echo '{"db_host": "localhost", "db_port": 5432}' | environment -U -- printenv DB_HOST DB_PORT

Inspect what would be spawned without ambiguity:

echo '{"TOKEN": "secret"}' | environment --debug -- ./my-service

License

MIT — see LICENSE.

S
Description
No description provided
Readme MIT 34 KiB
Languages
Rust 100%