Skip to content

A cross-shell tool for sourcing POSIX compliant .env scripts

License

Notifications You must be signed in to change notification settings

ins0mniaque/srcenv

Repository files navigation

srcenv

Version License CI Release

Installation / Usage / Roadmap / Contributing

A cross-shell tool for sourcing POSIX compliant .env scripts.

Description

srcenv takes a snapshot of the POSIX shell environment, sources the .env scripts and prints a shell specific script exporting the environment variables that have changed since the snapshot, with support for reverting those changes.

Installation

Important

sh (or any POSIX shell) is required to evaluate the .env scripts. On Windows, srcenv can use sh.exe installed by git.

jq is required and needs to be in your PATH; see installation options.

Homebrew

brew install ins0mniaque/srcenv/srcenv

Debian / RPM packages

Download the latest packages from the GitHub release page.

Windows

PowerShell

Install for current user:

# Create installation directory
New-Item "$Env:LOCALAPPDATA\srcenv" -ItemType Directory

# Download srcenv to installation directory
Invoke-WebRequest https://raw.githubusercontent.com/ins0mniaque/srcenv/main/srcenv -OutFile "$Env:LOCALAPPDATA\srcenv\srcenv"

Add to $HOME\Documents\PowerShell\Profile.ps1:

# Add POSIX shell (sh) to PATH
$Env:PATH += ";$Env:ProgramFiles\Git\usr\bin\"

# Initialize srcenv
Invoke-Expression (sh "$Env:LOCALAPPDATA\srcenv\srcenv" init pwsh)

Manual

Download the latest script and/or man page and make it executable:

curl -OL https://raw.githubusercontent.com/ins0mniaque/srcenv/main/srcenv
curl -OL https://raw.githubusercontent.com/ins0mniaque/srcenv/main/srcenv.1

chmod +x srcenv

or

Clone the repository:

git clone https://github.com/ins0mniaque/srcenv

Usage

srcenv can integrate with your shell and add the following command to source .env scripts:

src [options] [files]
    [-h|--help|-v|--version]
❯ src project.env     # Sources `project.env`
❯ src project2.env    # Reverts `project.env` and sources `project2.env`
❯ src --restore       # Reverts `project2.env` (same as src -r)
❯ src --version       # Shows the version of srcenv
  srcenv x.y.z
❯ _

Shell integration

To add the src command, add the following to your shell's configuration file:

Shell Command
POSIX source <(srcenv init bash)
source <(srcenv init dash)
source <(srcenv init ksh)
source <(srcenv init zsh)
Csh/Tcsh srcenv init csh | source /dev/stdin
srcenv init tcsh | source /dev/stdin
Elvish var src~ = { }; eval &on-end={|ns| set src~ = $ns[src] } (srcenv init elvish)
Murex srcenv init murex -> source
Nushell srcenv init nu | save -f srcenv.init.nu (env.nu)
source srcenv.init.nu (config.nu)
Fish srcenv init fish | source
PowerShell Invoke-Expression (sh "/path/to/srcenv" init pwsh)

Note

To use a different command name (e.g. magicenv), add --cmd magicenv.

e.g. source <(srcenv init bash --cmd magicenv).

Tip

You can pass different arguments to srcenv with -- at the end. Without --, the default options are --backup --restore.

e.g. source <(srcenv init bash --cmd srcundo -- --restore) creates a command named srcundo that restores the last backed up changes.

For a list of supported options, see src --help.

Direct usage (without shell integration)

The following table shows how to source .env in different shells:

Shell Command
POSIX source <(srcenv sh .env)
Csh/Tcsh srcenv csh .env | source /dev/stdin
Elvish eval (srcenv elvish .env | slurp)
Murex srcenv murex .env -> source
Nushell srcenv json .env | from json | load-env
Fish srcenv fish .env | source
PowerShell Invoke-Expression (sh "/path/to/srcenv" pwsh .env)

For a list of supported shells and options, see srcenv --help.

For more advanced usage see the srcenv(1) manpage (man ./srcenv.1).

Roadmap

  • Fill out roadmap

Contributing

See CONTRIBUTING.md for more information on how to contribute.