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

summarizer.*ResultSummarizer.printTree   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 summarizer
2
3
import (
4
	"fmt"
5
	"net/http"
6
	"sort"
7
	"sync"
8
9
	"github.com/sirupsen/logrus"
10
	"github.com/stefanoj3/dirstalk/pkg/scan"
11
	"github.com/stefanoj3/dirstalk/pkg/scan/summarizer/tree"
12
)
13
14
const (
15
	breakingText = "Found something breaking"
16
	foundText    = "Found"
17
)
18
19
func NewResultSummarizer(treePrinter ResultTreePrinter, logger *logrus.Logger) *ResultSummarizer {
0 ignored issues
show
introduced by
exported function NewResultSummarizer should have comment or be unexported
Loading history...
20
	return &ResultSummarizer{
21
		treePrinter: treePrinter,
22
		logger:      logger,
23
		resultMap:   make(map[string]struct{}),
24
	}
25
}
26
27
type ResultSummarizer struct {
0 ignored issues
show
introduced by
exported type ResultSummarizer should have comment or be unexported
Loading history...
28
	treePrinter ResultTreePrinter
29
	logger      *logrus.Logger
30
	results     []scan.Result
31
	resultMap   map[string]struct{}
32
	mux         sync.RWMutex
33
}
34
35
func (s *ResultSummarizer) Add(result scan.Result) {
0 ignored issues
show
introduced by
exported method ResultSummarizer.Add should have comment or be unexported
Loading history...
36
	s.mux.Lock()
37
	defer s.mux.Unlock()
38
39
	key := keyForResult(result)
40
	_, found := s.resultMap[key]
41
	if found {
42
		return
43
	}
44
45
	s.log(result)
46
47
	s.resultMap[key] = struct{}{}
48
49
	s.results = append(s.results, result)
50
}
51
52
func (s *ResultSummarizer) Summarize() {
0 ignored issues
show
introduced by
exported method ResultSummarizer.Summarize should have comment or be unexported
Loading history...
53
	s.mux.Lock()
54
	defer s.mux.Unlock()
55
56
	sort.Slice(s.results, func(i, j int) bool {
57
		return s.results[i].Target.Path < s.results[j].Target.Path
58
	})
59
60
	s.printSummary()
61
	s.printTree()
62
63
	for _, r := range s.results {
64
		_, _ = fmt.Fprintln(
65
			s.logger.Out,
66
			fmt.Sprintf(
67
				"%s [%d] [%s]",
68
				r.URL.String(),
69
				r.StatusCode,
70
				r.Target.Method,
71
			),
72
		)
73
	}
74
}
75
76
func (s *ResultSummarizer) printSummary() {
77
	_, _ = fmt.Fprintln(
78
		s.logger.Out,
79
		fmt.Sprintf("%d results found", len(s.results)),
80
	)
81
}
82
83
func (s *ResultSummarizer) printTree() {
84
	tree.NewResultTreePrinter().Print(s.results, s.logger.Out)
85
}
86
87
func (s *ResultSummarizer) log(result scan.Result) {
88
	statusCode := result.StatusCode
89
90
	l := s.logger.WithFields(logrus.Fields{
91
		"status-code": statusCode,
92
		"method":      result.Target.Method,
93
		"url":         result.URL.String(),
94
	})
95
96
	if statusCode >= http.StatusInternalServerError {
97
		l.Warn(breakingText)
98
	} else {
99
		l.Info(foundText)
100
	}
101
}
102
103
func keyForResult(result scan.Result) string {
104
	return fmt.Sprintf("%s~%s", result.URL.String(), result.Target.Method)
105
}
106