summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-07-04 20:53:35 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-07-04 21:04:25 -0400
commitf21c15b7458d581961b21561304f3dff2d251697 (patch)
treeb057cdaa0454511b600dd1be30c225472df33deb
parent8cefdc24500864993e66d28ba198070906161b67 (diff)
fix cd not working with relative paths
-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(())
}