pkg/result/load.go   A
last analyzed

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 29
dl 0
loc 50
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B result.LoadResultsFromFile 0 39 7
1
package result
2
3
import (
4
	"bufio"
5
	"encoding/json"
6
	"os"
7
8
	"github.com/pkg/errors"
9
	"github.com/stefanoj3/dirstalk/pkg/scan"
10
)
11
12
func LoadResultsFromFile(resultFilePath string) ([]scan.Result, error) {
13
	file, err := os.Open(resultFilePath) // #nosec
14
	if err != nil {
15
		return nil, errors.Wrapf(err, "failed to open %s", resultFilePath)
16
	}
17
18
	defer file.Close() //nolint
19
20
	fileInfo, err := file.Stat()
21
	if err != nil {
22
		return nil, errors.Wrapf(err, "failed to read properties of %s", resultFilePath)
23
	}
24
25
	if fileInfo.IsDir() {
26
		return nil, errors.Errorf("`%s` is a directory, you need to specify a valid result file", resultFilePath)
27
	}
28
29
	fileScanner := bufio.NewScanner(file)
30
31
	lineCounter := 0
32
	results := make([]scan.Result, 0, 10)
33
34
	for fileScanner.Scan() {
35
		lineCounter++
36
37
		r := scan.Result{}
38
39
		if err := json.Unmarshal(fileScanner.Bytes(), &r); err != nil {
40
			return nil, errors.Wrapf(err, "unable to read line %d", lineCounter)
41
		}
42
43
		results = append(results, r)
44
	}
45
46
	if err := fileScanner.Err(); err != nil {
47
		return nil, errors.Wrap(err, "an error occurred while reading the result file")
48
	}
49
50
	return results, nil
51
}
52