Skip to content

Commit

Permalink
Add overloads for listFiles and listDirectories to filter return list (
Browse files Browse the repository at this point in the history
…#555)

Closes #429
  • Loading branch information
chrisrohr authored Apr 26, 2021
1 parent 0bb5365 commit 4d1199c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/org/kiwiproject/jsch/SftpTransfers.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,19 @@ public List<String> listFiles(Path remotePath) {
return listRemoteItems(remotePath, file -> !file.getAttrs().isDir());
}

/**
* Returns a list of files that exist in the given path and matching the given file filter on the remote server.
*
* @param remotePath path on the remote server to list files
* @param fileFilter predicate to filter file names being returned
* @return a list of filenames that exist in the given path matching the given file filter
*/
public List<String> listFiles(Path remotePath, Predicate<String> fileFilter) {
return listFiles(remotePath).stream()
.filter(fileFilter)
.collect(toList());
}

/**
* Returns a list of directories that exist in the given path on the remote server.
*
Expand All @@ -229,6 +242,20 @@ public List<String> listDirectories(Path remotePath) {
return listRemoteItems(remotePath, file -> file.getAttrs().isDir());
}

/**
* Returns a list of directories that exist in the given path and matching the given directory filter on the
* remote server.
*
* @param remotePath path on the remote server to list files
* @param dirFilter predicate to filter directory names being returned
* @return a list of directories that exist in the given path matching the given directory filter
*/
public List<String> listDirectories(Path remotePath, Predicate<String> dirFilter) {
return listDirectories(remotePath).stream()
.filter(dirFilter)
.collect(toList());
}

private List<String> listRemoteItems(Path remotePath, Predicate<ChannelSftp.LsEntry> filterFunction) {
return connector.runCommandWithResponse(channel -> ls(channel, remotePath)
.stream()
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/kiwiproject/jsch/SftpTransfersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ void shouldReturnAListOfFilesInThePath() throws SftpException {
assertThat(files).hasSize(2).contains("test-file-1.txt", "test-file-2.txt");
}

@Test
void shouldReturnAListOfFilesInThePath_FilteredByPredicate() throws SftpException {
var entries = buildEntries();
when(channelSftp.ls(config.getRemoteBasePath())).thenReturn(entries);

var files = sftp.listFiles(Path.of(config.getRemoteBasePath()), file -> file.contains("file-2"));
assertThat(files).hasSize(1).containsOnly("test-file-2.txt");

files = sftp.listFiles(Path.of(config.getRemoteBasePath()), file -> file.contains("file-1"));
assertThat(files).hasSize(1).containsOnly("test-file-1.txt");
}

@Test
void shouldReturnAListOfDirectoriesInThePath() throws SftpException {
var entries = buildEntries();
Expand All @@ -375,6 +387,18 @@ void shouldReturnAListOfDirectoriesInThePath() throws SftpException {
assertThat(files).hasSize(1).contains("test-dir");
}

@Test
void shouldReturnAListOfDirectoriesInThePath_FilteredByPredicate() throws SftpException {
var entries = buildEntries();
when(channelSftp.ls(config.getRemoteBasePath())).thenReturn(entries);

var files = sftp.listDirectories(Path.of(config.getRemoteBasePath()), dir -> dir.equals("test-dir"));
assertThat(files).hasSize(1).contains("test-dir");

files = sftp.listDirectories(Path.of(config.getRemoteBasePath()), dir -> dir.equals("foo-dir"));
assertThat(files).isEmpty();
}

@Test
void deleteRemoteFileShouldRemoveFile() throws SftpException {
sftp.deleteRemoteFile(Path.of(config.getRemoteBasePath()), "test-file-to-pull.txt");
Expand Down

0 comments on commit 4d1199c

Please sign in to comment.