Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div align="center">

# ⚡️cdwe (cd with env)
A simple configurable cd wrapper that provides powerful utilities for customizing your envionment per directory. \
A simple configurable cd wrapper that provides powerful utilities for customizing your environment per directory. \
*(For **ZSH** / **BASH** / **FISH** Shells)*


Expand Down Expand Up @@ -30,12 +30,19 @@ cargo install cdwe

2. **Init your shell**
```bash
cdwe init
# Or explicitly:
cdwe init zsh # zsh shells
cdwe init bash # bash shells
cdwe init fish # fish shells
```

3. **Reload your shell and start using!**
3. **Reload your shell**
```bash
exec "$SHELL"
```

4. **Start using!**
```bash
# check that env var gets set
cdwe /Users/synoet/dev/projecta
Expand Down Expand Up @@ -257,7 +264,7 @@ similar directory structure for each user.
```bash
cdwe-remove #removes the `source <output>` from your .zshrc/.bashrc/.fish

zsh #reload your shell, use bash or fish if you use those.
exec "$SHELL" #reload your shell, use bash or fish if you use those.
```

2. Uninstall binary
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ pub enum Commands {
#[arg(long = "new_dir", required = true)]
new_dir: String,
},
#[command(arg_required_else_help = true)]
#[command(arg_required_else_help = false)]
Init {
#[arg(value_name = "SHELL", required = true)]
#[arg(value_name = "SHELL", required = false)]
shell: Option<Shell>,
},
Reload {
#[arg(value_name = "SHELL", required = true)]
#[arg(value_name = "SHELL", required = false)]
shell: Option<Shell>,
},
Remove {
#[arg(value_name = "SHELL", required = true)]
#[arg(value_name = "SHELL", required = false)]
shell: Option<Shell>,
},
}
26 changes: 24 additions & 2 deletions src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ use super::Shell;
use anyhow::{Context, Result};
use std::path::Path;

pub fn init_shell(config: Option<Config>, shell: Shell) -> Result<()> {

fn detect_shell() -> Result<Shell> {
let shell_path = std::env::var("SHELL").context("Could not read $SHELL environment variable")?;
let shell_name = Path::new(&shell_path)
.file_name()
.and_then(|os_str| os_str.to_str())
.ok_or_else(|| anyhow::anyhow!("Could not determine shell from $SHELL"))?
.to_lowercase();
Shell::from_string(&shell_name).context(format!(
"Unsupported shell '{}'. Supported shells are: bash, zsh, fish.",
shell_name
))
}

pub fn init_shell(config: Option<Config>, shell: Option<Shell>) -> Result<()> {
let shell = match shell {
Some(shell) => shell,
None => detect_shell()?,
};
let home_var = std::env::var("HOME").context("no $HOME set")?;
let home = Path::new(&home_var);
let toml_path = std::path::Path::join(home, "cdwe.toml")
Expand Down Expand Up @@ -56,7 +74,11 @@ pub fn init_shell(config: Option<Config>, shell: Shell) -> Result<()> {
Ok(())
}

pub fn remove_shell(shell: Shell) -> Result<()> {
pub fn remove_shell(shell: Option<Shell>) -> Result<()> {
let shell = match shell {
Some(shell) => shell,
None => detect_shell()?,
};
let shell_script_target = shell.get_shell_script_target()?;
let config_path = shell.get_config_path()?;
let source_string = format!(
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Shell {
}

pub fn from_string(s: &str) -> Result<Self> {
match s {
match s.to_lowercase().as_str() {
"bash" => Ok(Shell::Bash),
"fish" => Ok(Shell::Fish),
"zsh" => Ok(Shell::Zsh),
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() -> Result<()> {
let cache_path = format!("{}/{}", &home, ".cdwe_cache.json");

match matches.command {
cmd::Commands::Init { shell } => init_shell(None, shell.unwrap())?,
cmd::Commands::Init { shell } => init_shell(None, shell)?,
cmd::Commands::Run { old_dir, new_dir } => {
let local_config_path = format!("{}/{}", new_dir, "cdwe.toml");
let old_local_config_path = format!("{}/{}", old_dir, "cdwe.toml");
Expand Down Expand Up @@ -50,9 +50,9 @@ async fn main() -> Result<()> {
}
cmd::Commands::Reload { shell } => {
let config: Config = Config::from_config_file(&config_path)?;
init_shell(Some(config), shell.unwrap())?;
init_shell(Some(config), shell)?;
}
cmd::Commands::Remove { shell } => remove_shell(shell.context("no shell passed")?)?,
cmd::Commands::Remove { shell } => remove_shell(shell)?,
}

Ok(())
Expand Down