diff --git a/Cargo.toml b/Cargo.toml index 0495ac2..656de4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ resolver = "2" [package] name = "lade" -version = "0.11.5-beta.1" +version = "0.11.5" 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" @@ -25,7 +25,7 @@ serde = { version = "1.0.214", features = ["derive"] } serde_yaml = "0.9.34" clap = { version = "4.5.20", features = ["derive"] } regex = "1.11.1" -lade-sdk = { path = "./sdk", version = "0.11.5-beta.1" } +lade-sdk = { path = "./sdk", version = "0.11.5" } tokio = { version = "1", features = ["full"] } indexmap = { version = "2.6.0", features = ["serde"] } clap-verbosity-flag = "2.2.2" diff --git a/README.md b/README.md index 7d4e488..e93a6fd 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,7 @@ eval "$(lade off)" eval "$(cargo run -- on)" echo a $A1 $A2 $B1 $B2 $B3 $C1 $C2 $C3 cargo run -- -vvv set echo a +cargo run -- inject echo a eval "$(cargo run -- off)" eval "$(lade on)" ``` diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index 48425c8..7eede69 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "lade-sdk" -version = "0.11.5-beta.1" +version = "0.11.5" edition = "2021" description = "Lade SDK" license = "MPL-2.0" diff --git a/src/main.rs b/src/main.rs index 360d65f..e3b9247 100644 --- a/src/main.rs +++ b/src/main.rs @@ -283,19 +283,19 @@ async fn main() -> Result<()> { Ok(()) } Command::On => { - println!("{}\n{}", shell.off(), shell.on()); + println!("{}\n{}", shell.off()?, shell.on()?); Ok(()) } Command::Off => { - println!("{}", shell.off()); + println!("{}", shell.off()?); Ok(()) } Command::Install => { - println!("Auto launcher installed in {}", shell.install()); + println!("Auto launcher installed in {}", shell.install()?); Ok(()) } Command::Uninstall => { - println!("Auto launcher uninstalled in {}", shell.uninstall()); + println!("Auto launcher uninstalled in {}", shell.uninstall()?); Ok(()) } } diff --git a/src/shell.rs b/src/shell.rs index 7bdd8d6..3aefb9b 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -25,6 +25,7 @@ pub enum Shell { Bash, Zsh, Fish, + Sh, } impl Shell { @@ -33,6 +34,7 @@ impl Shell { Shell::Bash => "bash", Shell::Zsh => "zsh", Shell::Fish => "fish", + Shell::Sh => "sh", } } @@ -51,34 +53,43 @@ impl Shell { "bash" => Ok(Shell::Bash), "zsh" => Ok(Shell::Zsh), "fish" => Ok(Shell::Fish), - _ => bail!("Unsupported shell"), + "sh" => Ok(Shell::Sh), + _ => bail!("Unsupported shell: {shell}"), } } - pub fn on(&self) -> String { + pub fn on(&self) -> Result { match self { - Shell::Bash => format!( + Shell::Bash => Ok(format!( "{}\n{}", import!("../scripts/bash-preexec.sh"), import!("../scripts/on.bash") - ), - Shell::Zsh => import!("../scripts/on.zsh"), - Shell::Fish => import!("../scripts/on.fish"), + )), + Shell::Zsh => Ok(import!("../scripts/on.zsh")), + Shell::Fish => Ok(import!("../scripts/on.fish")), + _ => { + let shell = self.bin(); + bail!("Unsupported behavior on shell {shell}") + } } } - pub fn off(&self) -> String { + pub fn off(&self) -> Result { match self { - Shell::Bash => import!("../scripts/off.bash"), - Shell::Zsh => import!("../scripts/off.zsh"), - Shell::Fish => import!("../scripts/off.fish"), + Shell::Bash => Ok(import!("../scripts/off.bash")), + Shell::Zsh => Ok(import!("../scripts/off.zsh")), + Shell::Fish => Ok(import!("../scripts/off.fish")), + _ => { + let shell = self.bin(); + bail!("Unsupported behavior on shell {shell}") + } } } pub fn set(&self, env: HashMap) -> String { env.into_iter() .map(|(k, v)| match self { - Shell::Bash | Shell::Zsh => { + Shell::Bash | Shell::Zsh | Shell::Sh => { format!("export {k}='{v}'") } Shell::Fish => { @@ -91,21 +102,23 @@ impl Shell { pub fn unset(&self, keys: Vec) -> String { let format = match self { - Shell::Zsh | Shell::Bash => |k| format!("unset -v {k}"), + Shell::Zsh | Shell::Bash | Shell::Sh => |k| format!("unset -v {k}"), Shell::Fish => |k| format!("set --global --erase {k}"), }; keys.into_iter().map(format).collect::>().join(";") } - pub fn install(&self) -> String { - self.configure_auto_launch(true).display().to_string() + pub fn install(&self) -> Result { + self.configure_auto_launch(true) + .map(|c| c.display().to_string()) } - pub fn uninstall(&self) -> String { - self.configure_auto_launch(false).display().to_string() + pub fn uninstall(&self) -> Result { + self.configure_auto_launch(false) + .map(|c| c.display().to_string()) } - fn configure_auto_launch(&self, install: bool) -> PathBuf { + fn configure_auto_launch(&self, install: bool) -> Result { 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"); @@ -113,15 +126,23 @@ impl Shell { Shell::Bash => format!("source <(echo \"$({} on)\")", curr_exe.display()), Shell::Zsh => format!("eval \"$({} on)\"", curr_exe.display()), Shell::Fish => format!("source ({} on | psub)", curr_exe.display()), + _ => { + let shell = self.bin(); + bail!("Unsupported behavior on shell {shell}") + } }; 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"), + _ => { + let shell = self.bin(); + bail!("Unsupported behavior on shell {shell}") + } }; edit_config(&config_file, command, marker, install); - config_file + Ok(config_file) } }