mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
path type
error and not found changes (#13007)
# Description Instead of an empty string, this PR changes `path type` to return null if the path does not exist. If some other IO error is encountered, then that error is bubbled up instead of treating it as a "not found" case. # User-Facing Changes - `path type` will now return null instead of an empty string, which is technically a breaking change. In most cases though, I think this shouldn't affect the behavior of scripts too much. - `path type` can now error instead of returning an empty string if some other IO error besides a "not found" error occurs. Since this PR introduces breaking changes, it should be merged after the 0.94.1 patch.
This commit is contained in:
parent
a55a48529d
commit
944d941dec
@ -1,7 +1,10 @@
|
|||||||
use super::PathSubcommandArguments;
|
use super::PathSubcommandArguments;
|
||||||
use nu_engine::command_prelude::*;
|
use nu_engine::command_prelude::*;
|
||||||
use nu_protocol::engine::StateWorkingSet;
|
use nu_protocol::engine::StateWorkingSet;
|
||||||
use std::path::{Path, PathBuf};
|
use std::{
|
||||||
|
io,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
pwd: PathBuf,
|
pwd: PathBuf,
|
||||||
@ -36,7 +39,7 @@ impl Command for SubCommand {
|
|||||||
|
|
||||||
fn extra_usage(&self) -> &str {
|
fn extra_usage(&self) -> &str {
|
||||||
r#"This checks the file system to confirm the path's object type.
|
r#"This checks the file system to confirm the path's object type.
|
||||||
If nothing is found, an empty string will be returned."#
|
If the path does not exist, null will be returned."#
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_const(&self) -> bool {
|
fn is_const(&self) -> bool {
|
||||||
@ -104,9 +107,11 @@ If nothing is found, an empty string will be returned."#
|
|||||||
|
|
||||||
fn path_type(path: &Path, span: Span, args: &Arguments) -> Value {
|
fn path_type(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||||
let path = nu_path::expand_path_with(path, &args.pwd, true);
|
let path = nu_path::expand_path_with(path, &args.pwd, true);
|
||||||
let meta = path.symlink_metadata();
|
match path.symlink_metadata() {
|
||||||
let ty = meta.as_ref().map(get_file_type).unwrap_or("");
|
Ok(metadata) => Value::string(get_file_type(&metadata), span),
|
||||||
Value::string(ty, span)
|
Err(err) if err.kind() == io::ErrorKind::NotFound => Value::nothing(span),
|
||||||
|
Err(err) => Value::error(err.into_spanned(span).into(), span),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_file_type(md: &std::fs::Metadata) -> &str {
|
fn get_file_type(md: &std::fs::Metadata) -> &str {
|
||||||
|
Loading…
Reference in New Issue
Block a user