-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
section.go
81 lines (71 loc) 路 1.78 KB
/
section.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of K9s
package xray
import (
"context"
"fmt"
"strings"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/popeye/pkg/config"
)
// Section represents an xray renderer.
type Section struct {
render.Base
}
// Render renders an xray node.
func (s *Section) Render(ctx context.Context, ns string, o interface{}) error {
section, ok := o.(render.Section)
if !ok {
return fmt.Errorf("expected Section, but got %T", o)
}
root := NewTreeNode(section.GVR, section.Title)
parent, ok := ctx.Value(KeyParent).(*TreeNode)
if !ok {
return fmt.Errorf("Expecting a TreeNode but got %T", ctx.Value(KeyParent))
}
s.outcomeRefs(root, section)
parent.Add(root)
return nil
}
func (*Section) outcomeRefs(parent *TreeNode, section render.Section) {
for k, issues := range section.Outcome {
p := NewTreeNode(section.GVR, cleanse(k))
parent.Add(p)
for _, issue := range issues {
msg := colorize(cleanse(issue.Message), issue.Level)
c := NewTreeNode(fmt.Sprintf("issue_%d", issue.Level), msg)
if issue.Group == "__root__" {
p.Add(c)
continue
}
if pa := p.Find(issue.GVR, issue.Group); pa != nil {
pa.Add(c)
continue
}
pa := NewTreeNode(issue.GVR, issue.Group)
pa.Add(c)
p.Add(pa)
}
}
}
// ----------------------------------------------------------------------------
// Helpers...
func colorize(s string, l config.Level) string {
c := "green"
// nolint:exhaustive
switch l {
case config.ErrorLevel:
c = "red"
case config.WarnLevel:
c = "orange"
case config.InfoLevel:
c = "blue"
}
return fmt.Sprintf("[%s::]%s", c, s)
}
func cleanse(s string) string {
s = strings.Replace(s, "[", "(", -1)
s = strings.Replace(s, "]", ")", -1)
s = strings.Replace(s, "/", "::", -1)
return s
}