| Total Lines | 46 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package cmd |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "fmt" |
||
| 5 | "io" |
||
| 6 | |||
| 7 | "github.com/pkg/errors" |
||
| 8 | "github.com/spf13/cobra" |
||
| 9 | "github.com/stefanoj3/dirstalk/pkg/common" |
||
| 10 | "github.com/stefanoj3/dirstalk/pkg/result" |
||
| 11 | "github.com/stefanoj3/dirstalk/pkg/scan/summarizer/tree" |
||
| 12 | ) |
||
| 13 | |||
| 14 | func NewResultViewCommand(out io.Writer) *cobra.Command { |
||
| 15 | cmd := &cobra.Command{ |
||
| 16 | Use: "result.view", |
||
| 17 | Short: "Read a scan output file and render the folder tree", |
||
| 18 | RunE: buildResultViewCmd(out), |
||
| 19 | } |
||
| 20 | |||
| 21 | cmd.Flags().StringP( |
||
| 22 | flagResultViewResultFile, |
||
| 23 | flagResultViewResultFileShort, |
||
| 24 | "", |
||
| 25 | "result file to read", |
||
| 26 | ) |
||
| 27 | common.Must(cmd.MarkFlagFilename(flagResultViewResultFile)) |
||
| 28 | common.Must(cmd.MarkFlagRequired(flagResultViewResultFile)) |
||
| 29 | |||
| 30 | return cmd |
||
| 31 | } |
||
| 32 | |||
| 33 | func buildResultViewCmd(out io.Writer) func(cmd *cobra.Command, args []string) error { |
||
| 34 | return func(cmd *cobra.Command, args []string) error { |
||
| 35 | resultFilePath := cmd.Flag(flagResultViewResultFile).Value.String() |
||
| 36 | |||
| 37 | results, err := result.LoadResultsFromFile(resultFilePath) |
||
| 38 | if err != nil { |
||
| 39 | return errors.Wrapf(err, "failed to load results from %s", resultFilePath) |
||
| 40 | } |
||
| 41 | |||
| 42 | treeAsString := tree.NewResultTreeProducer().String(results) |
||
| 43 | |||
| 44 | _, err = fmt.Fprintln(out, treeAsString) |
||
| 45 | |||
| 46 | return errors.Wrap(err, "failed to print result tree") |
||
| 47 | } |
||
| 49 |