mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-11 14:11:14 +00:00
use 'lines []string' for updateMacroBlock
This commit is contained in:
@@ -35,7 +35,8 @@ func updateTOC(filePath string, markdown []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
updatedMarkdown, err := updateMacroBlock(markdown, "<!-- BEGIN GENERATED TOC -->", "<!-- END GENERATED TOC -->", string(toc))
|
||||
lines := splitLines(markdown)
|
||||
updatedMarkdown, err := updateMacroBlock(lines, "<!-- BEGIN GENERATED TOC -->", "<!-- END GENERATED TOC -->", string(toc))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -22,17 +22,23 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Replaces the text between matching "beginMark" and "endMark" within "document" with "insertThis".
|
||||
//
|
||||
// Delimiters should occupy own line.
|
||||
// Returns copy of document with modifications.
|
||||
func updateMacroBlock(document []byte, beginMark, endMark, insertThis string) ([]byte, error) {
|
||||
var buffer bytes.Buffer
|
||||
// Splits a document up into a slice of lines.
|
||||
func splitLines(document []byte) []string {
|
||||
lines := strings.Split(string(document), "\n")
|
||||
// Skip trailing empty string from Split-ing
|
||||
if len(lines) > 0 && lines[len(lines)-1] == "" {
|
||||
lines = lines[:len(lines)-1]
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// Replaces the text between matching "beginMark" and "endMark" within the
|
||||
// document represented by "lines" with "insertThis".
|
||||
//
|
||||
// Delimiters should occupy own line.
|
||||
// Returns copy of document with modifications.
|
||||
func updateMacroBlock(lines []string, beginMark, endMark, insertThis string) ([]byte, error) {
|
||||
var buffer bytes.Buffer
|
||||
betweenBeginAndEnd := false
|
||||
for _, line := range lines {
|
||||
trimmedLine := strings.Trim(line, " \n")
|
||||
|
@@ -34,7 +34,7 @@ func Test_updateMacroBlock(t *testing.T) {
|
||||
"Lorem ipsum \n BEGIN\nfoo\n\nEND\nsit amet\n"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
actual, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo\n")
|
||||
actual, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo\n")
|
||||
assert.NoError(t, err)
|
||||
if c.out != string(actual) {
|
||||
t.Errorf("Expected '%v' but got '%v'", c.out, string(actual))
|
||||
@@ -56,7 +56,7 @@ func Test_updateMacroBlock_errors(t *testing.T) {
|
||||
{"BEGIN\nBEGIN\nEND\nEND"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
_, err := updateMacroBlock([]byte(c.in), "BEGIN", "END", "foo")
|
||||
_, err := updateMacroBlock(splitLines([]byte(c.in)), "BEGIN", "END", "foo")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user