More efficient front matter processor

This commit is contained in:
Mike Farah 2021-07-20 10:38:42 +10:00
parent 4e628327c4
commit 8508d3309b
6 changed files with 49 additions and 53 deletions

View File

@ -98,10 +98,7 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename() args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename()
if frontMatter == "process" { if frontMatter == "process" {
reader, err := os.Open(frontMatterHandler.GetContentFilename()) // #nosec reader := frontMatterHandler.GetContentReader()
if err != nil {
return err
}
printer.SetAppendix(reader) printer.SetAppendix(reader)
defer yqlib.SafelyCloseReader(reader) defer yqlib.SafelyCloseReader(reader)
} }

View File

@ -112,10 +112,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename() args[firstFileIndex] = frontMatterHandler.GetYamlFrontMatterFilename()
if frontMatter == "process" { if frontMatter == "process" {
reader, err := os.Open(frontMatterHandler.GetContentFilename()) // #nosec reader := frontMatterHandler.GetContentReader()
if err != nil {
return err
}
printer.SetAppendix(reader) printer.SetAppendix(reader)
defer yqlib.SafelyCloseReader(reader) defer yqlib.SafelyCloseReader(reader)
} }

View File

@ -2,4 +2,5 @@
a: apple a: apple
b: bannana b: bannana
--- ---
<h1>I like {{a}} and {{b}} </h1> hello there
apples: great

View File

@ -9,47 +9,48 @@ import (
type frontMatterHandler interface { type frontMatterHandler interface {
Split() error Split() error
GetYamlFrontMatterFilename() string GetYamlFrontMatterFilename() string
GetContentFilename() string GetContentReader() io.Reader
CleanUp() CleanUp()
} }
type frontMatterHandlerImpl struct { type frontMatterHandlerImpl struct {
originalFilename string originalFilename string
yamlFrontMatterFilename string yamlFrontMatterFilename string
contentFilename string contentReader io.Reader
} }
func NewFrontMatterHandler(originalFilename string) frontMatterHandler { func NewFrontMatterHandler(originalFilename string) frontMatterHandler {
return &frontMatterHandlerImpl{originalFilename, "", ""} return &frontMatterHandlerImpl{originalFilename, "", nil}
} }
func (f *frontMatterHandlerImpl) GetYamlFrontMatterFilename() string { func (f *frontMatterHandlerImpl) GetYamlFrontMatterFilename() string {
return f.yamlFrontMatterFilename return f.yamlFrontMatterFilename
} }
func (f *frontMatterHandlerImpl) GetContentFilename() string { func (f *frontMatterHandlerImpl) GetContentReader() io.Reader {
return f.contentFilename return f.contentReader
} }
func (f *frontMatterHandlerImpl) CleanUp() { func (f *frontMatterHandlerImpl) CleanUp() {
tryRemoveFile(f.yamlFrontMatterFilename) tryRemoveFile(f.yamlFrontMatterFilename)
tryRemoveFile(f.contentFilename)
} }
// Splits the given file by yaml front matter // Splits the given file by yaml front matter
// yaml content will be saved to first temporary file // yaml content will be saved to first temporary file
// remaining content will be saved to second temporary file // remaining content will be saved to second temporary file
func (f *frontMatterHandlerImpl) Split() error { func (f *frontMatterHandlerImpl) Split() error {
var reader io.Reader var reader *bufio.Reader
var err error var err error
if f.originalFilename == "-" { if f.originalFilename == "-" {
reader = bufio.NewReader(os.Stdin) reader = bufio.NewReader(os.Stdin)
} else { } else {
reader, err = os.Open(f.originalFilename) // #nosec file, err := os.Open(f.originalFilename) // #nosec
if err != nil { if err != nil {
return err return err
} }
reader = bufio.NewReader(file)
} }
f.contentReader = reader
yamlTempFile, err := createTempFile() yamlTempFile, err := createTempFile()
if err != nil { if err != nil {
@ -58,39 +59,35 @@ func (f *frontMatterHandlerImpl) Split() error {
f.yamlFrontMatterFilename = yamlTempFile.Name() f.yamlFrontMatterFilename = yamlTempFile.Name()
log.Debug("yamlTempFile: %v", yamlTempFile.Name()) log.Debug("yamlTempFile: %v", yamlTempFile.Name())
contentTempFile, err := createTempFile()
if err != nil {
return err
}
f.contentFilename = contentTempFile.Name()
log.Debug("contentTempFile: %v", contentTempFile.Name())
scanner := bufio.NewScanner(reader)
lineCount := 0 lineCount := 0
yamlContentBlock := true
for scanner.Scan() { for {
line := scanner.Text() peekBytes, err := reader.Peek(3)
if err == io.EOF {
if lineCount > 0 && line == "---" { // we've finished reading the yaml content..I guess
//we've finished reading the yaml content break
yamlContentBlock = false } else if err != nil {
}
if yamlContentBlock {
_, err = yamlTempFile.Write([]byte(line + "\n"))
} else {
_, err = contentTempFile.Write([]byte(line + "\n"))
}
if err != nil {
return err return err
} }
if lineCount > 0 && string(peekBytes) == "---" {
// we've finished reading the yaml content..
break
}
line, errReading := reader.ReadString('\n')
lineCount = lineCount + 1 lineCount = lineCount + 1
if errReading != nil && errReading != io.EOF {
return errReading
}
_, errWriting := yamlTempFile.Write([]byte(line))
if errWriting != nil {
return errWriting
}
} }
safelyCloseFile(yamlTempFile) safelyCloseFile(yamlTempFile)
safelyCloseFile(contentTempFile)
return scanner.Err() return nil
} }

View File

@ -60,8 +60,11 @@ yaml: doc
test.AssertResult(t, expectedYamlFm, yamlFm) test.AssertResult(t, expectedYamlFm, yamlFm)
content := readFile(fmHandler.GetContentFilename()) contentBytes, err := ioutil.ReadAll(fmHandler.GetContentReader())
test.AssertResult(t, expectedContent, content) if err != nil {
panic(err)
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file) tryRemoveFile(file)
fmHandler.CleanUp() fmHandler.CleanUp()
@ -94,8 +97,11 @@ yaml: doc
test.AssertResult(t, expectedYamlFm, yamlFm) test.AssertResult(t, expectedYamlFm, yamlFm)
content := readFile(fmHandler.GetContentFilename()) contentBytes, err := ioutil.ReadAll(fmHandler.GetContentReader())
test.AssertResult(t, expectedContent, content) if err != nil {
panic(err)
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file) tryRemoveFile(file)
fmHandler.CleanUp() fmHandler.CleanUp()
@ -125,8 +131,11 @@ yaml: doc
test.AssertResult(t, expectedYamlFm, yamlFm) test.AssertResult(t, expectedYamlFm, yamlFm)
content := readFile(fmHandler.GetContentFilename()) contentBytes, err := ioutil.ReadAll(fmHandler.GetContentReader())
test.AssertResult(t, expectedContent, content) if err != nil {
panic(err)
}
test.AssertResult(t, expectedContent, string(contentBytes))
tryRemoveFile(file) tryRemoveFile(file)
fmHandler.CleanUp() fmHandler.CleanUp()

View File

@ -28,7 +28,6 @@ type resultsPrinter struct {
previousFileIndex int previousFileIndex int
printedMatches bool printedMatches bool
treeNavigator DataTreeNavigator treeNavigator DataTreeNavigator
preambleReader io.Reader
appendixReader io.Reader appendixReader io.Reader
} }
@ -45,10 +44,6 @@ func NewPrinter(writer io.Writer, outputToJSON bool, unwrapScalar bool, colorsEn
} }
} }
func (p *resultsPrinter) SetPreamble(reader io.Reader) {
p.preambleReader = reader
}
func (p *resultsPrinter) SetAppendix(reader io.Reader) { func (p *resultsPrinter) SetAppendix(reader io.Reader) {
p.appendixReader = reader p.appendixReader = reader
} }