Skip to content

Commit

Permalink
feat: add minimal support for sh (#95)
Browse files Browse the repository at this point in the history
* feat: add minimal support for sh

* bump: chore
  • Loading branch information
zifeo authored Oct 31, 2024
1 parent 7a1c0a8 commit b959b30
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
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.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"
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
```
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.11.5-beta.1"
version = "0.11.5"
edition = "2021"
description = "Lade SDK"
license = "MPL-2.0"
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down
57 changes: 39 additions & 18 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Shell {
Bash,
Zsh,
Fish,
Sh,
}

impl Shell {
Expand All @@ -33,6 +34,7 @@ impl Shell {
Shell::Bash => "bash",
Shell::Zsh => "zsh",
Shell::Fish => "fish",
Shell::Sh => "sh",
}
}

Expand All @@ -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<String> {
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<String> {
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, String>) -> 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 => {
Expand All @@ -91,37 +102,47 @@ impl Shell {

pub fn unset(&self, keys: Vec<String>) -> 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::<Vec<_>>().join(";")
}

pub fn install(&self) -> String {
self.configure_auto_launch(true).display().to_string()
pub fn install(&self) -> Result<String> {
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<String> {
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<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 = match self {
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)
}
}

Expand Down

0 comments on commit b959b30

Please sign in to comment.