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

Commit a50eb34

Browse files
author
Hidetatsu Yaginuma
committed
consider dry run
1 parent a5964ec commit a50eb34

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

printer/kubectl_apply.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,64 @@ func (ap *ApplyPrinter) Print(r io.Reader, w io.Writer) {
2222
applyActionCreated = "created"
2323
applyActionConfigured = "configured"
2424
applyActionUnchanged = "unchanged"
25+
26+
dryRunStr = "(dry run)"
2527
)
2628

27-
colors := map[string]color.Color{
29+
darkColors := map[string]color.Color{
30+
applyActionCreated: color.Green,
31+
applyActionConfigured: color.Yellow,
32+
applyActionUnchanged: color.Magenta,
33+
dryRunStr: color.Cyan,
34+
}
35+
lightColors := map[string]color.Color{
2836
applyActionCreated: color.Green,
2937
applyActionConfigured: color.Yellow,
3038
applyActionUnchanged: color.Magenta,
39+
dryRunStr: color.Blue,
3140
}
3241

33-
colorize := func(line, action string, wr io.Writer) {
42+
colors := func(action string, dark bool) color.Color {
43+
if dark {
44+
return darkColors[action]
45+
}
46+
return lightColors[action]
47+
}
48+
49+
colorize := func(line, action string, dryRun bool, wr io.Writer) {
50+
if dryRun {
51+
arg := strings.TrimSuffix(line, fmt.Sprintf(" %s %s", action, dryRunStr))
52+
fmt.Fprintf(w, "%s %s %s\n",
53+
arg,
54+
color.Apply(action, colors(action, ap.DarkBackground)),
55+
color.Apply(dryRunStr, colors(dryRunStr, ap.DarkBackground)),
56+
)
57+
return
58+
}
59+
3460
arg := strings.TrimSuffix(line, " "+action)
35-
fmt.Fprintf(w, "%s %s\n", arg, color.Apply(action, colors[action]))
61+
fmt.Fprintf(w, "%s %s\n", arg, color.Apply(action, colors(action, ap.DarkBackground)))
3662
}
3763

3864
scanner := bufio.NewScanner(r)
3965
for scanner.Scan() {
4066
line := scanner.Text()
4167
switch {
68+
// on dry run cases, it shows "xxx created (dry run)"
69+
case strings.HasSuffix(line, fmt.Sprintf(" %s %s", applyActionCreated, dryRunStr)):
70+
colorize(line, applyActionCreated, true, w)
71+
case strings.HasSuffix(line, fmt.Sprintf(" %s %s", applyActionConfigured, dryRunStr)):
72+
colorize(line, applyActionConfigured, true, w)
73+
case strings.HasSuffix(line, fmt.Sprintf(" %s %s", applyActionUnchanged, dryRunStr)):
74+
colorize(line, applyActionUnchanged, true, w)
75+
76+
// not dry run cases, it shows "xxx created"
4277
case strings.HasSuffix(line, " "+applyActionCreated):
43-
colorize(line, applyActionCreated, w)
78+
colorize(line, applyActionCreated, false, w)
4479
case strings.HasSuffix(line, " "+applyActionConfigured):
45-
colorize(line, applyActionConfigured, w)
80+
colorize(line, applyActionConfigured, false, w)
4681
case strings.HasSuffix(line, " "+applyActionUnchanged):
47-
colorize(line, applyActionUnchanged, w)
82+
colorize(line, applyActionUnchanged, false, w)
4883
default:
4984
fmt.Fprintf(w, "%s\n", color.Apply(line, color.Green))
5085
}

printer/kubectl_apply_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func Test_ApplyPrinter_Print(t *testing.T) {
4343
deployment.apps/foo unchanged
4444
`),
4545
},
46+
{
47+
name: "dry run",
48+
darkBackground: true,
49+
input: testutil.NewHereDoc(`
50+
deployment.apps/foo unchanged (dry run)`),
51+
expected: testutil.NewHereDoc(`
52+
deployment.apps/foo unchanged (dry run)
53+
`),
54+
},
4655
{
4756
name: "something else. This likely won't happen but fallbacks here just in case.",
4857
darkBackground: true,

0 commit comments

Comments
 (0)