Skip to content

Commit 6d915a8

Browse files
TeaDovePeter Ibragimov
andauthored
338: fix: move highlighting after formatting (#339)
* fix: move highlighting after formatting * ref: add tests for highlight and include * ref: remove wrong change * feat: add custom colors as template func * ref: update readme * STERN-340: ref: rework withoutHighligh feature * STERN-340: ref: fix tests --------- Co-authored-by: Peter Ibragimov <[email protected]>
1 parent 3cd85b4 commit 6d915a8

5 files changed

Lines changed: 190 additions & 17 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ functions](https://golang.org/pkg/text/template/#hdr-Functions)):
181181
| `colorMagenta` | `string` | Print text using magenta color |
182182
| `colorCyan` | `string` | Print text using cyan color |
183183
| `colorWhite` | `string` | Print text using white color |
184+
| `colorCustom` | `string, int [, int]` | Print text using custom color, i.e. {{color "Hi" 3 96}} will print "Hi" as italic with cyan color |
184185

185186
### Log level verbosity
186187

‎cmd/cmd.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,9 @@ func (o *options) generateTemplate() (*template.Template, error) {
641641
"color": func(color color.Color, text string) string {
642642
return color.SprintFunc()(text)
643643
},
644+
"colorCustom": func(text string, args ...color.Attribute) string {
645+
return color.New(args...).SprintFunc()(text)
646+
},
644647
"colorBlack": color.BlackString,
645648
"colorRed": color.RedString,
646649
"colorGreen": color.GreenString,

‎stern/file_tail.go‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ func (t *FileTail) ConsumeReader(reader *bufio.Reader) error {
5656
}
5757
}
5858

59-
// Print prints a color coded log message
60-
func (t *FileTail) Print(msg string) {
59+
func (t *FileTail) sprint(msg string) (string, error) {
6160
vm := Log{
6261
Message: msg,
6362
NodeName: "",
@@ -70,11 +69,32 @@ func (t *FileTail) Print(msg string) {
7069

7170
var buf bytes.Buffer
7271
if err := t.tmpl.Execute(&buf, vm); err != nil {
73-
fmt.Fprintf(t.errOut, "expanding template failed: %s\n", err)
72+
return "", fmt.Errorf("expanding template failed: %s", err)
73+
}
74+
75+
return buf.String(), nil
76+
}
77+
78+
// Print prints a color coded log message
79+
func (t *FileTail) Print(msg string) {
80+
buf, err := t.sprint(msg)
81+
if err != nil {
82+
fmt.Fprintf(t.errOut, "%s\n", err)
83+
return
84+
}
85+
86+
fmt.Fprint(t.out, t.Options.HighlightMatchedString(buf))
87+
}
88+
89+
// PrintWithoutHighlight prints a log message without applying any highlight.
90+
func (t *FileTail) PrintWithoutHighlight(msg string) {
91+
buf, err := t.sprint(msg)
92+
if err != nil {
93+
fmt.Fprintf(t.errOut, "%s\n", err)
7494
return
7595
}
7696

77-
fmt.Fprint(t.out, buf.String())
97+
fmt.Fprint(t.out, buf)
7898
}
7999

80100
func (t *FileTail) consumeLine(line string) {
@@ -84,6 +104,5 @@ func (t *FileTail) consumeLine(line string) {
84104
return
85105
}
86106

87-
msg := t.Options.HighlightMatchedString(content)
88-
t.Print(msg)
107+
t.Print(content)
89108
}

‎stern/tail.go‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ func (t *Tail) ConsumeRequest(ctx context.Context, request rest.ResponseWrapper)
197197
}
198198
}
199199

200-
// Print prints a color coded log message with the pod and container names
201-
func (t *Tail) Print(msg string) {
200+
func (t *Tail) sprint(msg string) (string, error) {
202201
vm := Log{
203202
Message: msg,
204203
NodeName: t.Pod.Spec.NodeName,
@@ -213,11 +212,32 @@ func (t *Tail) Print(msg string) {
213212

214213
var buf bytes.Buffer
215214
if err := t.tmpl.Execute(&buf, vm); err != nil {
216-
fmt.Fprintf(t.errOut, "expanding template failed: %s\n", err)
215+
return "", fmt.Errorf("expanding template failed: %s", err)
216+
}
217+
218+
return buf.String(), nil
219+
}
220+
221+
// Print prints a color coded log message with the pod and container names
222+
func (t *Tail) Print(msg string) {
223+
buf, err := t.sprint(msg)
224+
if err != nil {
225+
fmt.Fprintf(t.errOut, "%s\n", err)
226+
return
227+
}
228+
229+
fmt.Fprint(t.out, t.Options.HighlightMatchedString(buf))
230+
}
231+
232+
// PrintWithoutHighlight prints a log message without applying any highlight.
233+
func (t *Tail) PrintWithoutHighlight(msg string) {
234+
buf, err := t.sprint(msg)
235+
if err != nil {
236+
fmt.Fprintf(t.errOut, "%s\n", err)
217237
return
218238
}
219239

220-
fmt.Fprint(t.out, buf.String())
240+
fmt.Fprint(t.out, buf)
221241
}
222242

223243
func (t *Tail) GetResumeRequest() *ResumeRequest {
@@ -230,7 +250,7 @@ func (t *Tail) GetResumeRequest() *ResumeRequest {
230250
func (t *Tail) consumeLine(line string) {
231251
rfc3339Nano, content, err := splitLogLine(line)
232252
if err != nil {
233-
t.Print(fmt.Sprintf("[%v] %s", err, line))
253+
t.PrintWithoutHighlight(fmt.Sprintf("[%v] %s", err, line))
234254
return
235255
}
236256

@@ -246,18 +266,16 @@ func (t *Tail) consumeLine(line string) {
246266
return
247267
}
248268

249-
msg := t.Options.HighlightMatchedString(content)
250-
251269
if t.Options.Timestamps {
252270
updatedTs, err := t.Options.UpdateTimezoneAndFormat(rfc3339Nano)
253271
if err != nil {
254-
t.Print(fmt.Sprintf("[%v] %s", err, line))
272+
t.PrintWithoutHighlight(fmt.Sprintf("[%v] %s", err, line))
255273
return
256274
}
257-
msg = updatedTs + " " + msg
275+
content = updatedTs + " " + content
258276
}
259277

260-
t.Print(msg)
278+
t.Print(content)
261279
}
262280

263281
func (t *Tail) rememberLastTimestamp(timestamp string) {

‎stern/tail_test.go‎

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package stern
33
import (
44
"bytes"
55
"context"
6+
"fmt"
7+
"github.com/fatih/color"
68
"io"
79
"reflect"
10+
"regexp"
811
"testing"
912
"text/template"
1013

@@ -121,7 +124,136 @@ line 4 (my-node/my-namespace/my-pod/my-container)
121124
}
122125

123126
if !bytes.Equal(tt.expected, out.Bytes()) {
124-
t.Errorf("%d: expected %s, but actual %s", i, tt.expected, out)
127+
t.Errorf("%d: expected `%s`, but actual `%s`", i, tt.expected, out)
128+
}
129+
})
130+
}
131+
}
132+
133+
func TestHighlight(t *testing.T) {
134+
color.NoColor = false
135+
defer func() { color.NoColor = true }()
136+
coloredLine := colorHighlight("line")
137+
138+
tmpl := template.Must(template.New("").Parse(`{{printf "%s (%s/%s/%s)\n" .Message .Namespace .PodName .ContainerName}}`))
139+
140+
tests := []struct {
141+
name string
142+
logLine string
143+
expected []byte
144+
}{
145+
{
146+
name: "normal",
147+
logLine: `2023-02-13T21:20:30.000000001Z line 1
148+
2023-02-13T21:20:30.000000002Z line 2
149+
2023-02-13T21:20:31.000000001Z line 3
150+
2023-02-13T21:20:31.000000002Z line 4`,
151+
expected: []byte(fmt.Sprintf(`%s 1 (my-namespace/my-pod/my-container)
152+
%s 2 (my-namespace/my-pod/my-container)
153+
%s 3 (my-namespace/my-pod/my-container)
154+
%s 4 (my-namespace/my-pod/my-container)
155+
`, coloredLine, coloredLine, coloredLine, coloredLine)),
156+
},
157+
{
158+
name: "no highlight",
159+
logLine: `2023-02-13T21:20:30.000000001Z log 1
160+
2023-02-13T21:20:30.000000002Z log 2
161+
2023-02-13T21:20:31.000000001Z log 3
162+
2023-02-13T21:20:31.000000002Z log 4`,
163+
expected: []byte(`log 1 (my-namespace/my-pod/my-container)
164+
log 2 (my-namespace/my-pod/my-container)
165+
log 3 (my-namespace/my-pod/my-container)
166+
log 4 (my-namespace/my-pod/my-container)
167+
`),
168+
},
169+
}
170+
171+
clientset := fake.NewSimpleClientset()
172+
for i, tt := range tests {
173+
t.Run(tt.name, func(t *testing.T) {
174+
out := new(bytes.Buffer)
175+
pod := &corev1.Pod{
176+
ObjectMeta: metav1.ObjectMeta{
177+
Namespace: "my-namespace",
178+
Name: "my-pod",
179+
},
180+
}
181+
182+
tail := NewTail(clientset.CoreV1(), pod, "my-container", tmpl, out, io.Discard, &TailOptions{Highlight: []*regexp.Regexp{regexp.MustCompile("line")}}, false)
183+
if err := tail.ConsumeRequest(context.TODO(), &responseWrapperMock{data: bytes.NewBufferString(tt.logLine)}); err != nil {
184+
t.Fatalf("%d: unexpected err %v", i, err)
185+
}
186+
187+
if !bytes.Equal(tt.expected, out.Bytes()) {
188+
t.Errorf("%d: expected `%s`, but actual `%s`", i, tt.expected, out)
189+
}
190+
})
191+
}
192+
}
193+
194+
func TestInclude(t *testing.T) {
195+
color.NoColor = false
196+
defer func() { color.NoColor = true }()
197+
198+
coloredLine := colorHighlight("line")
199+
200+
tmpl := template.Must(template.New("").Parse(`{{printf "%s (%s/%s/%s)\n" .Message .Namespace .PodName .ContainerName}}`))
201+
202+
tests := []struct {
203+
name string
204+
logLine string
205+
expected []byte
206+
}{
207+
{
208+
name: "normal",
209+
logLine: `2023-02-13T21:20:30.000000001Z line 1
210+
2023-02-13T21:20:30.000000002Z line 2
211+
2023-02-13T21:20:31.000000001Z line 3
212+
2023-02-13T21:20:31.000000002Z line 4`,
213+
expected: []byte(fmt.Sprintf(`%s 1 (my-namespace/my-pod/my-container)
214+
%s 2 (my-namespace/my-pod/my-container)
215+
%s 3 (my-namespace/my-pod/my-container)
216+
%s 4 (my-namespace/my-pod/my-container)
217+
`, coloredLine, coloredLine, coloredLine, coloredLine)),
218+
},
219+
{
220+
name: "full excluded",
221+
logLine: `2023-02-13T21:20:30.000000001Z log 1
222+
2023-02-13T21:20:30.000000002Z log 2
223+
2023-02-13T21:20:31.000000001Z log 3
224+
2023-02-13T21:20:31.000000002Z log 4`,
225+
expected: []byte(""),
226+
},
227+
228+
{
229+
name: "one included",
230+
logLine: `2023-02-13T21:20:30.000000001Z log 1
231+
2023-02-13T21:20:30.000000002Z line 2
232+
2023-02-13T21:20:31.000000001Z log 3
233+
2023-02-13T21:20:31.000000002Z log 4`,
234+
expected: []byte(fmt.Sprintf(`%s 2 (my-namespace/my-pod/my-container)
235+
`, coloredLine)),
236+
},
237+
}
238+
239+
clientset := fake.NewSimpleClientset()
240+
for i, tt := range tests {
241+
t.Run(tt.name, func(t *testing.T) {
242+
out := new(bytes.Buffer)
243+
pod := &corev1.Pod{
244+
ObjectMeta: metav1.ObjectMeta{
245+
Namespace: "my-namespace",
246+
Name: "my-pod",
247+
},
248+
}
249+
250+
tail := NewTail(clientset.CoreV1(), pod, "my-container", tmpl, out, io.Discard, &TailOptions{Include: []*regexp.Regexp{regexp.MustCompile("line")}}, false)
251+
if err := tail.ConsumeRequest(context.TODO(), &responseWrapperMock{data: bytes.NewBufferString(tt.logLine)}); err != nil {
252+
t.Fatalf("%d: unexpected err %v", i, err)
253+
}
254+
255+
if !bytes.Equal(tt.expected, out.Bytes()) {
256+
t.Errorf("%d: expected `%s`, but actual `%s`", i, tt.expected, out)
125257
}
126258
})
127259
}

0 commit comments

Comments
 (0)