wip split

This commit is contained in:
Mike Farah 2021-10-29 20:53:28 +11:00
parent c2d9b98aeb
commit 3a7fd8f2c4
3 changed files with 36 additions and 17 deletions

View File

@ -100,22 +100,7 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
return err
}
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)
}
printerWriter := configurePrinterWriter(format, out)
printer := yqlib.NewPrinter(printerWriter, format, unwrapScalar, colorsEnabled, indent, !noDocSeparators)

View File

@ -83,6 +83,10 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
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 {
// only use colors if its forced
colorsEnabled = forceColor
@ -106,7 +110,9 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
return err
}
printer := yqlib.NewPrinter(yqlib.NewSinglePrinterWriter(out), format, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
printerWriter := configurePrinterWriter(format, out)
printer := yqlib.NewPrinter(printerWriter, format, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
streamEvaluator := yqlib.NewStreamEvaluator()

28
cmd/printer_utils.go Normal file
View File

@ -0,0 +1,28 @@
package cmd
import (
"io"
"github.com/mikefarah/yq/v4/pkg/yqlib"
)
func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) yqlib.PrinterWriter {
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)
}
return printerWriter
}