Apply .env files to your current shell
Go to file
Hamcha 4269888e8c
Escape quotes on PS1
2020-10-02 15:47:40 +02:00
scripts Add wrappers 2020-01-23 09:40:00 +01:00
src Escape quotes on PS1 2020-10-02 15:47:40 +02:00
.gitignore check in 2020-01-21 17:18:58 +01:00
Cargo.lock Escape quotes on PS1 2020-10-02 15:47:40 +02:00
Cargo.toml Escape quotes on PS1 2020-10-02 15:47:40 +02:00
README.md Add wrappers 2020-01-23 09:40:00 +01:00

README.md

osenv

Apply .env files to shell environments!

What is it

Given a .env like this:

DB_USER=admin
DB_PASSWORD=changeme

it will generate scripts for bash (and likely other *sh) and powershell, like:

Powershell

Example output:

$env:DB_USER = "admin"
$env:DB_PASSWORD = "changeme"

*nix shell

Example output:

export DB_USER=admin
export DB_PASSWORD=changeme

Installation

Requirements

  • Rust + Cargo (tested w/ 1.37)
  • A compatible shell (powershell/bash/zsh)

Compilation

cargo install --path .

This will install osenv-bin in your cargo bin folder (don't forget to add it to PATH!) You can already use it, with some caveats (having to specify the full string, like osenv-bin | iex), but you should just make a function and add it to your shell's profile file.

Add to profile

If you're using sh/bash/zsh, add this to your .bashrc/.zshrc/etc:

osenv() {
    RES=$(osenv-bin $@)
    if [ $? -eq 0 ]; then
        eval $RES
    else
        echo $RES
    fi
}

If you're using Powershell, add this to your $profile:

function osenv {
    $cmd = osenv-bin @Args
    if ($?) {
        $cmd | Invoke-Expression
    }
    else {
        Write-Output $cmd
    }
}

If you rather have them as script to source, you can find them in scripts in this repo.

If you want to enhance osenv to add a special "add to profile" command, or make some scripts that do (easy in powershell, not so much in the shell world), contributions are highly appreciated!