mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
Find command now supports also directories
This commit is contained in:
@@ -1105,11 +1105,14 @@ mod tests {
|
|||||||
// Pwd
|
// Pwd
|
||||||
assert_eq!(client.pwd().ok().unwrap(), PathBuf::from("/"));
|
assert_eq!(client.pwd().ok().unwrap(), PathBuf::from("/"));
|
||||||
// Search for file (let's search for pop3-*.png); there should be 2
|
// Search for file (let's search for pop3-*.png); there should be 2
|
||||||
let search_res: Vec<FsFile> = client.find("pop3-*.png").ok().unwrap();
|
let search_res: Vec<FsEntry> = client.find("pop3-*.png").ok().unwrap();
|
||||||
assert_eq!(search_res.len(), 2);
|
assert_eq!(search_res.len(), 2);
|
||||||
// verify names
|
// verify names
|
||||||
assert_eq!(search_res[0].name.as_str(), "pop3-browser.png");
|
assert_eq!(search_res[0].get_name(), "pop3-browser.png");
|
||||||
assert_eq!(search_res[1].name.as_str(), "pop3-console-client.png");
|
assert_eq!(search_res[1].get_name(), "pop3-console-client.png");
|
||||||
|
// Search directory
|
||||||
|
let search_res: Vec<FsEntry> = client.find("pub").ok().unwrap();
|
||||||
|
assert_eq!(search_res.len(), 1);
|
||||||
// Disconnect
|
// Disconnect
|
||||||
assert!(client.disconnect().is_ok());
|
assert!(client.disconnect().is_ok());
|
||||||
// Verify err
|
// Verify err
|
||||||
|
|||||||
@@ -237,7 +237,7 @@ pub trait FileTransfer {
|
|||||||
///
|
///
|
||||||
/// Find files from current directory (in all subdirectories) whose name matches the provided search
|
/// Find files from current directory (in all subdirectories) whose name matches the provided search
|
||||||
/// Search supports wildcards ('?', '*')
|
/// Search supports wildcards ('?', '*')
|
||||||
fn find(&mut self, search: &str) -> Result<Vec<FsFile>, FileTransferError> {
|
fn find(&mut self, search: &str) -> Result<Vec<FsEntry>, FileTransferError> {
|
||||||
match self.is_connected() {
|
match self.is_connected() {
|
||||||
true => {
|
true => {
|
||||||
// Starting from current directory, iter dir
|
// Starting from current directory, iter dir
|
||||||
@@ -261,8 +261,8 @@ pub trait FileTransfer {
|
|||||||
&mut self,
|
&mut self,
|
||||||
dir: &Path,
|
dir: &Path,
|
||||||
filter: &WildMatch,
|
filter: &WildMatch,
|
||||||
) -> Result<Vec<FsFile>, FileTransferError> {
|
) -> Result<Vec<FsEntry>, FileTransferError> {
|
||||||
let mut drained: Vec<FsFile> = Vec::new();
|
let mut drained: Vec<FsEntry> = Vec::new();
|
||||||
// Scan directory
|
// Scan directory
|
||||||
match self.list_dir(dir) {
|
match self.list_dir(dir) {
|
||||||
Ok(entries) => {
|
Ok(entries) => {
|
||||||
@@ -275,6 +275,10 @@ pub trait FileTransfer {
|
|||||||
for entry in entries.iter() {
|
for entry in entries.iter() {
|
||||||
match entry {
|
match entry {
|
||||||
FsEntry::Directory(dir) => {
|
FsEntry::Directory(dir) => {
|
||||||
|
// If directory name, matches wildcard, push it to drained
|
||||||
|
if filter.is_match(dir.name.as_str()) {
|
||||||
|
drained.push(FsEntry::Directory(dir.clone()));
|
||||||
|
}
|
||||||
match self.iter_search(dir.abs_path.as_path(), filter) {
|
match self.iter_search(dir.abs_path.as_path(), filter) {
|
||||||
Ok(mut filtered) => drained.append(&mut filtered),
|
Ok(mut filtered) => drained.append(&mut filtered),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
@@ -282,7 +286,7 @@ pub trait FileTransfer {
|
|||||||
}
|
}
|
||||||
FsEntry::File(file) => {
|
FsEntry::File(file) => {
|
||||||
if filter.is_match(file.name.as_str()) {
|
if filter.is_match(file.name.as_str()) {
|
||||||
drained.push(file.clone());
|
drained.push(FsEntry::File(file.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1070,11 +1070,14 @@ mod tests {
|
|||||||
// Check session and scp
|
// Check session and scp
|
||||||
assert!(client.session.is_some());
|
assert!(client.session.is_some());
|
||||||
// Search for file (let's search for pop3-*.png); there should be 2
|
// Search for file (let's search for pop3-*.png); there should be 2
|
||||||
let search_res: Vec<FsFile> = client.find("pop3-*.png").ok().unwrap();
|
let search_res: Vec<FsEntry> = client.find("pop3-*.png").ok().unwrap();
|
||||||
assert_eq!(search_res.len(), 2);
|
assert_eq!(search_res.len(), 2);
|
||||||
// verify names
|
// verify names
|
||||||
assert_eq!(search_res[0].name.as_str(), "pop3-browser.png");
|
assert_eq!(search_res[0].get_name(), "pop3-browser.png");
|
||||||
assert_eq!(search_res[1].name.as_str(), "pop3-console-client.png");
|
assert_eq!(search_res[1].get_name(), "pop3-console-client.png");
|
||||||
|
// Search directory
|
||||||
|
let search_res: Vec<FsEntry> = client.find("pub").ok().unwrap();
|
||||||
|
assert_eq!(search_res.len(), 1);
|
||||||
// Disconnect
|
// Disconnect
|
||||||
assert!(client.disconnect().is_ok());
|
assert!(client.disconnect().is_ok());
|
||||||
// Verify err
|
// Verify err
|
||||||
|
|||||||
@@ -965,11 +965,14 @@ mod tests {
|
|||||||
// Check session and scp
|
// Check session and scp
|
||||||
assert!(client.session.is_some());
|
assert!(client.session.is_some());
|
||||||
// Search for file (let's search for pop3-*.png); there should be 2
|
// Search for file (let's search for pop3-*.png); there should be 2
|
||||||
let search_res: Vec<FsFile> = client.find("pop3-*.png").ok().unwrap();
|
let search_res: Vec<FsEntry> = client.find("pop3-*.png").ok().unwrap();
|
||||||
assert_eq!(search_res.len(), 2);
|
assert_eq!(search_res.len(), 2);
|
||||||
// verify names
|
// verify names
|
||||||
assert_eq!(search_res[0].name.as_str(), "pop3-browser.png");
|
assert_eq!(search_res[0].get_name(), "pop3-browser.png");
|
||||||
assert_eq!(search_res[1].name.as_str(), "pop3-console-client.png");
|
assert_eq!(search_res[1].get_name(), "pop3-console-client.png");
|
||||||
|
// Search directory
|
||||||
|
let search_res: Vec<FsEntry> = client.find("pub").ok().unwrap();
|
||||||
|
assert_eq!(search_res.len(), 1);
|
||||||
// Disconnect
|
// Disconnect
|
||||||
assert!(client.disconnect().is_ok());
|
assert!(client.disconnect().is_ok());
|
||||||
// Verify err
|
// Verify err
|
||||||
|
|||||||
@@ -547,7 +547,7 @@ impl Localhost {
|
|||||||
///
|
///
|
||||||
/// Find files matching `search` on localhost starting from current directory. Search supports recursive search of course.
|
/// Find files matching `search` on localhost starting from current directory. Search supports recursive search of course.
|
||||||
/// The `search` argument supports wilcards ('*', '?')
|
/// The `search` argument supports wilcards ('*', '?')
|
||||||
pub fn find(&self, search: &str) -> Result<Vec<FsFile>, HostError> {
|
pub fn find(&self, search: &str) -> Result<Vec<FsEntry>, HostError> {
|
||||||
self.iter_search(self.wrkdir.as_path(), &WildMatch::new(search))
|
self.iter_search(self.wrkdir.as_path(), &WildMatch::new(search))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,9 +558,9 @@ impl Localhost {
|
|||||||
/// Recursive call for `find` method.
|
/// Recursive call for `find` method.
|
||||||
/// Search in current directory for files which match `filter`.
|
/// Search in current directory for files which match `filter`.
|
||||||
/// If a directory is found in current directory, `iter_search` will be called using that dir as argument.
|
/// If a directory is found in current directory, `iter_search` will be called using that dir as argument.
|
||||||
fn iter_search(&self, dir: &Path, filter: &WildMatch) -> Result<Vec<FsFile>, HostError> {
|
fn iter_search(&self, dir: &Path, filter: &WildMatch) -> Result<Vec<FsEntry>, HostError> {
|
||||||
// Scan directory
|
// Scan directory
|
||||||
let mut drained: Vec<FsFile> = Vec::new();
|
let mut drained: Vec<FsEntry> = Vec::new();
|
||||||
match self.scan_dir(dir) {
|
match self.scan_dir(dir) {
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
Ok(entries) => {
|
Ok(entries) => {
|
||||||
@@ -574,6 +574,10 @@ impl Localhost {
|
|||||||
for entry in entries.iter() {
|
for entry in entries.iter() {
|
||||||
match entry {
|
match entry {
|
||||||
FsEntry::Directory(dir) => {
|
FsEntry::Directory(dir) => {
|
||||||
|
// If directory matches; push directory to drained
|
||||||
|
if filter.is_match(dir.name.as_str()) {
|
||||||
|
drained.push(FsEntry::Directory(dir.clone()));
|
||||||
|
}
|
||||||
match self.iter_search(dir.abs_path.as_path(), filter) {
|
match self.iter_search(dir.abs_path.as_path(), filter) {
|
||||||
Ok(mut filtered) => drained.append(&mut filtered),
|
Ok(mut filtered) => drained.append(&mut filtered),
|
||||||
Err(err) => return Err(err),
|
Err(err) => return Err(err),
|
||||||
@@ -581,7 +585,7 @@ impl Localhost {
|
|||||||
}
|
}
|
||||||
FsEntry::File(file) => {
|
FsEntry::File(file) => {
|
||||||
if filter.is_match(file.name.as_str()) {
|
if filter.is_match(file.name.as_str()) {
|
||||||
drained.push(file.clone());
|
drained.push(FsEntry::File(file.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1043,15 +1047,21 @@ mod tests {
|
|||||||
assert!(make_sample_file(subdir.as_path(), "omar.txt").is_ok());
|
assert!(make_sample_file(subdir.as_path(), "omar.txt").is_ok());
|
||||||
assert!(make_sample_file(subdir.as_path(), "errors.txt").is_ok());
|
assert!(make_sample_file(subdir.as_path(), "errors.txt").is_ok());
|
||||||
assert!(make_sample_file(subdir.as_path(), "screenshot.png").is_ok());
|
assert!(make_sample_file(subdir.as_path(), "screenshot.png").is_ok());
|
||||||
|
assert!(make_sample_file(subdir.as_path(), "examples.csv").is_ok());
|
||||||
let host: Localhost = Localhost::new(PathBuf::from(dir_path)).ok().unwrap();
|
let host: Localhost = Localhost::new(PathBuf::from(dir_path)).ok().unwrap();
|
||||||
// Find txt files
|
// Find txt files
|
||||||
let result: Vec<FsFile> = host.find("*.txt").ok().unwrap();
|
let result: Vec<FsEntry> = host.find("*.txt").ok().unwrap();
|
||||||
// There should be 3 entries
|
// There should be 3 entries
|
||||||
assert_eq!(result.len(), 3);
|
assert_eq!(result.len(), 3);
|
||||||
// Check names (they should be sorted alphabetically already; NOTE: examples/ comes before pippo.txt)
|
// Check names (they should be sorted alphabetically already; NOTE: examples/ comes before pippo.txt)
|
||||||
assert_eq!(result[0].name.as_str(), "errors.txt");
|
assert_eq!(result[0].get_name(), "errors.txt");
|
||||||
assert_eq!(result[1].name.as_str(), "omar.txt");
|
assert_eq!(result[1].get_name(), "omar.txt");
|
||||||
assert_eq!(result[2].name.as_str(), "pippo.txt");
|
assert_eq!(result[2].get_name(), "pippo.txt");
|
||||||
|
// Search for directory
|
||||||
|
let result: Vec<FsEntry> = host.find("examples*").ok().unwrap();
|
||||||
|
assert_eq!(result.len(), 2);
|
||||||
|
assert_eq!(result[0].get_name(), "examples");
|
||||||
|
assert_eq!(result[1].get_name(), "examples.csv");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user