From 2c886026af30307d6d4d7175851c8d32d50a7738 Mon Sep 17 00:00:00 2001 From: Teo Stocco Date: Sat, 6 May 2023 01:41:53 +0200 Subject: [PATCH] feat: add auto launcher (#31) * fix: pr-title * feat: add auto launcher * chore: prepare release 0.6.0 --- .github/pr-title-checker-config.json | 2 +- Cargo.toml | 4 +-- README.md | 12 ++++++--- sdk/Cargo.toml | 2 +- src/args.rs | 4 +++ src/main.rs | 8 ++++++ src/shell.rs | 39 +++++++++++++++++++++++++++- 7 files changed, 62 insertions(+), 9 deletions(-) diff --git a/.github/pr-title-checker-config.json b/.github/pr-title-checker-config.json index f35be13..9344c29 100644 --- a/.github/pr-title-checker-config.json +++ b/.github/pr-title-checker-config.json @@ -4,7 +4,7 @@ "color": "eee" }, "CHECKS": { - "regexp": "(?build|chore|ci|docs|feat|fix|perf|refactor|revert|test)(?(?:/([^()\r\n]*/)|/()?(?!)?:)(?:.*)?", + "regexp": "(?build|chore|ci|docs|feat|fix|perf|refactor|revert|test)(?(?:([^()\\r\\n]*)|()?(?!)?:)(?:.*)?", "regexpFlags": "i" }, "MESSAGES": { diff --git a/Cargo.toml b/Cargo.toml index 99db58e..2af9f9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/README.md b/README.md index bb6ae8c..7385195 100644 --- a/README.md +++ b/README.md @@ -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), @@ -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 diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 9605631..3b75b53 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -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" diff --git a/src/args.rs b/src/args.rs index afaf8bc..18576c4 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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. diff --git a/src/main.rs b/src/main.rs index 3a3b6c0..6e93cb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(()) + } } } diff --git a/src/shell.rs b/src/shell.rs index 7a32990..6cdcbf7 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -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 { @@ -71,4 +75,37 @@ impl Shell { }; keys.into_iter().map(format).collect::>().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>(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::>(); + 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"); }