diff --git a/1 b/1 new file mode 100644 index 00000000..377f7756 --- /dev/null +++ b/1 @@ -0,0 +1 @@ +Error: cannot pass files in when using null-input flag diff --git a/acceptance_tests/bad_args.sh b/acceptance_tests/bad_args.sh new file mode 100755 index 00000000..3517421a --- /dev/null +++ b/acceptance_tests/bad_args.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +tearDown() { + set -e +} + +testWriteInPlacePipeIn() { + set +e + result=$(./yq e -i -n '.a' 2>&1) + assertEquals 1 $? + assertEquals "Error: write inplace flag only applicable when giving an expression and at least one file" "$result" +} + +testWriteInPlacePipeInEvalall() { + set +e + result=$(./yq ea -i -n '.a' 2>&1) + assertEquals 1 $? + assertEquals "Error: write inplace flag only applicable when giving an expression and at least one file" "$result" +} + +testWriteInPlaceWithSplit() { + set +e + result=$(./yq e -s "cat" -i '.a = "thing"' test.yml 2>&1) + assertEquals 1 $? + assertEquals "Error: write inplace cannot be used with split file" "$result" +} + +testWriteInPlaceWithSplitEvalAll() { + set +e + result=$(./yq ea -s "cat" -i '.a = "thing"' test.yml 2>&1) + assertEquals 1 $? + assertEquals "Error: write inplace cannot be used with split file" "$result" +} + +testNullWithFiles() { + set +e + result=$(./yq e -n '.a = "thing"' test.yml 2>&1) + assertEquals 1 $? + assertEquals "Error: cannot pass files in when using null-input flag" "$result" +} + +testNullWithFilesEvalAll() { + set +e + result=$(./yq ea -n '.a = "thing"' test.yml 2>&1) + assertEquals 1 $? + assertEquals "Error: cannot pass files in when using null-input flag" "$result" +} + + + +source ./scripts/shunit2 \ No newline at end of file diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index d8dc02c8..af3c6ec7 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -2,7 +2,6 @@ package cmd import ( "errors" - "fmt" "os" "github.com/mikefarah/yq/v4/pkg/yqlib" @@ -41,39 +40,23 @@ Note that it consumes more memory than eval. return cmdEvalAll } func evaluateAll(cmd *cobra.Command, args []string) error { - cmd.SilenceUsage = true // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence // 2+ args, [0] = expression, file the rest var err error + + firstFileIndex, err := initCommand(cmd, args) + if err != nil { + return err + } + stat, _ := os.Stdin.Stat() pipingStdIn := (stat.Mode() & os.ModeCharDevice) == 0 out := cmd.OutOrStdout() - fileInfo, _ := os.Stdout.Stat() - - if forceColor || (!forceNoColor && (fileInfo.Mode()&os.ModeCharDevice) != 0) { - colorsEnabled = true - } - - 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 && splitFileExp != "" { - return fmt.Errorf("Write inplace cannot be used with split file") - } - if writeInplace { // only use colors if its forced colorsEnabled = forceColor @@ -87,14 +70,6 @@ func evaluateAll(cmd *cobra.Command, args []string) error { defer func() { writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) }() } - if nullInput && len(args) > 1 { - return errors.New("Cannot pass files in when using null-input flag") - } - // backwards compatibilty - if outputToJSON { - outputFormat = "json" - } - format, err := yqlib.OutputFormatFromString(outputFormat) if err != nil { return err diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index c2b32d79..e875c4bd 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -54,39 +54,22 @@ func processExpression(expression string) string { } func evaluateSequence(cmd *cobra.Command, args []string) error { - cmd.SilenceUsage = true // 0 args, read std in // 1 arg, null input, process expression // 1 arg, read file in sequence // 2+ args, [0] = expression, file the rest var err error + firstFileIndex, err := initCommand(cmd, args) + if err != nil { + return err + } + stat, _ := os.Stdin.Stat() pipingStdIn := (stat.Mode() & os.ModeCharDevice) == 0 out := cmd.OutOrStdout() - fileInfo, _ := os.Stdout.Stat() - - if forceColor || (!forceNoColor && (fileInfo.Mode()&os.ModeCharDevice) != 0) { - colorsEnabled = true - } - - 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 && splitFileExp != "" { - return fmt.Errorf("Write inplace cannot be used with split file") - } - if writeInplace { // only use colors if its forced colorsEnabled = forceColor @@ -100,11 +83,6 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { defer func() { writeInPlaceHandler.FinishWriteInPlace(completedSuccessfully) }() } - // backwards compatibilty - if outputToJSON { - outputFormat = "json" - } - format, err := yqlib.OutputFormatFromString(outputFormat) if err != nil { return err @@ -116,10 +94,6 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { streamEvaluator := yqlib.NewStreamEvaluator() - if nullInput && len(args) > 1 { - return errors.New("Cannot pass files in when using null-input flag") - } - if frontMatter != "" { frontMatterHandler := yqlib.NewFrontMatterHandler(args[firstFileIndex]) err = frontMatterHandler.Split() diff --git a/cmd/printer_utils.go b/cmd/printer_utils.go deleted file mode 100644 index 9f638bff..00000000 --- a/cmd/printer_utils.go +++ /dev/null @@ -1,24 +0,0 @@ -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 != "" { - 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 -} diff --git a/cmd/utils.go b/cmd/utils.go new file mode 100644 index 00000000..80a2b295 --- /dev/null +++ b/cmd/utils.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "fmt" + "io" + "os" + + "github.com/mikefarah/yq/v4/pkg/yqlib" + "github.com/spf13/cobra" +) + +func initCommand(cmd *cobra.Command, args []string) (firstFileIndex int, err error) { + cmd.SilenceUsage = true + + fileInfo, _ := os.Stdout.Stat() + + if forceColor || (!forceNoColor && (fileInfo.Mode()&os.ModeCharDevice) != 0) { + colorsEnabled = true + } + + firstFileIndex = -1 + if !nullInput && len(args) == 1 { + firstFileIndex = 0 + } else if len(args) > 1 { + firstFileIndex = 1 + } + + // backwards compatibilty + if outputToJSON { + outputFormat = "json" + } + + if writeInplace && (firstFileIndex == -1) { + return 0, fmt.Errorf("write inplace flag only applicable when giving an expression and at least one file") + } + + if writeInplace && splitFileExp != "" { + return 0, fmt.Errorf("write inplace cannot be used with split file") + } + + if nullInput && len(args) > 1 { + return 0, fmt.Errorf("cannot pass files in when using null-input flag") + } + + return firstFileIndex, nil +} + +func configurePrinterWriter(format yqlib.PrinterOutputFormat, out io.Writer) yqlib.PrinterWriter { + + var printerWriter yqlib.PrinterWriter + + 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 +}