Skip to content

Commit

Permalink
Merge pull request #20 from mike-lloyd03/direnv
Browse files Browse the repository at this point in the history
Add direnv support
  • Loading branch information
mike-lloyd03 authored Jan 24, 2024
2 parents 500e741 + 3d4377b commit d14eb2e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ DATABASE_URL=postgres://{{pg_user}}:{{pg_pass}}@localhost:5432

Using `rudric env <shell_name>`, these variables can be set in your environment.

```bash
**fish**:

```fish
rudric env fish | source
```

**bash**:

```bash
source <(rudric env bash)
```

A default shell can be specified by setting it in the configuration file.

# Getting Started
Expand Down Expand Up @@ -113,6 +121,21 @@ session_lifetime: 6h
renv_filename: .env
```
# direnv
Rudric includes support for [`direnv`](https://github.com/direnv/direnv). However, in order to use it, you will have to start a Rudric session before using `direnv`.

Simply add the following to your `.envrc` file:

```
$(rudric env direnv)
```

Changing into the directory with both a `.envrc` and `.renv` file will automatically source your encrypted secrets in the environment.

> ![IMPORTANT]
> If a valid session token is not set, changing into a directory with a trusted `.envrc` will prompt you for your password. This will fail as input will not be passed to Rudric. I'm working on a solution for this but for now, start a session with `rudric session` first.

# Crates

Encryption is all accomplished using the fantastic [Orion](https://github.com/orion-rs/orion) library.
Expand Down
17 changes: 13 additions & 4 deletions src/types/renv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ impl Renv {
// Loads the given `path` and parses it's contents for variable names and secret names. Secret
// names will be replaced with their secret values.
pub async fn load(app: &App, path: &Path) -> Result<Self> {
let contents = fs::read_to_string(path)?;
let contents = match fs::read_to_string(path) {
Ok(c) => c,
Err(e) => bail!(
"Failed to load file '{}': {e}",
path.to_str().unwrap_or_default()
),
};
let lines: Vec<String> = contents.lines().map(|l| l.trim().to_string()).collect();

let mut variables = vec![];
Expand Down Expand Up @@ -110,13 +116,16 @@ impl Renv {
for v in &self.variables {
let line = match shell_type {
ShellType::Fish => {
format! {"set -x {} '{}'\n", v.name, v.value}
format! {"set -x '{}' '{}';", v.name, v.value}
}
ShellType::Bash | ShellType::Zsh => {
format! {"export {}='{}'\n", v.name, v.value}
format! {"export '{}'='{}';", v.name, v.value}
}
ShellType::Nu => {
format! {"$env.{} = '{}'\n", v.name, v.value}
format! {"$env.{} = '{}';", v.name, v.value}
}
ShellType::Direnv => {
format! {"export {}={}\n", v.name, v.value}
}
};

Expand Down
1 change: 1 addition & 0 deletions src/types/shell_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub enum ShellType {
Fish,
Zsh,
Nu,
Direnv,
}

0 comments on commit d14eb2e

Please sign in to comment.