Skip to content

Commit 8cabfc9

Browse files
committed
fix: Fix get_absolute_path logic for some cases
1 parent b0df664 commit 8cabfc9

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

cli/src/errors.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
fmt,
77
fmt::{Display, Formatter},
88
io::Error as IoError,
9-
path::PathBuf,
9+
path::{PathBuf, StripPrefixError},
1010
};
1111

1212
use tera::Error as TeraError;
@@ -80,6 +80,7 @@ pub enum DevrcError {
8080
content_checksum: String,
8181
},
8282
AnyhowError(anyhow::Error),
83+
HomeDirNotFound,
8384
}
8485

8586
impl Display for DevrcError {
@@ -145,3 +146,9 @@ impl From<DevrcPluginError> for DevrcError {
145146
DevrcError::PluginError(value)
146147
}
147148
}
149+
150+
impl From<StripPrefixError> for DevrcError {
151+
fn from(_: StripPrefixError) -> Self {
152+
DevrcError::RuntimeError
153+
}
154+
}

cli/src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ pub fn get_absolute_path(file: &PathBuf, base: Option<&PathBuf>) -> DevrcResult<
2828
return Ok(file.to_path_buf());
2929
}
3030

31+
if file.starts_with("~/") {
32+
return match dirs_next::home_dir() {
33+
Some(home) => {
34+
let right_part = file.strip_prefix("~/")?;
35+
Ok(Path::new(&home).join(right_part))
36+
}
37+
None => Err(DevrcError::HomeDirNotFound),
38+
};
39+
}
40+
3141
let file = if let Some(value) = base {
3242
let mut new_path = value.clone();
3343
if new_path.is_file() {

0 commit comments

Comments
 (0)