2021-03-04 08:04:56 +01:00
|
|
|
use super::{operate, PathSubcommandArguments};
|
2020-07-27 04:12:07 +02:00
|
|
|
use crate::prelude::*;
|
2021-01-10 03:50:49 +01:00
|
|
|
use nu_engine::filesystem::filesystem_shell::get_file_type;
|
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-07-27 04:12:07 +02:00
|
|
|
use nu_errors::ShellError;
|
2020-11-24 10:18:38 +01:00
|
|
|
use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value};
|
2020-07-27 04:12:07 +02:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
pub struct PathType;
|
|
|
|
|
2020-11-24 10:18:38 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct PathTypeArguments {
|
|
|
|
rest: Vec<ColumnPath>,
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:04:56 +01:00
|
|
|
impl PathSubcommandArguments for PathTypeArguments {
|
|
|
|
fn get_column_paths(&self) -> &Vec<ColumnPath> {
|
|
|
|
&self.rest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 04:12:07 +02:00
|
|
|
impl WholeStreamCommand for PathType {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"path type"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-11-24 10:18:38 +01:00
|
|
|
Signature::build("path type")
|
|
|
|
.rest(SyntaxShape::ColumnPath, "Optionally operate by column path")
|
2020-07-27 04:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-11-24 10:18:38 +01:00
|
|
|
"Gives the type of the object a path refers to (e.g., file, dir, symlink)"
|
2020-07-27 04:12:07 +02:00
|
|
|
}
|
|
|
|
|
2021-04-06 18:19:43 +02:00
|
|
|
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2020-07-30 20:51:14 +02:00
|
|
|
let tag = args.call_info.name_tag.clone();
|
2021-04-06 18:19:43 +02:00
|
|
|
let (PathTypeArguments { rest }, input) = args.process()?;
|
2021-03-04 08:04:56 +01:00
|
|
|
let args = Arc::new(PathTypeArguments { rest });
|
2021-04-06 18:19:43 +02:00
|
|
|
Ok(operate(input, &action, tag.span, args))
|
2020-07-27 04:12:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Show type of a filepath",
|
|
|
|
example: "echo '.' | path type",
|
|
|
|
result: Some(vec![Value::from("Dir")]),
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:04:56 +01:00
|
|
|
fn action(path: &Path, _args: &PathTypeArguments) -> UntaggedValue {
|
2020-07-27 04:12:07 +02:00
|
|
|
let meta = std::fs::symlink_metadata(path);
|
|
|
|
UntaggedValue::string(match &meta {
|
|
|
|
Ok(md) => get_file_type(md),
|
|
|
|
Err(_) => "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::PathType;
|
2020-10-03 16:06:02 +02:00
|
|
|
use super::ShellError;
|
2020-07-27 04:12:07 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-10-03 16:06:02 +02:00
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-07-27 04:12:07 +02:00
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2021-02-12 11:13:14 +01:00
|
|
|
test_examples(PathType {})
|
2020-07-27 04:12:07 +02:00
|
|
|
}
|
|
|
|
}
|