Skip to content
This repository was archived by the owner on Jul 30, 2023. It is now read-only.

Commit a5964ec

Browse files
author
Hidetatsu Yaginuma
committed
colorize apply result by the action
1 parent a381b64 commit a5964ec

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

printer/kubectl_apply.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package printer
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"strings"
8+
9+
"github.com/dty1er/kubecolor/color"
10+
)
11+
12+
type ApplyPrinter struct {
13+
DarkBackground bool
14+
}
15+
16+
// kubectl apply
17+
// deployment.apps/foo unchanged
18+
// deployment.apps/bar created
19+
// deployment.apps/quux configured
20+
func (ap *ApplyPrinter) Print(r io.Reader, w io.Writer) {
21+
const (
22+
applyActionCreated = "created"
23+
applyActionConfigured = "configured"
24+
applyActionUnchanged = "unchanged"
25+
)
26+
27+
colors := map[string]color.Color{
28+
applyActionCreated: color.Green,
29+
applyActionConfigured: color.Yellow,
30+
applyActionUnchanged: color.Magenta,
31+
}
32+
33+
colorize := func(line, action string, wr io.Writer) {
34+
arg := strings.TrimSuffix(line, " "+action)
35+
fmt.Fprintf(w, "%s %s\n", arg, color.Apply(action, colors[action]))
36+
}
37+
38+
scanner := bufio.NewScanner(r)
39+
for scanner.Scan() {
40+
line := scanner.Text()
41+
switch {
42+
case strings.HasSuffix(line, " "+applyActionCreated):
43+
colorize(line, applyActionCreated, w)
44+
case strings.HasSuffix(line, " "+applyActionConfigured):
45+
colorize(line, applyActionConfigured, w)
46+
case strings.HasSuffix(line, " "+applyActionUnchanged):
47+
colorize(line, applyActionUnchanged, w)
48+
default:
49+
fmt.Fprintf(w, "%s\n", color.Apply(line, color.Green))
50+
}
51+
}
52+
}

printer/kubectl_apply_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package printer
2+
3+
import (
4+
"bytes"
5+
"strings"
6+
"testing"
7+
8+
"github.com/dty1er/kubecolor/testutil"
9+
)
10+
11+
func Test_ApplyPrinter_Print(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
darkBackground bool
15+
tablePrinter *TablePrinter
16+
input string
17+
expected string
18+
}{
19+
{
20+
name: "created",
21+
darkBackground: true,
22+
input: testutil.NewHereDoc(`
23+
deployment.apps/foo created`),
24+
expected: testutil.NewHereDoc(`
25+
deployment.apps/foo created
26+
`),
27+
},
28+
{
29+
name: "configured",
30+
darkBackground: true,
31+
input: testutil.NewHereDoc(`
32+
deployment.apps/foo configured`),
33+
expected: testutil.NewHereDoc(`
34+
deployment.apps/foo configured
35+
`),
36+
},
37+
{
38+
name: "unchanged",
39+
darkBackground: true,
40+
input: testutil.NewHereDoc(`
41+
deployment.apps/foo unchanged`),
42+
expected: testutil.NewHereDoc(`
43+
deployment.apps/foo unchanged
44+
`),
45+
},
46+
{
47+
name: "something else. This likely won't happen but fallbacks here just in case.",
48+
darkBackground: true,
49+
input: testutil.NewHereDoc(`
50+
deployment.apps/foo bar`),
51+
expected: testutil.NewHereDoc(`
52+
deployment.apps/foo bar
53+
`),
54+
},
55+
}
56+
for _, tt := range tests {
57+
tt := tt
58+
t.Run(tt.name, func(t *testing.T) {
59+
t.Parallel()
60+
r := strings.NewReader(tt.input)
61+
var w bytes.Buffer
62+
printer := ApplyPrinter{DarkBackground: tt.darkBackground}
63+
printer.Print(r, &w)
64+
testutil.MustEqual(t, tt.expected, w.String())
65+
})
66+
}
67+
}

printer/kubectl_output_colored_printer.go

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func (kp *KubectlOutputColoredPrinter) Print(r io.Reader, w io.Writer) {
9999
case kp.SubcommandInfo.FormatOption == kubectl.Yaml:
100100
printer = &YamlPrinter{DarkBackground: kp.DarkBackground}
101101
default:
102+
printer = &ApplyPrinter{DarkBackground: kp.DarkBackground}
102103
}
103104
}
104105

0 commit comments

Comments
 (0)