-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
83 lines (71 loc) · 2.1 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package sftpClient
import (
"fmt"
"log"
)
// FindRemoteFiles enumerates files in a remote directory
func (c *SFTPClient) FindRemoteFiles(path string) func() (r <-chan FileResponse) {
return func() (r <-chan FileResponse) {
responseChannel := make(chan FileResponse)
go func() {
defer close(responseChannel)
stats, err := c.client.Lstat(path)
if err != nil {
responseChannel <- FileResponse{File: "", Err: fmt.Errorf("Cannot STAT 'remote:%s': %v", path, err)}
return
}
if !stats.IsDir() {
responseChannel <- FileResponse{File: "", Err: fmt.Errorf("'remote:%s' is not a directory", path)}
return
}
var walker = *c.client.Walk(path)
for walker.Step() {
if err := walker.Err(); err != nil {
responseChannel <- FileResponse{File: "", Err: err}
continue
}
if walker.Path() == path {
continue
}
responseChannel <- FileResponse{File: walker.Path(), Err: nil}
}
}()
return responseChannel
}
}
func findRemoteFilesAggregator(functions []func() (r <-chan FileResponse)) (r <-chan FileResponse) {
responseChannel := make(chan FileResponse)
go func(functions []func() (r <-chan FileResponse)) {
for _, function := range functions {
intermediateChannel := function()
for response := range intermediateChannel {
responseChannel <- response
}
}
close(responseChannel)
}(functions)
return responseChannel
}
// FindAllRemoteFiles enumerates all remote files in multiple directories and their descendents
func (c *SFTPClient) FindAllRemoteFiles(paths []string) ([]string, error) {
var functions []func() (r <-chan FileResponse)
var files []string
for _, path := range paths {
functions = append(functions, c.FindRemoteFiles(path))
}
responseChannel := findRemoteFilesAggregator(functions)
encounteredErrors := 0
for response := range responseChannel {
if response.Err != nil {
encounteredErrors++
log.Println(response.Err)
}
if encounteredErrors == 0 {
files = append(files, response.File)
}
}
if encounteredErrors > 0 {
return nil, fmt.Errorf("Encountered %d errors", encounteredErrors)
}
return files, nil
}