Skip to content

Commit

Permalink
feat: add functionality to check the users existing shell
Browse files Browse the repository at this point in the history
  • Loading branch information
matzxrr committed Jan 29, 2024
1 parent ce59212 commit 702378e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 20 deletions.
6 changes: 4 additions & 2 deletions dotme-core/src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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(())
}
Expand Down
1 change: 1 addition & 0 deletions dotme-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod cmd;
pub mod config;
pub mod path_utils;
pub mod repo;
pub mod shell;
30 changes: 12 additions & 18 deletions dotme-core/src/path_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = std::result::Result<T, PathUtilsError>;
Expand All @@ -23,31 +27,21 @@ pub fn base_dirs() -> Result<BaseDirs> {
))
}

/*
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();
Expand All @@ -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();
}
}
67 changes: 67 additions & 0 deletions dotme-core/src/shell.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, ShellError>;

#[derive(Default)]
pub struct ShellConfig {
pub shell: String,
pub path: String,
pub file: String,
}

impl ShellConfig {
pub fn load() -> Result<ShellConfig> {
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<String> {
match shell {
"bash" => Some(String::from(".bashrc")),
"zsh" => Some(String::from(".zshrc")),
_ => None,
}
}

fn get_shell_name_from_path(shell: &str) -> Option<String> {
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");
}
}

0 comments on commit 702378e

Please sign in to comment.