Skip to content

Commit

Permalink
nix_health: use enum for Shell.
Browse files Browse the repository at this point in the history
  • Loading branch information
sinrohit committed Oct 10, 2024
1 parent d101e14 commit 5cc216c
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions crates/nix_health/src/check/shell_configurations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,42 @@ use std::path::Path;

use crate::traits::{Check, CheckResult, Checkable};

#[derive(Debug, Serialize, Deserialize, Clone, Hash, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Shell {
Zsh,
Bash,
#[serde(other)]
Unknown,
}

impl Shell {
fn from_path(path: &str) -> Self {
let shell_name = Path::new(path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");

match shell_name {
"zsh" => Shell::Zsh,
"bash" => Shell::Bash,
_ => Shell::Unknown,
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct ShellConfigurations {
dotfiles: HashMap<String, Vec<String>>,
dotfiles: HashMap<Shell, Vec<String>>,
}

impl Default for ShellConfigurations {
fn default() -> Self {
let mut dotfiles = HashMap::new();
dotfiles.insert("zsh".to_string(), vec![".zshrc".to_string()]);
dotfiles.insert(Shell::Zsh, vec![".zshrc".to_string()]);
dotfiles.insert(
"bash".to_string(),
Shell::Bash,
vec![
".bashrc".to_string(),
".bash_profile".to_string(),
Expand All @@ -29,33 +53,27 @@ impl Default for ShellConfigurations {

impl ShellConfigurations {
fn check_shell_configuration(&self) -> bool {
match std::env::var("SHELL").ok() {
Some(shell) => {
let shell_name = Path::new(&shell)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("");

self.are_shell_dotfiles_nix_managed(shell_name)
}
None => false,
}
std::env::var("SHELL")
.ok()
.map(|shell| {
let shell_name = Shell::from_path(&shell);
self.are_shell_dotfiles_nix_managed(&shell_name)
})
.unwrap_or(false)
}

fn are_shell_dotfiles_nix_managed(&self, shell: &str) -> bool {
let home = match dirs::home_dir() {
Some(path) => path,
None => return false,
};

self.dotfiles.get(shell).map_or(false, |shell_files| {
shell_files.iter().all(|dotfile| {
let path = home.join(dotfile);
std::fs::canonicalize(&path)
.map(|canonical_path| canonical_path.starts_with("/nix/store/"))
.unwrap_or(false)
fn are_shell_dotfiles_nix_managed(&self, shell: &Shell) -> bool {
dirs::home_dir()
.map(|home| {
self.dotfiles.get(shell).map_or(false, |shell_files| {
shell_files.iter().all(|dotfile| {
std::fs::canonicalize(home.join(dotfile))
.map(|canonical_path| canonical_path.starts_with("/nix/store/"))
.unwrap_or(false)
})
})
})
})
.unwrap_or(false)
}
}

Expand Down

0 comments on commit 5cc216c

Please sign in to comment.