1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use super::{column_paths_from_args, 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,
columns: Vec<ColumnPath>,
}
impl PathSubcommandArguments for PathExpandArguments {
fn get_column_paths(&self) -> &Vec<ColumnPath> {
&self.columns
}
}
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'),
)
.named(
"columns",
SyntaxShape::Table,
"Optionally operate by column path",
Some('c'),
)
}
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"),
columns: column_paths_from_args(&args)?,
});
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 path in a column",
example: "ls | path expand -c [ name ]",
result: None,
},
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 path in a column",
example: "ls | path expand -c [ name ]",
result: None,
},
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 {})
}
}