Better ls paths (#4612)

* Fix ls paths... again

* Fix ls paths... again

* Always expand paths inside of glob_from

* Expand in ls before we check for directory info
This commit is contained in:
JT 2022-02-23 10:54:47 -05:00 committed by GitHub
parent f507613b38
commit 676457acd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 14 deletions

View File

@ -29,11 +29,8 @@ impl Command for Ls {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("ls") Signature::build("ls")
.optional( // Using a string instead of a glob pattern shape so it won't auto-expand
"pattern", .optional("pattern", SyntaxShape::String, "the glob pattern to use")
SyntaxShape::GlobPattern,
"the glob pattern to use",
)
.switch("all", "Show hidden files", Some('a')) .switch("all", "Show hidden files", Some('a'))
.switch( .switch(
"long", "long",
@ -69,18 +66,22 @@ impl Command for Ls {
let ctrl_c = engine_state.ctrlc.clone(); let ctrl_c = engine_state.ctrlc.clone();
let call_span = call.head; let call_span = call.head;
let cwd = current_dir(engine_state, stack)?; let cwd = current_dir(engine_state, stack)?;
let pattern_arg = call.opt::<Spanned<String>>(engine_state, stack, 0)?;
let pattern_arg: Option<Spanned<String>> = call.opt(engine_state, stack, 0)?;
let (path, p_tag, absolute_path) = match pattern_arg { let (path, p_tag, absolute_path) = match pattern_arg {
Some(p) => { Some(p) => {
let p_tag = p.span; let p_tag = p.span;
let mut p = PathBuf::from(p.item); let mut p = PathBuf::from(p.item);
if p.is_dir() {
let expanded = nu_path::expand_path_with(&p, &cwd);
if expanded.is_dir() {
if permission_denied(&p) { if permission_denied(&p) {
#[cfg(unix)] #[cfg(unix)]
let error_msg = format!( let error_msg = format!(
"The permissions of {:o} do not allow access for this user", "The permissions of {:o} do not allow access for this user",
p.metadata() expanded
.metadata()
.expect( .expect(
"this shouldn't be called since we already know there is a dir" "this shouldn't be called since we already know there is a dir"
) )
@ -96,7 +97,7 @@ impl Command for Ls {
p_tag, p_tag,
)); ));
} }
if is_empty_dir(&p) { if is_empty_dir(&expanded) {
return Ok(Value::nothing(call_span).into_pipeline_data()); return Ok(Value::nothing(call_span).into_pipeline_data());
} }
p.push("*"); p.push("*");

View File

@ -25,11 +25,7 @@ pub fn glob_from(
ShellError, ShellError,
> { > {
let path = PathBuf::from(&pattern.item); let path = PathBuf::from(&pattern.item);
let path = if path.is_relative() { let path = expand_path_with(path, cwd);
expand_path_with(path, cwd)
} else {
path
};
let (prefix, pattern) = if path.to_string_lossy().contains('*') { let (prefix, pattern) = if path.to_string_lossy().contains('*') {
// Path is a glob pattern => do not check for existence // Path is a glob pattern => do not check for existence