Completed
Push — master ( 015fff...2fb069 )
by Valentin
02:25
created

cmd/writecmd/operations/scanFiles.go   A

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 35
dl 0
loc 52
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C operations.ScanFiles 0 42 10
1
package operations
2
3
import (
4
	"github.com/vvval/go-metadata-scanner/cmd/scancmd"
5
	"github.com/vvval/go-metadata-scanner/config"
6
	"github.com/vvval/go-metadata-scanner/etool"
7
	"github.com/vvval/go-metadata-scanner/vars"
8
	"sync"
9
)
10
11
func ScanFiles(fileChunks []vars.Chunk, poolSize int) []vars.File {
12
	var done = make(chan struct{})
13
14
	var wg sync.WaitGroup
15
	var chunks = make(chan vars.Chunk)
16
	var scannedFiles = make(chan vars.File)
17
	scancmd.CreatePool(
18
		&wg,
19
		poolSize,
20
		chunks,
21
		func(files vars.Chunk) ([]byte, error) {
22
			return etool.Read(files, config.App.Fields())
23
		},
24
		func(data []byte) {
25
			for _, parsed := range etool.Parse(data) {
26
				scannedFiles <- parsed
27
			}
28
		},
29
	)
30
31
	for _, chunk := range fileChunks {
32
		wg.Add(1)
33
		chunks <- chunk
34
	}
35
36
	go func() {
37
		wg.Wait()
38
		close(scannedFiles)
39
		close(chunks)
40
		done <- struct{}{}
41
	}()
42
43
	var files []vars.File
44
45
	for file := range scannedFiles {
46
		files = append(files, file)
47
	}
48
49
	<-done
50
	close(done)
51
52
	return files
53
}
54