wip split

This commit is contained in:
Mike Farah 2021-10-29 20:49:06 +11:00
parent 54b20f68db
commit c2d9b98aeb
9 changed files with 79 additions and 23 deletions

View File

@ -21,4 +21,6 @@ var prettyPrint = false
// can be either "" (off), "extract" or "process" // can be either "" (off), "extract" or "process"
var frontMatter = "" var frontMatter = ""
var splitFileExp = ""
var completedSuccessfully = false var completedSuccessfully = false

View File

@ -70,6 +70,10 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file") return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file")
} }
if writeInplace && splitFileExp != "" {
return fmt.Errorf("Write inplace cannot be used with split file")
}
if writeInplace { if writeInplace {
// only use colors if its forced // only use colors if its forced
colorsEnabled = forceColor colorsEnabled = forceColor
@ -96,7 +100,24 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
return err return err
} }
printer := yqlib.NewPrinter(out, format, unwrapScalar, colorsEnabled, indent, !noDocSeparators) var printerWriter yqlib.PrinterWriter
if splitFileExp == "i" {
colorsEnabled = forceColor
printerWriter = yqlib.NewMultiPrinterWriter(nil, format)
} else if splitFileExp != "" {
colorsEnabled = forceColor
splitExp, err := yqlib.NewExpressionParser().ParseExpression(splitFileExp)
if err != nil {
return nil
}
printerWriter = yqlib.NewMultiPrinterWriter(splitExp, format)
} else {
printerWriter = yqlib.NewSinglePrinterWriter(out)
}
printer := yqlib.NewPrinter(printerWriter, format, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
if frontMatter != "" { if frontMatter != "" {
frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex]) frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex])

View File

@ -106,7 +106,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
return err return err
} }
printer := yqlib.NewPrinter(out, format, unwrapScalar, colorsEnabled, indent, !noDocSeparators) printer := yqlib.NewPrinter(yqlib.NewSinglePrinterWriter(out), format, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
streamEvaluator := yqlib.NewStreamEvaluator() streamEvaluator := yqlib.NewStreamEvaluator()

View File

@ -63,6 +63,9 @@ See https://mikefarah.gitbook.io/yq/ for detailed documentation and examples.`,
rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors") rootCmd.PersistentFlags().BoolVarP(&forceNoColor, "no-colors", "M", false, "force print with no colors")
rootCmd.PersistentFlags().StringVarP(&frontMatter, "front-matter", "f", "", "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact") rootCmd.PersistentFlags().StringVarP(&frontMatter, "front-matter", "f", "", "(extract|process) first input as yaml front-matter. Extract will pull out the yaml content, process will run the expression against the yaml content, leaving the remaining data intact")
rootCmd.PersistentFlags().BoolVarP(&leadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression. This is a workaround for go-yaml to persist header content properly.") rootCmd.PersistentFlags().BoolVarP(&leadingContentPreProcessing, "header-preprocess", "", true, "Slurp any header comments and separators before processing expression. This is a workaround for go-yaml to persist header content properly.")
rootCmd.PersistentFlags().StringVarP(&splitFileExp, "split-exp", "s", "", "print each result (or doc) into a file named (exp). Set to 'i' if you'd like to have numbered output.")
rootCmd.AddCommand( rootCmd.AddCommand(
createEvaluateSequenceCommand(), createEvaluateSequenceCommand(),
createEvaluateAllCommand(), createEvaluateAllCommand(),

5
go.mod
View File

@ -13,13 +13,8 @@ require (
) )
require ( require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/timtadh/data-structures v0.5.3 // indirect github.com/timtadh/data-structures v0.5.3 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
) )
go 1.17 go 1.17

View File

@ -13,7 +13,8 @@ import (
func yamlToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) { func yamlToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {
var output bytes.Buffer var output bytes.Buffer
log.Debug("printing with indent: %v", prefs.indent) log.Debug("printing with indent: %v", prefs.indent)
printer := NewPrinter(bufio.NewWriter(&output), prefs.format, true, false, prefs.indent, true)
printer := NewPrinterWithSingleWriter(bufio.NewWriter(&output), prefs.format, true, false, prefs.indent, true)
err := printer.PrintResults(candidate.AsList()) err := printer.PrintResults(candidate.AsList())
return output.String(), err return output.String(), err
} }

View File

@ -44,7 +44,7 @@ type resultsPrinter struct {
colorsEnabled bool colorsEnabled bool
indent int indent int
printDocSeparators bool printDocSeparators bool
printerWriter printerWriter printerWriter PrinterWriter
firstTimePrinting bool firstTimePrinting bool
previousDocIndex uint previousDocIndex uint
previousFileIndex int previousFileIndex int
@ -53,7 +53,11 @@ type resultsPrinter struct {
appendixReader io.Reader appendixReader io.Reader
} }
func NewPrinter(printerWriter printerWriter, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer { func NewPrinterWithSingleWriter(writer io.Writer, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
return NewPrinter(NewSinglePrinterWriter(writer), outputFormat, unwrapScalar, colorsEnabled, indent, printDocSeparators)
}
func NewPrinter(printerWriter PrinterWriter, outputFormat PrinterOutputFormat, unwrapScalar bool, colorsEnabled bool, indent int, printDocSeparators bool) Printer {
return &resultsPrinter{ return &resultsPrinter{
printerWriter: printerWriter, printerWriter: printerWriter,
outputFormat: outputFormat, outputFormat: outputFormat,
@ -212,10 +216,14 @@ func (p *resultsPrinter) PrintResults(matchingNodes *list.List) error {
} }
if p.appendixReader != nil && p.outputFormat == YamlOutputFormat { if p.appendixReader != nil && p.outputFormat == YamlOutputFormat {
writer := p.printerWriter.GetWriter(nil, index) writer, err := p.printerWriter.GetWriter(nil, index)
if err != nil {
return err
}
log.Debug("Piping appendix reader...") log.Debug("Piping appendix reader...")
betterReader := bufio.NewReader(p.appendixReader) betterReader := bufio.NewReader(p.appendixReader)
_, err := io.Copy(writer, betterReader) _, err = io.Copy(writer, betterReader)
if err != nil { if err != nil {
return err return err
} }

View File

@ -36,7 +36,7 @@ func nodeToList(candidate *CandidateNode) *list.List {
func TestPrinterMultipleDocsInSequence(t *testing.T) { func TestPrinterMultipleDocsInSequence(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -74,7 +74,7 @@ func TestPrinterMultipleDocsInSequence(t *testing.T) {
func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -116,7 +116,7 @@ func TestPrinterMultipleDocsInSequenceWithLeadingContent(t *testing.T) {
func TestPrinterMultipleFilesInSequence(t *testing.T) { func TestPrinterMultipleFilesInSequence(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -163,7 +163,7 @@ func TestPrinterMultipleFilesInSequence(t *testing.T) {
func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) { func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -213,7 +213,7 @@ func TestPrinterMultipleFilesInSequenceWithLeadingContent(t *testing.T) {
func TestPrinterMultipleDocsInSinglePrint(t *testing.T) { func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -232,7 +232,7 @@ func TestPrinterMultipleDocsInSinglePrint(t *testing.T) {
func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) { func TestPrinterMultipleDocsInSinglePrintWithLeadingDoc(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -261,7 +261,7 @@ a: coconut
func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) { func TestPrinterMultipleDocsInSinglePrintWithLeadingDocTrailing(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {
@ -287,7 +287,7 @@ a: coconut
func TestPrinterScalarWithLeadingCont(t *testing.T) { func TestPrinterScalarWithLeadingCont(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
printer := NewPrinter(writer, YamlOutputFormat, true, false, 2, true) printer := NewPrinterWithSingleWriter(writer, YamlOutputFormat, true, false, 2, true)
node, err := NewExpressionParser().ParseExpression(".a") node, err := NewExpressionParser().ParseExpression(".a")
if err != nil { if err != nil {
@ -314,7 +314,7 @@ func TestPrinterMultipleDocsJson(t *testing.T) {
var writer = bufio.NewWriter(&output) var writer = bufio.NewWriter(&output)
// note printDocSeparators is true, it should still not print document separators // note printDocSeparators is true, it should still not print document separators
// when outputing JSON. // when outputing JSON.
printer := NewPrinter(writer, JsonOutputFormat, true, false, 0, true) printer := NewPrinterWithSingleWriter(writer, JsonOutputFormat, true, false, 0, true)
inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0) inputs, err := readDocuments(strings.NewReader(multiDocSample), "sample.yml", 0)
if err != nil { if err != nil {

View File

@ -4,9 +4,10 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"os"
) )
type printerWriter interface { type PrinterWriter interface {
GetWriter(node *CandidateNode, index int) (*bufio.Writer, error) GetWriter(node *CandidateNode, index int) (*bufio.Writer, error)
} }
@ -14,7 +15,7 @@ type singlePrinterWriter struct {
bufferedWriter *bufio.Writer bufferedWriter *bufio.Writer
} }
func NewSinglePrinterWriter(writer io.Writer) printerWriter { func NewSinglePrinterWriter(writer io.Writer) PrinterWriter {
return &singlePrinterWriter{ return &singlePrinterWriter{
bufferedWriter: bufio.NewWriter(writer), bufferedWriter: bufio.NewWriter(writer),
} }
@ -30,6 +31,23 @@ type multiPrintWriter struct {
extension string extension string
} }
func NewMultiPrinterWriter(expression *ExpressionNode, format PrinterOutputFormat) PrinterWriter {
extension := "yml"
switch format {
case JsonOutputFormat:
extension = "json"
case PropsOutputFormat:
extension = "properties"
}
return &multiPrintWriter{
nameExpression: expression,
extension: extension,
treeNavigator: NewDataTreeNavigator(),
}
}
func (sp *multiPrintWriter) GetWriter(node *CandidateNode, index int) (*bufio.Writer, error) { func (sp *multiPrintWriter) GetWriter(node *CandidateNode, index int) (*bufio.Writer, error) {
name := "" name := ""
@ -49,4 +67,12 @@ func (sp *multiPrintWriter) GetWriter(node *CandidateNode, index int) (*bufio.Wr
name = fmt.Sprintf("%v.%v", name, sp.extension) name = fmt.Sprintf("%v.%v", name, sp.extension)
} }
f, err := os.Create(name)
if err != nil {
return nil, err
}
return bufio.NewWriter(f), nil
} }