Passed
Pull Request — master (#71)
by Stefano
02:39
created

tree.NewResultTreePrinter   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
package tree
2
3
import (
4
	"fmt"
5
	"io"
6
	"sort"
7
	"strings"
8
9
	gotree "github.com/DiSiqueira/GoTree"
10
	"github.com/stefanoj3/dirstalk/pkg/scan"
11
)
12
13
const (
14
	breakingText = "Found something breaking"
15
	foundText    = "Found"
16
)
17
18
func NewResultTreePrinter() ResultTreePrinter {
0 ignored issues
show
introduced by
exported function NewResultTreePrinter should have comment or be unexported
Loading history...
19
	return ResultTreePrinter{}
20
}
21
22
type ResultTreePrinter struct{}
0 ignored issues
show
introduced by
exported type ResultTreePrinter should have comment or be unexported
Loading history...
23
24
func (s ResultTreePrinter) Print(results []scan.Result, out io.Writer) {
0 ignored issues
show
introduced by
exported method ResultTreePrinter.Print should have comment or be unexported
Loading history...
25
	sort.Slice(results, func(i, j int) bool {
26
		return results[i].Target.Path < results[j].Target.Path
27
	})
28
29
	root := gotree.New("/")
30
31
	// TODO: improve efficiency
32
	for _, r := range results {
33
		currentBranch := root
34
35
		parts := strings.Split(r.URL.Path, "/")
36
		for _, p := range parts {
37
			if len(p) == 0 {
38
				continue
39
			}
40
41
			found := false
42
43
			for _, item := range currentBranch.Items() {
44
				if item.Text() != p {
45
					continue
46
				}
47
48
				currentBranch = item
49
				found = true
50
				break
51
			}
52
53
			if found {
54
				continue
55
			}
56
57
			newTree := gotree.New(p)
58
			currentBranch.AddTree(newTree)
59
			currentBranch = newTree
60
		}
61
	}
62
63
	_, _ = fmt.Fprintln(out, root.Print())
64
}
65