From 1d0ed7e95760cf70f4851d9b16e98d773b23b530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Tue, 3 Sep 2019 05:17:44 -0500 Subject: [PATCH 1/2] ls lists contents of value entered with or without path given. --- src/shell/value_shell.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/shell/value_shell.rs b/src/shell/value_shell.rs index 1f6d6c796..8d6c7b6fd 100644 --- a/src/shell/value_shell.rs +++ b/src/shell/value_shell.rs @@ -8,7 +8,7 @@ use crate::prelude::*; use crate::shell::shell::Shell; use crate::utils::ValueStructure; use std::ffi::OsStr; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; #[derive(Clone, Debug)] pub struct ValueShell { @@ -23,9 +23,10 @@ impl ValueShell { value, } } - fn members(&self) -> VecDeque> { + + fn members_under(&self, path: &Path) -> VecDeque> { let mut shell_entries = VecDeque::new(); - let full_path = PathBuf::from(&self.path); + let full_path = path.to_path_buf(); let mut viewed = self.value.clone(); let sep_string = std::path::MAIN_SEPARATOR.to_string(); let sep = OsStr::new(&sep_string); @@ -54,7 +55,11 @@ impl ValueShell { } } - shell_entries + shell_entries + } + + fn members(&self) -> VecDeque> { + self.members_under(Path::new(".")) } } @@ -74,9 +79,16 @@ impl Shell for ValueShell { dirs::home_dir() } - fn ls(&self, _args: EvaluatedWholeStreamCommandArgs) -> Result { + fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { + let mut full_path = PathBuf::from(self.path()); + + match &args.nth(0) { + Some(value) => full_path.push(Path::new(&value.as_path()?)), + _ => {} + } + Ok(self - .members() + .members_under(full_path.as_path()) .map(|x| ReturnSuccess::value(x)) .to_output_stream()) } From 3256b7adb3c2786afdaa19f313330f2602b8233c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Tue, 3 Sep 2019 05:24:04 -0500 Subject: [PATCH 2/2] if path to ls given that does not exist, report the error. --- src/shell/value_shell.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/shell/value_shell.rs b/src/shell/value_shell.rs index 8d6c7b6fd..26dab3583 100644 --- a/src/shell/value_shell.rs +++ b/src/shell/value_shell.rs @@ -82,11 +82,32 @@ impl Shell for ValueShell { fn ls(&self, args: EvaluatedWholeStreamCommandArgs) -> Result { let mut full_path = PathBuf::from(self.path()); - match &args.nth(0) { + let target = args.nth(0); + + match target { Some(value) => full_path.push(Path::new(&value.as_path()?)), _ => {} } + let mut value_system = ValueStructure::new(); + value_system.walk_decorate(&self.value)?; + + if !value_system.exists(&full_path) { + if let Some(target) = target { + return Err(ShellError::labeled_error( + "Can not list entries inside", + "No such path exists", + target.span(), + )); + } + + return Err(ShellError::labeled_error( + "Can not list entries inside", + "No such path exists", + args.call_info.name_span, + )); + } + Ok(self .members_under(full_path.as_path()) .map(|x| ReturnSuccess::value(x))