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

operations.ScanFiles   C

Complexity

Conditions 10

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 28
nop 2
dl 0
loc 42
rs 5.9999
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like operations.ScanFiles often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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