cargo_hoist/
shell.rs

1//! Shell Utilities
2
3use anyhow::Result;
4use std::path::PathBuf;
5
6/// The bash function to install the hoist cargo pre-hook.
7pub const INSTALL_BASH_FUNCTION: &str = r#"
8function cargo() {
9    if ~/.cargo/bin/cargo hoist --help &>/dev/null; then
10      ~/.cargo/bin/cargo hoist --quiet install
11    fi
12    ~/.cargo/bin/cargo "$@"
13}
14"#;
15
16/// The type of shell
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum ShellType {
19    /// Zsh
20    Zsh,
21    /// Bash
22    Bash,
23    /// Other
24    Other,
25}
26
27/// Detect the type of shell a user is using.
28pub fn detect_shell() -> Result<ShellType> {
29    if let Ok(shell_path) = std::env::var("SHELL") {
30        if shell_path.contains("zsh") {
31            Ok(ShellType::Zsh)
32        } else if shell_path.contains("bash") {
33            Ok(ShellType::Bash)
34        } else {
35            Ok(ShellType::Other)
36        }
37    } else {
38        // default to bash for now
39        Ok(ShellType::Bash)
40        // Err(anyhow::anyhow!("Unable to determine the user's shell."))
41    }
42}
43
44/// Helper to get the path to the user's shell config file.
45pub fn get_shell_config_file(shell_type: ShellType) -> Result<PathBuf> {
46    let home_dir = std::env::var("HOME")?;
47    match shell_type {
48        ShellType::Zsh => Ok(PathBuf::from(format!("{}/.zshrc", home_dir))),
49        _ => Ok(PathBuf::from(format!("{}/.bashrc", home_dir))),
50    }
51}