Passed
Push — master ( f028f1...4fb92b )
by Stefano
02:12
created

pkg/scan/output/saver.go   A

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A output.Saver.Save 0 12 3
A output.Saver.Close 0 6 2
A output.NewFileSaver 0 7 2
1
package output
2
3
import (
4
	"fmt"
5
	"io"
6
	"os"
7
8
	"github.com/pkg/errors"
9
	"github.com/stefanoj3/dirstalk/pkg/scan"
10
)
11
12
var (
13
	errNilWriteCloser = errors.New("Saver: writeCloser is nil")
14
)
15
16
func NewFileSaver(path string) (Saver, error) {
0 ignored issues
show
introduced by
exported function NewFileSaver should have comment or be unexported
Loading history...
17
	file, err := os.Create(path)
18
	if err != nil {
19
		return Saver{}, errors.Wrapf(err, "failed to create file `%s` for output", path)
20
	}
21
22
	return Saver{writeCloser: file}, nil
23
}
24
25
type Saver struct {
0 ignored issues
show
introduced by
exported type Saver should have comment or be unexported
Loading history...
26
	writeCloser io.WriteCloser
27
}
28
29
func (f Saver) Save(r scan.Result) error {
0 ignored issues
show
introduced by
exported method Saver.Save should have comment or be unexported
Loading history...
30
	if f.writeCloser == nil {
31
		return errNilWriteCloser
32
	}
33
34
	rawResult, err := convertResultToRawData(r)
35
	if err != nil {
36
		return errors.Wrap(err, "Saver: failed to convert result")
37
	}
38
39
	_, err = fmt.Fprintln(f.writeCloser, string(rawResult))
40
	return errors.Wrapf(err, "Saver: failed to write result: %s", rawResult)
41
}
42
43
func (f Saver) Close() error {
0 ignored issues
show
introduced by
exported method Saver.Close should have comment or be unexported
Loading history...
44
	if f.writeCloser == nil {
45
		return errNilWriteCloser
46
	}
47
48
	return f.writeCloser.Close()
49
}
50