mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-27 08:55:37 +00:00
remove leading content indicator
This commit is contained in:
parent
5df71162c9
commit
11b6261e8b
@ -5,7 +5,7 @@ import (
|
|||||||
"container/list"
|
"container/list"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"regexp"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v3"
|
yaml "gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@ -132,7 +132,8 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
|
|||||||
return errorWriting
|
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 {
|
if (p.previousDocIndex != mappedDoc.Document || p.previousFileIndex != mappedDoc.FileIndex) && p.printDocSeparators && !commentStartsWithSeparator {
|
||||||
log.Debug("-- writing doc sep")
|
log.Debug("-- writing doc sep")
|
||||||
|
@ -82,15 +82,15 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
el := inputs.Front()
|
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))
|
sample1 := nodeToList(el.Value.(*CandidateNode))
|
||||||
|
|
||||||
el = el.Next()
|
el = el.Next()
|
||||||
el.Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n"
|
el.Value.(*CandidateNode).LeadingContent = "$yqDocSeperator$\n"
|
||||||
sample2 := nodeToList(el.Value.(*CandidateNode))
|
sample2 := nodeToList(el.Value.(*CandidateNode))
|
||||||
|
|
||||||
el = el.Next()
|
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))
|
sample3 := nodeToList(el.Value.(*CandidateNode))
|
||||||
|
|
||||||
err = printer.PrintResults(sample1)
|
err = printer.PrintResults(sample1)
|
||||||
@ -174,21 +174,21 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
|
|||||||
elNode := el.Value.(*CandidateNode)
|
elNode := el.Value.(*CandidateNode)
|
||||||
elNode.Document = 0
|
elNode.Document = 0
|
||||||
elNode.FileIndex = 0
|
elNode.FileIndex = 0
|
||||||
elNode.LeadingContent = "$yqLeadingContent$\n# go cats\n$yqDocSeperator$\n"
|
elNode.LeadingContent = "# go cats\n$yqDocSeperator$\n"
|
||||||
sample1 := nodeToList(elNode)
|
sample1 := nodeToList(elNode)
|
||||||
|
|
||||||
el = el.Next()
|
el = el.Next()
|
||||||
elNode = el.Value.(*CandidateNode)
|
elNode = el.Value.(*CandidateNode)
|
||||||
elNode.Document = 0
|
elNode.Document = 0
|
||||||
elNode.FileIndex = 1
|
elNode.FileIndex = 1
|
||||||
elNode.LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n"
|
elNode.LeadingContent = "$yqDocSeperator$\n"
|
||||||
sample2 := nodeToList(elNode)
|
sample2 := nodeToList(elNode)
|
||||||
|
|
||||||
el = el.Next()
|
el = el.Next()
|
||||||
elNode = el.Value.(*CandidateNode)
|
elNode = el.Value.(*CandidateNode)
|
||||||
elNode.Document = 0
|
elNode.Document = 0
|
||||||
elNode.FileIndex = 2
|
elNode.FileIndex = 2
|
||||||
elNode.LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n# cool\n"
|
elNode.LeadingContent = "$yqDocSeperator$\n# cool\n"
|
||||||
sample3 := nodeToList(elNode)
|
sample3 := nodeToList(elNode)
|
||||||
|
|
||||||
err = printer.PrintResults(sample1)
|
err = printer.PrintResults(sample1)
|
||||||
@ -239,7 +239,7 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) {
|
|||||||
panic(err)
|
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)
|
err = printer.PrintResults(inputs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -267,7 +267,7 @@ func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n$yqDocSeperator$\n"
|
inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqDocSeperator$\n"
|
||||||
err = printer.PrintResults(inputs)
|
err = printer.PrintResults(inputs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -321,7 +321,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inputs.Front().Value.(*CandidateNode).LeadingContent = "$yqLeadingContent$\n# ignore this\n"
|
inputs.Front().Value.(*CandidateNode).LeadingContent = "# ignore this\n"
|
||||||
|
|
||||||
err = printer.PrintResults(inputs)
|
err = printer.PrintResults(inputs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -37,52 +37,46 @@ func writeString(writer io.Writer, txt string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processLeadingContent(mappedDoc *CandidateNode, writer io.Writer, printDocSeparators bool, outputFormat PrinterOutputFormat) 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("headcommentwas %v", mappedDoc.LeadingContent)
|
log.Debug("finished headcomment")
|
||||||
log.Debug("finished headcomment")
|
reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent))
|
||||||
reader := bufio.NewReader(strings.NewReader(mappedDoc.LeadingContent))
|
mappedDoc.Node.HeadComment = ""
|
||||||
mappedDoc.Node.HeadComment = ""
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
readline, errReading := reader.ReadString('\n')
|
readline, errReading := reader.ReadString('\n')
|
||||||
if errReading != nil && errReading != io.EOF {
|
if errReading != nil && errReading != io.EOF {
|
||||||
return errReading
|
return errReading
|
||||||
}
|
}
|
||||||
if strings.Contains(readline, "$yqLeadingContent$") {
|
if strings.Contains(readline, "$yqDocSeperator$") {
|
||||||
// skip this
|
if printDocSeparators {
|
||||||
|
if err := writeString(writer, "---\n"); err != nil {
|
||||||
} 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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if outputFormat == YamlOutputFormat {
|
||||||
if errReading == io.EOF {
|
if err := writeString(writer, readline); err != nil {
|
||||||
if readline != "" {
|
return err
|
||||||
// the last comment we read didn't have a new line, put one in
|
|
||||||
if err := writeString(writer, "\n"); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
|
func processReadStream(reader *bufio.Reader) (io.Reader, string, error) {
|
||||||
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
var commentLineRegEx = regexp.MustCompile(`^\s*#`)
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
sb.WriteString("$yqLeadingContent$\n")
|
|
||||||
for {
|
for {
|
||||||
peekBytes, err := reader.Peek(3)
|
peekBytes, err := reader.Peek(3)
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
|
Loading…
Reference in New Issue
Block a user