wip - write in place

This commit is contained in:
Mike Farah
2020-11-29 20:25:47 +11:00
parent 1258fa199e
commit 8de10e550d
10 changed files with 137 additions and 729 deletions
+20
View File
@@ -1,6 +1,7 @@
package cmd
import (
"fmt"
"os"
"github.com/mikefarah/yq/v4/pkg/yqlib"
@@ -39,7 +40,24 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
if forceColor || (!forceNoColor && (fileInfo.Mode()&os.ModeCharDevice) != 0) {
colorsEnabled = true
}
if writeInplace && len(args) < 2 {
return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file")
}
completedSuccessfully := false
if writeInplace {
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1])
out, err = writeInPlaceHandler.CreateTempFile()
if err != nil {
return err
}
defer writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully)
}
printer := yqlib.NewPrinter(out, outputToJSON, unwrapScalar, colorsEnabled, indent, !noDocSeparators)
allAtOnceEvaluator := yqlib.NewAllAtOnceEvaluator()
switch len(args) {
case 0:
@@ -59,6 +77,8 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
err = allAtOnceEvaluator.EvaluateFiles(args[0], args[1:], printer)
}
completedSuccessfully = err == nil
cmd.SilenceUsage = true
return err
}