Skip to content

Commit

Permalink
feat: add auto launcher (#31)
Browse files Browse the repository at this point in the history
* fix: pr-title

* feat: add auto launcher

* chore: prepare release 0.6.0
  • Loading branch information
zifeo authored May 5, 2023
1 parent 584bfd2 commit 2c88602
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/pr-title-checker-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"color": "eee"
},
"CHECKS": {
"regexp": "(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|test)(?<scope>(?:/([^()\r\n]*/)|/()?(?<breaking>!)?:)(?<subject>:.*)?",
"regexp": "(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|test)(?<scope>(?:([^()\\r\\n]*)|()?(?<breaking>!)?:)(?<subject>:.*)?",
"regexpFlags": "i"
},
"MESSAGES": {
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"

[package]
name = "lade"
version = "0.5.6"
version = "0.6.0"
edition = "2021"
description = "Automatically load secrets from your preferred vault as environment variables, and clear them once your shell command is over."
license = "MPL-2.0"
Expand All @@ -24,7 +24,7 @@ serde = { version = "1.0.162", features = ["derive"] }
serde_yaml = "0.9.21"
clap = { version = "4.2.7", features = ["derive"] }
regex = "1.8.1"
lade-sdk = { path = "./sdk", version = "0.5.6" }
lade-sdk = { path = "./sdk", version = "0.6.0" }
tokio = { version = "1", features = ["full"] }
indexmap = { version = "1.9.3", features = ["serde"] }
clap-verbosity-flag = "2.0.1"
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ cargo install lade --locked
cargo install --git https://github.com/zifeo/lade --locked
# upgrade
lade self upgrade
lade upgrade
# install/uninstall shell auto launch (see below)
lade install
lade uninstall
```

Compatible shells: [Fish](https://fishshell.com),
Expand Down Expand Up @@ -58,12 +62,12 @@ eval "$(lade off)"
```

You can also add `eval "$(lade on)"` to your shell configuration file (e.g.
`~/.bashrc` or `~/.config/fish/config.fish`) to automatically enable Lade on
each shell session.
`~/.bashrc`, `~/.zshrc` or `~/.config/fish/config.fish`) to automatically enable
Lade on each shell session (`lade install` will configure this for you).

Note: most of the vault loaders use their native CLI to operate. This means you
must have them installed locally and your login/credentials must be valid. Lade
may evolve by integrating directly with the corresponding API but this is left
may evolve by integrating directly with the corresponding API, but this is left
as future work.

See [lade.yml](lade.yml) or the [examples](./examples) folders for other uses
Expand Down
2 changes: 1 addition & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lade-sdk"
version = "0.5.6"
version = "0.6.0"
edition = "2021"
description = "Lade SDK"
license = "MPL-2.0"
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub enum Command {
On,
/// Disable execution hooks.
Off,
/// Install auto launcher in shell profile.
Install,
/// Uninstall auto launcher in shell profile.
Uninstall,
/// Set environment for shell.
Set(EvalCommand),
/// Unset environment for shell.
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ async fn main() -> Result<()> {
println!("{}", shell.off());
Ok(())
}
Command::Install => {
println!("Auto launcher installed in {}", shell.install());
Ok(())
}
Command::Uninstall => {
println!("Auto launcher uninstalled in {}", shell.uninstall());
Ok(())
}
}
}

Expand Down
39 changes: 38 additions & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use anyhow::{bail, Result};
use std::{collections::HashMap, env};
use std::{
collections::HashMap,
env,
path::{Path, PathBuf},
};

#[cfg(debug_assertions)]
macro_rules! import {
Expand Down Expand Up @@ -71,4 +75,37 @@ impl Shell {
};
keys.into_iter().map(format).collect::<Vec<_>>().join(";")
}
pub fn install(&self) -> String {
self.configure_auto_launch(true).display().to_string()
}
pub fn uninstall(&self) -> String {
self.configure_auto_launch(false).display().to_string()
}
fn configure_auto_launch(&self, install: bool) -> PathBuf {
let user = directories::UserDirs::new().expect("cannot get HOME location");
let home_dir = user.home_dir();
let curr_exe = std::env::current_exe().expect("cannot get current executable path");
let command = format!("eval \"$({} on)\"", curr_exe.display());
let marker = "lade-do-not-edit".to_string();
let config_file = match self {
Shell::Bash => home_dir.join(".bashrc"),
Shell::Zsh => home_dir.join(".zshrc"),
Shell::Fish => home_dir.join(".config/fish/config.fish"),
};
edit_config(&config_file, command, marker, install);
config_file
}
}

fn edit_config<P: AsRef<Path>>(config_file: P, line: String, marker: String, install: bool) {
let old_config = std::fs::read_to_string(&config_file).unwrap_or_default();
let mut new_config = old_config
.lines()
.filter(|l| !l.contains(&marker))
.collect::<Vec<_>>();
let new_line = format!("{} # {}", line, marker);
if install {
new_config.push(&new_line);
}
std::fs::write(config_file, new_config.join("\n")).expect("cannot write to config file");
}

0 comments on commit 2c88602

Please sign in to comment.