Completed
Push — master ( 082c99...1af50e )
by Valentin
02:15
created

cmd/writecmd/worker.go   A

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 27
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A writecmd.findScanned 0 8 4
A writecmd.Work 0 25 5
1
package writecmd
2
3
import (
4
	"github.com/vvval/go-metadata-scanner/etool"
5
	"github.com/vvval/go-metadata-scanner/util"
6
	"github.com/vvval/go-metadata-scanner/util/scan"
7
	"github.com/vvval/go-metadata-scanner/vars"
8
)
9
10
func Work(job *Job, append, originals bool, extensions []string, files *vars.Chunk, filesData *[]vars.File) ([]byte, error) {
11
	filename, found := scan.Candidates(job.Filename(), files, extensions)
12
	if !found {
13
		return []byte{}, NoFileErr
14
	}
15
16
	if append {
17
		if file, found := findScanned(filename, filesData); found {
18
			job.MergePayload(file.Tags())
19
		}
20
	}
21
22
	if !job.HasPayload() {
23
		return []byte{}, SkipFileErr
24
	}
25
26
	payload := job.Payload()
27
	result, err := etool.Write(
28
		filename,
29
		payload.Tags(),
30
		payload.UseSeparator(),
31
		originals,
32
	)
33
34
	return result, err
35
}
36
37
func findScanned(filename string, files *[]vars.File) (vars.File, bool) {
38
	for _, file := range *files {
39
		if util.PathsEqual(file.Filename(), filename) {
40
			return file, true
41
		}
42
	}
43
44
	return vars.File{}, false
45
}
46