mirror of
https://github.com/nushell/nushell.git
synced 2025-07-08 10:27:47 +02:00
* Refactor path subcommand argument handling DefaultArguments are no longer passed to each subcommand. Instead, each subcommand has its own Path<xxx>Arguments. This means that it is no longer necessary to edit every single path subcommand source file when changing the arguments struct. * Add new path join subcommand Makes it easier to create new paths. It's just a wrapper around Rust's Path.join().
78 lines
2.1 KiB
Rust
78 lines
2.1 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 std::path::Path;
|
|
|
|
pub struct PathExists;
|
|
|
|
#[derive(Deserialize)]
|
|
struct PathExistsArguments {
|
|
rest: Vec<ColumnPath>,
|
|
}
|
|
|
|
impl PathSubcommandArguments for PathExistsArguments {
|
|
fn get_column_paths(&self) -> &Vec<ColumnPath> {
|
|
&self.rest
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for PathExists {
|
|
fn name(&self) -> &str {
|
|
"path exists"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("path exists")
|
|
.rest(SyntaxShape::ColumnPath, "Optionally operate by column path")
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Checks whether a path exists"
|
|
}
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let tag = args.call_info.name_tag.clone();
|
|
let (PathExistsArguments { rest }, input) = args.process().await?;
|
|
let args = Arc::new(PathExistsArguments { rest });
|
|
operate(input, &action, tag.span, args).await
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Check if file exists",
|
|
example: "echo 'C:\\Users\\joe\\todo.txt' | path exists",
|
|
result: Some(vec![Value::from(UntaggedValue::boolean(false))]),
|
|
}]
|
|
}
|
|
|
|
#[cfg(not(windows))]
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![Example {
|
|
description: "Check if file exists",
|
|
example: "echo '/home/joe/todo.txt' | path exists",
|
|
result: Some(vec![Value::from(UntaggedValue::boolean(false))]),
|
|
}]
|
|
}
|
|
}
|
|
|
|
fn action(path: &Path, _args: &PathExistsArguments) -> UntaggedValue {
|
|
UntaggedValue::boolean(path.exists())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::PathExists;
|
|
use super::ShellError;
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
use crate::examples::test as test_examples;
|
|
|
|
test_examples(PathExists {})
|
|
}
|
|
}
|