Passed
Push — master ( bd2e0b...bf7a11 )
by Stefano
02:44
created

pkg/cmd/result_view.go   A

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 32
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A cmd.buildResultViewCmd 0 12 3
A cmd.NewResultViewCommand 0 24 3
1
package cmd
2
3
import (
4
	"io"
5
6
	"github.com/pkg/errors"
7
	"github.com/spf13/cobra"
8
	"github.com/stefanoj3/dirstalk/pkg/result"
9
	"github.com/stefanoj3/dirstalk/pkg/scan/summarizer/tree"
10
)
11
12
func NewResultViewCommand(out io.Writer) (*cobra.Command, error) {
0 ignored issues
show
introduced by
exported function NewResultViewCommand should have comment or be unexported
Loading history...
13
	cmd := &cobra.Command{
14
		Use:   "result.view",
15
		Short: "Read a scan output file and render the folder tree",
16
		RunE:  buildResultViewCmd(out),
17
	}
18
19
	cmd.Flags().StringP(
20
		flagResultFile,
21
		flagResultFileShort,
22
		"",
23
		"result file to read",
24
	)
25
	err := cmd.MarkFlagFilename(flagResultFile)
26
	if err != nil {
27
		return nil, err
28
	}
29
30
	err = cmd.MarkFlagRequired(flagResultFile)
31
	if err != nil {
32
		return nil, err
33
	}
34
35
	return cmd, nil
36
}
37
38
func buildResultViewCmd(out io.Writer) func(cmd *cobra.Command, args []string) error {
39
	return func(cmd *cobra.Command, args []string) error {
40
		resultFilePath := cmd.Flag(flagResultFile).Value.String()
41
42
		results, err := result.LoadResultsFromFile(resultFilePath)
43
		if err != nil {
44
			return errors.Wrapf(err, "failed to load results from %s", resultFilePath)
45
		}
46
47
		tree.NewResultTreePrinter().Print(results, out)
48
49
		return nil
50
	}
51
}
52