|
|
@ -1,5 +1,6 @@ |
|
|
|
use crate::builtins::{Builtin, BuiltinConfig};
|
|
|
|
use std::env::set_current_dir;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use crate::error::ShellError;
|
|
|
|
|
|
|
@ -7,11 +8,17 @@ pub struct Cd; |
|
|
|
|
|
|
|
impl Builtin for Cd {
|
|
|
|
fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
|
|
|
|
let Some(first) = args.get(0) else {
|
|
|
|
return Err(ShellError::ExecuteFailure("no argument given".to_string()))
|
|
|
|
let dir = match args.get(0) {
|
|
|
|
Some(first) => Path::new(first).to_path_buf(),
|
|
|
|
None => {
|
|
|
|
let Some(home) = dirs::home_dir() else {
|
|
|
|
return Ok(())
|
|
|
|
};
|
|
|
|
home
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
set_current_dir(first).map_err(|e| ShellError::ExecuteFailure(e.to_string()))
|
|
|
|
set_current_dir(dir).map_err(|e| ShellError::ExecuteFailure(e.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|