* Add new path parse subcommand This includes a slight refactor to all the path subcommand `action()` functions. * Remove filestem and extension; Fix example * Add additional description to path parse * Put join arg behind flag; Fix missing import (Win) * Fix error when column path is passed as arg * Add structured path joining Structured path is implicitly joined at every patch subcommand call. * Fix existing path join tests; Fix rustfmt * Remove redundant 'static lifetime (clippy) * Add initial impl of path split subcommand * Add ability to join path from parts * Fix wrong results in path split examples * Fix remaining asyncs after engine change * Do not wrap split path parts into table When the input is just a list of values, the `path split` command will split each value directly into the output stream, similar to `split-row`. Column path--specified values are still wrapped into a table so they can still be used to replace table fields. * Join list of values instead of going one-by-one When `path join` encounters a list of values, it attempts to join them, instead of going one-by-one like the rest of the path commands. You can still `each { echo $it | path join }` to join them one-by-one, if the values are, e.g., tables. Now, the behavior of `path split` and `path join` should match the `split-row` and `str collect` counterparts and should hopefully align better with user's expectations. * Make sure path join detects structured path * Fix panic on empty input stream Also, doesn't collect input into vector unnecessarily. * Fix path join not appending value * Remove argument serialization * Make better errors; Misc refactor * OsStr -> String encoding is now lossy, instead of throwing an error * The consequence is action() now always returns Value instead of Result * Removed redundant handle_value() call in `path join` * Fix possible incorrect error detection in `path split` * Applied rustfmt + clippy * Add more usage, examples & test; Fix type error The 'parent' column was required to be a path but didn't work with string. * Add more help & examples; Maybe fix Windows error * Refactor operate function Reducing code repetition * Review usages and examples * Add the option to manually specify the extension * Add more tests; Fix failures on Windows * Move path commands to engine-p * Small refactor
114 lines
3.3 KiB
Rust
114 lines
3.3 KiB
Rust
use super::{operate, PathSubcommandArguments};
|
|
use crate::prelude::*;
|
|
use nu_engine::WholeStreamCommand;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value};
|
|
use nu_source::Tagged;
|
|
use std::path::Path;
|
|
|
|
pub struct PathBasename;
|
|
|
|
struct PathBasenameArguments {
|
|
rest: Vec<ColumnPath>,
|
|
replace: Option<Tagged<String>>,
|
|
}
|
|
|
|
impl PathSubcommandArguments for PathBasenameArguments {
|
|
fn get_column_paths(&self) -> &Vec<ColumnPath> {
|
|
&self.rest
|
|
}
|
|
}
|
|
|
|
impl WholeStreamCommand for PathBasename {
|
|
fn name(&self) -> &str {
|
|
"path basename"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("path basename")
|
|
.rest(SyntaxShape::ColumnPath, "Optionally operate by column path")
|
|
.named(
|
|
"replace",
|
|
SyntaxShape::String,
|
|
"Return original path with basename replaced by this string",
|
|
Some('r'),
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Get the final component of a path"
|
|
}
|
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let tag = args.call_info.name_tag.clone();
|
|
let args = args.evaluate_once()?;
|
|
let cmd_args = Arc::new(PathBasenameArguments {
|
|
rest: args.rest_args()?,
|
|
replace: args.get_flag("replace")?,
|
|
});
|
|
|
|
Ok(operate(args.input, &action, tag.span, cmd_args))
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Get basename of a path",
|
|
example: "echo 'C:\\Users\\joe\\test.txt' | path basename",
|
|
result: Some(vec![Value::from("test.txt")]),
|
|
},
|
|
Example {
|
|
description: "Replace basename of a path",
|
|
example: "echo 'C:\\Users\\joe\\test.txt' | path basename -r 'spam.png'",
|
|
result: Some(vec![Value::from(UntaggedValue::filepath(
|
|
"C:\\Users\\joe\\spam.png",
|
|
))]),
|
|
},
|
|
]
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Get basename of a path",
|
|
example: "echo '/home/joe/test.txt' | path basename",
|
|
result: Some(vec![Value::from("test.txt")]),
|
|
},
|
|
Example {
|
|
description: "Replace basename of a path",
|
|
example: "echo '/home/joe/test.txt' | path basename -r 'spam.png'",
|
|
result: Some(vec![Value::from(UntaggedValue::filepath(
|
|
"/home/joe/spam.png",
|
|
))]),
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
fn action(path: &Path, tag: Tag, args: &PathBasenameArguments) -> Value {
|
|
let untagged = match args.replace {
|
|
Some(ref basename) => UntaggedValue::filepath(path.with_file_name(&basename.item)),
|
|
None => UntaggedValue::string(match path.file_name() {
|
|
Some(filename) => filename.to_string_lossy(),
|
|
None => "".into(),
|
|
}),
|
|
};
|
|
|
|
untagged.into_value(tag)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::PathBasename;
|
|
use super::ShellError;
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
use crate::examples::test as test_examples;
|
|
|
|
test_examples(PathBasename {})
|
|
}
|
|
}
|