Ensure reasons end up as comments in kubectl edit.

Signed-off-by: Bruno Miguel Custodio <brunomcustodio@gmail.com>
This commit is contained in:
Bruno Miguel Custodio 2018-03-10 13:08:50 +00:00
parent 7c9293e1c3
commit 6889dc82ef
No known key found for this signature in database
GPG Key ID: 84CDD9E18A1A6B2C
3 changed files with 73 additions and 4 deletions

View File

@ -41,7 +41,10 @@ go_library(
go_test(
name = "go_default_test",
srcs = ["editor_test.go"],
srcs = [
"editoptions_test.go",
"editor_test.go",
],
embed = [":go_default_library"],
)

View File

@ -657,12 +657,12 @@ func (h *editHeader) writeTo(w io.Writer, editMode EditMode) error {
for _, r := range h.reasons {
if len(r.other) > 0 {
fmt.Fprintf(w, "# %s:\n", r.head)
fmt.Fprintf(w, "# %s:\n", hashOnLineBreak(r.head))
} else {
fmt.Fprintf(w, "# %s\n", r.head)
fmt.Fprintf(w, "# %s\n", hashOnLineBreak(r.head))
}
for _, o := range r.other {
fmt.Fprintf(w, "# * %s\n", o)
fmt.Fprintf(w, "# * %s\n", hashOnLineBreak(o))
}
fmt.Fprintln(w, "#")
}
@ -738,3 +738,18 @@ func hasLines(r io.Reader) (bool, error) {
}
return false, nil
}
// hashOnLineBreak returns a string built from the provided string by inserting any necessary '#'
// characters after '\n' characters, indicating a comment.
func hashOnLineBreak(s string) string {
r := ""
for i, ch := range s {
j := i + 1
if j < len(s) && ch == '\n' && s[j] != '#' {
r += "\n# "
} else {
r += string(ch)
}
}
return r
}

View File

@ -0,0 +1,51 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package editor
import (
"testing"
)
func TestHashOnLineBreak(t *testing.T) {
tests := []struct {
original string
expected string
}{
{
original: "",
expected: "",
},
{
original: "\n",
expected: "\n",
},
{
original: "a\na\na\n",
expected: "a\n# a\n# a\n",
},
{
original: "a\n\n\na\n\n",
expected: "a\n# \n# \n# a\n# \n",
},
}
for _, test := range tests {
r := hashOnLineBreak(test.original)
if r != test.expected {
t.Errorf("expected: %s, saw: %s", test.expected, r)
}
}
}