diff --git a/pkg/yqlib/printer.go b/pkg/yqlib/printer.go index 71783fe3..a264bd49 100644 --- a/pkg/yqlib/printer.go +++ b/pkg/yqlib/printer.go @@ -5,7 +5,7 @@ import ( "container/list" "fmt" "io" - "strings" + "regexp" yaml "gopkg.in/yaml.v3" ) @@ -132,7 +132,8 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error { return errorWriting } - commentStartsWithSeparator := strings.Contains(mappedDoc.LeadingContent, "$yqLeadingContent$\n$yqDocSeperator$") + commentsStartWithSepExp := regexp.MustCompile(`^\$yqDocSeperator\$`) + commentStartsWithSeparator := commentsStartWithSepExp.MatchString(mappedDoc.LeadingContent) if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator { log.Debug("-- writing doc sep") diff --git a/pkg/yqlib/printer_test.go b/pkg/yqlib/printer_test.go index a2fb7152..8c74af87 100644 --- a/pkg/yqlib/printer_test.go +++ b/pkg/yqlib/printer_test.go @@ -82,15 +82,15 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) { } el := inputs.Front() - el.Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n" + el.Value.(*CandidateNode).LeadingContent = "# go cats\n$yqDocSeperator$\n" sample1 := nodeToList(el.Value.(*CandidateNode)) el = el.Next() - el.Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n" + el.Value.(*CandidateNode).LeadingContent = "$yqDocSeperator$\n" sample2 := nodeToList(el.Value.(*CandidateNode)) el = el.Next() - el.Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n# cool\n" + el.Value.(*CandidateNode).LeadingContent = "$yqDocSeperator$\n# cool\n" sample3 := nodeToList(el.Value.(*CandidateNode)) err = printer.PrintResults(sample1) @@ -174,21 +174,21 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) { elNode := el.Value.(*CandidateNode) elNode.Document = 0 elNode.FileIndex = 0 - elNode.LeadingContent = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n" + elNode.LeadingContent = "# go cats\n$yqDocSeperator$\n" sample1 := nodeToList(elNode) el = el.Next() elNode = el.Value.(*CandidateNode) elNode.Document = 0 elNode.FileIndex = 1 - elNode.LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n" + elNode.LeadingContent = "$yqDocSeperator$\n" sample2 := nodeToList(elNode) el = el.Next() elNode = el.Value.(*CandidateNode) elNode.Document = 0 elNode.FileIndex = 2 - elNode.LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n# cool\n" + elNode.LeadingContent = "$yqDocSeperator$\n# cool\n" sample3 := nodeToList(elNode) err = printer.PrintResults(sample1) @@ -239,7 +239,7 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) { panic(err) } - inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n" + inputs.Front().Value.(*CandidateNode).LeadingContent = "# go cats\n$yqDocSeperator$\n" err = printer.PrintResults(inputs) if err != nil { @@ -267,7 +267,7 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) { if err != nil { panic(err) } - inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n" + inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqDocSeperator$\n" err = printer.PrintResults(inputs) if err != nil { panic(err) @@ -321,7 +321,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) { panic(err) } - inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n# ignore this\n" + inputs.Front().Value.(*CandidateNode).LeadingContent = "# ignore this\n" err = printer.PrintResults(inputs) if err != nil { diff --git a/pkg/yqlib/utils.go b/pkg/yqlib/utils.go index c38c3c43..7405f730 100644 --- a/pkg/yqlib/utils.go +++ b/pkg/yqlib/utils.go @@ -37,52 +37,46 @@ func writeString(writer io.Writer, txt string) error { } func processLeadingContent(mappedDoc *CandidateNode, writer io.Writer, printDocSeparators bool, outputFormat PrinterOutputFormat) error { - if strings.Contains(mappedDoc.LeadingContent, "$yqLeadingContent$") { - log.Debug("headcommentwas %v", mappedDoc.LeadingContent) - log.Debug("finished headcomment") - reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent)) - mappedDoc.Node.HeadComment = "" + log.Debug("headcommentwas %v", mappedDoc.LeadingContent) + log.Debug("finished headcomment") + reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent)) + mappedDoc.Node.HeadComment = "" - for { + for { - readline, errReading := reader.ReadString('\n') - if errReading != nil && errReading != io.EOF { - return errReading - } - if strings.Contains(readline, "$yqLeadingContent$") { - // skip this - - } else if strings.Contains(readline, "$yqDocSeperator$") { - if printDocSeparators { - if err := writeString(writer, "---\n"); err != nil { - return err - } - } - } else if outputFormat == YamlOutputFormat { - if err := writeString(writer, readline); err != nil { + readline, errReading := reader.ReadString('\n') + if errReading != nil && errReading != io.EOF { + return errReading + } + if strings.Contains(readline, "$yqDocSeperator$") { + if printDocSeparators { + if err := writeString(writer, "---\n"); err != nil { return err } } - - if errReading == io.EOF { - if readline != "" { - // the last comment we read didn't have a new line, put one in - if err := writeString(writer, "\n"); err != nil { - return err - } - } - break + } else if outputFormat == YamlOutputFormat { + if err := writeString(writer, readline); err != nil { + return err } } + if errReading == io.EOF { + if readline != "" { + // the last comment we read didn't have a new line, put one in + if err := writeString(writer, "\n"); err != nil { + return err + } + } + break + } } + return nil } func processReadStream(reader *bufio.Reader) (io.Reader, string, error) { var commentLineRegEx = regexp.MustCompile(`^\s*#`) var sb strings.Builder - sb.WriteString("$yqLeadingContent$\n") for { peekBytes, err := reader.Peek(3) if err == io.EOF {