From bd0818c48100ed187ade7ee979e762d86807919d Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Wed, 10 Feb 2021 17:06:16 +1100 Subject: [PATCH] Fixed write-inplace with no expression --- cmd/evaluate_all_command.go | 11 +++++++++-- cmd/evalute_sequence_command.go | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index 68c9a14d..44801f69 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -42,14 +42,21 @@ func evaluateAll(cmd *cobra.Command, args []string) error { colorsEnabled = true } - if writeInplace && len(args) < 2 { + firstFileIndex := -1 + if !nullInput && len(args) == 1 { + firstFileIndex = 0 + } else if len(args) > 1 { + firstFileIndex = 1 + } + + if writeInplace && (firstFileIndex == -1) { return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file") } if writeInplace { // only use colors if its forced colorsEnabled = forceColor - writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1]) + writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex]) out, err = writeInPlaceHandler.CreateTempFile() if err != nil { return err diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index 54ce2eb1..7f17df26 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -62,14 +62,21 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { colorsEnabled = true } - if writeInplace && len(args) < 2 { + firstFileIndex := -1 + if !nullInput && len(args) == 1 { + firstFileIndex = 0 + } else if len(args) > 1 { + firstFileIndex = 1 + } + + if writeInplace && (firstFileIndex == -1) { return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file") } if writeInplace { // only use colors if its forced colorsEnabled = forceColor - writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1]) + writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex]) out, err = writeInPlaceHandler.CreateTempFile() if err != nil { return err