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

Commit 4f8240a

Browse files
author
Hidetatsu Yaginuma
committed
fix bug: leading dash is unintendedly removed in yaml printer
1 parent 2b28875 commit 4f8240a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

manifests/pod.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: test-pod
5+
namespace: kubecolor
6+
spec:
7+
containers:
8+
- name: main
9+
image: remrain/tiny-hello
10+
args:
11+
- -test

printer/yaml.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func (yp *YamlPrinter) printLineAsYamlFormat(line string, w io.Writer, dark bool
2929
trimmedLine := strings.TrimLeft(line, " ")
3030

3131
if yp.inString {
32-
// fmt.Println(trimmedLine)
3332
// if inString is true, the line must be a part of a string which is broken into several lines
3433
fmt.Fprintf(w, "%s%s\n", indent, yp.toColorizedStringValue(trimmedLine, dark))
3534
yp.inString = !yp.isStringClosed(trimmedLine)
@@ -59,8 +58,8 @@ func (yp *YamlPrinter) printLineAsYamlFormat(line string, w io.Writer, dark bool
5958
func (yp *YamlPrinter) toColorizedYamlKey(key string, indentCnt, basicWidth int, dark bool) string {
6059
hasColon := strings.HasSuffix(key, ":")
6160
hasLeadingDash := strings.HasPrefix(key, "- ")
62-
key = strings.TrimRight(key, ":")
63-
key = strings.TrimLeft(key, "- ")
61+
key = strings.TrimSuffix(key, ":")
62+
key = strings.TrimPrefix(key, "- ")
6463

6564
format := "%s"
6665
if hasColon {
@@ -81,10 +80,10 @@ func (yp *YamlPrinter) toColorizedYamlValue(value string, dark bool) string {
8180
}
8281

8382
hasLeadingDash := strings.HasPrefix(value, "- ")
84-
value = strings.TrimLeft(value, "- ")
83+
value = strings.TrimPrefix(value, "- ")
8584

8685
isDoubleQuoted := strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`)
87-
trimmedValue := strings.TrimRight(strings.TrimLeft(value, `"`), `"`)
86+
trimmedValue := strings.TrimSuffix(strings.TrimPrefix(value, `"`), `"`)
8887

8988
var format string
9089
switch {

printer/yaml_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,29 @@ func Test_YamlPrinter_Print(t *testing.T) {
7878
- sleep 30
7979
`),
8080
},
81+
{
82+
name: "a value contains dash",
83+
darkBackground: true,
84+
input: testutil.NewHereDoc(`
85+
apiVersion: v1
86+
items:
87+
- apiVersion: v1
88+
key:
89+
- key2: 415
90+
key3: true
91+
key4:
92+
key: -val`),
93+
expected: testutil.NewHereDoc(`
94+
apiVersion: v1
95+
items:
96+
- apiVersion: v1
97+
key:
98+
- key2: 415
99+
key3: true
100+
key4:
101+
key: -val
102+
`),
103+
},
81104
{
82105
name: "a long string which is broken into several lines can be colored",
83106
darkBackground: true,

0 commit comments

Comments
 (0)