nushell/crates/nu-command/src/commands/path/expand.rs
Jakub Žádník cc5c4d38bb
Small fixes and refactors to paths & source command (#3998)
* Expand path when converting value -> PathBuf

Also includes Tagged<PathBuf>.

Fixes #3605

* Expand path for PATH env. variable

Fixes #1834

* Remove leftover Cows after nu-path refactor

There were some unnecessary Cow conversions leftover from the old
nu-path implementation.

* Use canonicalize in source command; Improve errors

Previously, `source` used `expand_path()` which does not follow
symlinks.

As a follow up, I improved the source error messages so they now tell
why the source file could not be canonicalized or read into string.
2021-09-12 02:36:14 +03:00

124 lines
3.5 KiB
Rust

use super::{operate, PathSubcommandArguments};
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_path::{canonicalize, expand_path};
use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Span;
use std::path::Path;
pub struct PathExpand;
struct PathExpandArguments {
strict: bool,
rest: Vec<ColumnPath>,
}
impl PathSubcommandArguments for PathExpandArguments {
fn get_column_paths(&self) -> &Vec<ColumnPath> {
&self.rest
}
}
impl WholeStreamCommand for PathExpand {
fn name(&self) -> &str {
"path expand"
}
fn signature(&self) -> Signature {
Signature::build("path expand")
.switch(
"strict",
"Throw an error if the path could not be expanded",
Some('s'),
)
.rest(
"rest",
SyntaxShape::ColumnPath,
"Optionally operate by column path",
)
}
fn usage(&self) -> &str {
"Try to expand a path to its absolute form"
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let cmd_args = Arc::new(PathExpandArguments {
strict: args.has_flag("strict"),
rest: args.rest(0)?,
});
Ok(operate(args.input, &action, tag.span, cmd_args))
}
#[cfg(windows)]
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Expand an absolute path",
example: r"'C:\Users\joe\foo\..\bar' | path expand",
result: Some(vec![
UntaggedValue::filepath(r"C:\Users\joe\bar").into_value(Span::new(0, 25))
]),
},
Example {
description: "Expand a relative path",
example: r"'foo\..\bar' | path expand",
result: Some(vec![
UntaggedValue::filepath("bar").into_value(Span::new(0, 12))
]),
},
]
}
#[cfg(not(windows))]
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Expand an absolute path",
example: "'/home/joe/foo/../bar' | path expand",
result: Some(vec![
UntaggedValue::filepath("/home/joe/bar").into_value(Span::new(0, 22))
]),
},
Example {
description: "Expand a relative path",
example: "'foo/../bar' | path expand",
result: Some(vec![
UntaggedValue::filepath("bar").into_value(Span::new(0, 12))
]),
},
]
}
}
fn action(path: &Path, tag: Tag, args: &PathExpandArguments) -> Value {
if let Ok(p) = canonicalize(path) {
UntaggedValue::filepath(p).into_value(tag)
} else if args.strict {
Value::error(ShellError::labeled_error(
"Could not expand path",
"could not be expanded (path might not exist, non-final \
component is not a directory, or other cause)",
tag.span,
))
} else {
UntaggedValue::filepath(expand_path(path)).into_value(tag)
}
}
#[cfg(test)]
mod tests {
use super::PathExpand;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(PathExpand {})
}
}