From 702378e3bdf619c1408460e36aa4a52d37dc6843 Mon Sep 17 00:00:00 2001 From: matzxrr Date: Mon, 29 Jan 2024 00:02:00 -0700 Subject: [PATCH] feat: add functionality to check the users existing shell --- dotme-core/src/cmd/init.rs | 6 ++-- dotme-core/src/lib.rs | 1 + dotme-core/src/path_utils.rs | 30 +++++++--------- dotme-core/src/shell.rs | 67 ++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 20 deletions(-) create mode 100644 dotme-core/src/shell.rs diff --git a/dotme-core/src/cmd/init.rs b/dotme-core/src/cmd/init.rs index 3154455..7a5f2da 100644 --- a/dotme-core/src/cmd/init.rs +++ b/dotme-core/src/cmd/init.rs @@ -5,7 +5,7 @@ use dialoguer::{theme::ColorfulTheme, Error as DialoguerError, Input}; use console::Term; -use crate::path_utils::{add_to_bashrc, base_dirs, PathUtilsError}; +use crate::path_utils::{add_config_cmd_to_shell_file, base_dirs, PathUtilsError}; use crate::repo::{Repo, RepoError}; #[derive(Debug, Error)] @@ -52,7 +52,9 @@ pub fn init() -> Result<()> { repo.repo.path().display() ))?; - add_to_bashrc(repo.repo.path())?; + term.write_line("Writing 'config' command to your shell file")?; + add_config_cmd_to_shell_file(repo.repo.path())?; + term.write_line("Added 'config' command to your shell")?; Ok(()) } diff --git a/dotme-core/src/lib.rs b/dotme-core/src/lib.rs index e96053b..62c67b9 100644 --- a/dotme-core/src/lib.rs +++ b/dotme-core/src/lib.rs @@ -5,3 +5,4 @@ pub mod cmd; pub mod config; pub mod path_utils; pub mod repo; +pub mod shell; diff --git a/dotme-core/src/path_utils.rs b/dotme-core/src/path_utils.rs index a558781..538bbf0 100644 --- a/dotme-core/src/path_utils.rs +++ b/dotme-core/src/path_utils.rs @@ -3,10 +3,14 @@ use std::{fs::OpenOptions, io::Write, path::Path}; use directories::{BaseDirs, ProjectDirs}; use thiserror::Error; +use crate::shell::{ShellConfig, ShellError}; + #[derive(Debug, Error)] pub enum PathUtilsError { #[error("Dirs Error: {0}")] DirectoriesError(&'static str), + #[error("Shell Error: {0}")] + ShellError(#[from] ShellError), } type Result = std::result::Result; @@ -23,31 +27,21 @@ pub fn base_dirs() -> Result { )) } -/* -Command::new("sh") - .arg("-c") - .arg("echo hello") - .output() - .expect("failed to execute process") - -Reading bash file -basename $(readlink /proc/$$/exe) -ps -o comm= -p $$ -## My Aliases -*/ -pub fn add_to_bashrc(path: &Path) -> Result<()> { - let base_dirs = base_dirs()?; - let home_dirs = base_dirs.home_dir(); - let bashrc = home_dirs.join(".bashrc"); +pub fn add_config_cmd_to_shell_file(path: &Path) -> Result<()> { + let shell_config = ShellConfig::load()?; let alias = format!( "alias config='/usr/bin/git --git-dir={} --work-tree=$HOME'\n", path.display() ); + let base_dirs = base_dirs()?; + let home = base_dirs.home_dir(); + let shell_config_file = home.join(shell_config.file); + let mut file = OpenOptions::new() .write(true) .append(true) - .open(bashrc) + .open(shell_config_file) .unwrap(); file.write_all(alias.as_bytes()).unwrap(); @@ -64,6 +58,6 @@ mod test_path_utils { #[test] fn test_add() { let path = PathBuf::from("/home/magreenberg/.test"); - add_to_bashrc(path.as_path()).unwrap(); + add_config_cmd_to_shell_file(path.as_path()).unwrap(); } } diff --git a/dotme-core/src/shell.rs b/dotme-core/src/shell.rs new file mode 100644 index 0000000..f3c9959 --- /dev/null +++ b/dotme-core/src/shell.rs @@ -0,0 +1,67 @@ +use std::env; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum ShellError { + #[error("Can't find the users shell, might not be supported")] + CannotFindShell, +} + +type Result = std::result::Result; + +#[derive(Default)] +pub struct ShellConfig { + pub shell: String, + pub path: String, + pub file: String, +} + +impl ShellConfig { + pub fn load() -> Result { + let shell_result = env::var("SHELL"); + if let Ok(shell_path) = shell_result { + if let Some(shell_name) = get_shell_name_from_path(&shell_path) { + if let Some(shell_file) = match_shell_to_config_file(&shell_name) { + return Ok(ShellConfig { + shell: shell_name, + path: shell_path, + file: shell_file, + }); + } + } + } + Err(ShellError::CannotFindShell) + } +} + +fn match_shell_to_config_file(shell: &str) -> Option { + match shell { + "bash" => Some(String::from(".bashrc")), + "zsh" => Some(String::from(".zshrc")), + _ => None, + } +} + +fn get_shell_name_from_path(shell: &str) -> Option { + let shell_name = shell.split('/').last().expect("should have last value"); + match shell_name { + "bash" => Some(String::from("bash")), + "zsh" => Some(String::from("zsh")), + // "fish" => Some(String::from("fish")), + // "sh" => Some(String::from("sh")), + _ => None, + } +} + +#[cfg(test)] +mod shell_tests { + use super::*; + + #[test] + fn it_should_load_users_shell() { + let shell = ShellConfig::load().expect("load shell"); + assert_eq!(&shell.shell, "bash"); + assert_eq!(&shell.file, ".bashrc"); + assert_eq!(&shell.path, "/bin/bash"); + } +}