Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 0 |
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) { |
||
|
|||
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 | } |
||
52 |