MCPcopy Create free account
hub / github.com/aws/amazon-q-developer-cli / canonicalizes_path

Function canonicalizes_path

crates/chat-cli/src/util/paths.rs:154–173  ·  view source on GitHub ↗

Canonicalizes path given by expanding the path given

(os: &Os, path_as_str: &str)

Source from the content-addressed store, hash-verified

152
153/// Canonicalizes path given by expanding the path given
154pub fn canonicalizes_path(os: &Os, path_as_str: &str) -> Result<String> {
155 let context = |input: &str| Ok(os.env.get(input).ok());
156 let home_dir_fn = || os.env.home().map(|p| p.to_string_lossy().to_string());
157
158 let expanded = shellexpand::full_with_context(path_as_str, home_dir_fn, context)?;
159 let path_buf = if !expanded.starts_with("/") {
160 let current_dir = os.env.current_dir()?;
161 current_dir.join(expanded.as_ref() as &str)
162 } else {
163 PathBuf::from(expanded.as_ref() as &str)
164 };
165
166 match path_buf.canonicalize() {
167 Ok(normalized) => Ok(normalized.as_path().to_string_lossy().to_string()),
168 Err(_) => {
169 let normalized = normalize_path(&path_buf);
170 Ok(normalized.to_string_lossy().to_string())
171 },
172 }
173}
174
175/// Manually normalize a path by resolving . and .. components
176fn normalize_path(path: &std::path::Path) -> std::path::PathBuf {

Callers 2

eval_permMethod · 0.85
eval_permMethod · 0.85

Calls 9

mapMethod · 0.80
to_stringMethod · 0.80
current_dirMethod · 0.80
joinMethod · 0.80
canonicalizeMethod · 0.80
normalize_pathFunction · 0.70
getMethod · 0.45
homeMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected