summaryrefslogtreecommitdiffstats
path: root/src/commands/change_directory.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/commands/change_directory.rs')
-rw-r--r--src/commands/change_directory.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs
index 0499011..b665d0c 100644
--- a/src/commands/change_directory.rs
+++ b/src/commands/change_directory.rs
@@ -1,7 +1,8 @@
+use std::io;
use std::path;
use crate::context::AppContext;
-use crate::error::JoshutoResult;
+use crate::error::{JoshutoError, JoshutoResult};
use crate::history::DirectoryHistory;
pub fn cd(path: &path::Path, context: &mut AppContext) -> std::io::Result<()> {
@@ -23,6 +24,31 @@ fn _change_directory(path: &path::Path, context: &mut AppContext) -> std::io::Re
}
pub fn change_directory(context: &mut AppContext, path: &path::Path) -> JoshutoResult<()> {
- _change_directory(path, context)?;
+ let new_cwd = if path.is_absolute() {
+ let new_cwd = path.canonicalize()?;
+ if !new_cwd.exists() {
+ let err = io::Error::new(
+ io::ErrorKind::NotFound,
+ "No such file or directory".to_string(),
+ );
+ let err = JoshutoError::from(err);
+ return Err(err);
+ }
+ new_cwd
+ } else {
+ let mut new_cwd = std::env::current_dir()?;
+ new_cwd.push(path.canonicalize()?);
+ if !new_cwd.exists() {
+ let err = io::Error::new(
+ io::ErrorKind::NotFound,
+ "No such file or directory".to_string(),
+ );
+ let err = JoshutoError::from(err);
+ return Err(err);
+ }
+ new_cwd
+ };
+
+ _change_directory(new_cwd.as_path(), context)?;
Ok(())
}