From 0328cfd6191b052f7aeed3cfe17ac0fa8c70b1c5 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Mon, 28 Dec 2020 11:40:41 +1100 Subject: [PATCH] Added prettyPrint flag --- cmd/constant.go | 1 + cmd/evaluate_all_command.go | 8 ++++---- cmd/evalute_sequence_command.go | 16 +++++++++++++--- cmd/root.go | 1 + cmd/version.go | 2 +- pkg/yqlib/lib.go | 2 +- snap/snapcraft.yaml | 2 +- 7 files changed, 22 insertions(+), 10 deletions(-) diff --git a/cmd/constant.go b/cmd/constant.go index fb8f94d4..fb73fa9e 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -14,5 +14,6 @@ var noDocSeparators = false var nullInput = false var verbose = false var version = false +var prettyPrint = false var completedSuccessfully = false diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index fc7b4ec2..0942380a 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -65,19 +65,19 @@ func evaluateAll(cmd *cobra.Command, args []string) error { switch len(args) { case 0: if pipingStdIn { - err = allAtOnceEvaluator.EvaluateFiles("", []string{"-"}, printer) + err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer) } else { cmd.Println(cmd.UsageString()) return nil } case 1: if nullInput { - err = yqlib.NewStreamEvaluator().EvaluateNew(args[0], printer) + err = yqlib.NewStreamEvaluator().EvaluateNew(processExpression(args[0]), printer) } else { - err = allAtOnceEvaluator.EvaluateFiles("", []string{args[0]}, printer) + err = allAtOnceEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer) } default: - err = allAtOnceEvaluator.EvaluateFiles(args[0], args[1:], printer) + err = allAtOnceEvaluator.EvaluateFiles(processExpression(args[0]), args[1:], printer) } completedSuccessfully = err == nil diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index c9eabeb8..2e67687e 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -33,6 +33,16 @@ yq e '.a.b = "cool"' -i file.yaml } return cmdEvalSequence } + +func processExpression(expression string) string { + if prettyPrint && expression == "" { + return `... style=""` + } else if prettyPrint { + return fmt.Sprintf("%v | ... style= \"\"", expression) + } + return expression +} + func evaluateSequence(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true // 0 args, read std in @@ -76,16 +86,16 @@ func evaluateSequence(cmd *cobra.Command, args []string) error { switch len(args) { case 0: if pipingStdIn { - err = streamEvaluator.EvaluateFiles("", []string{"-"}, printer) + err = streamEvaluator.EvaluateFiles(processExpression(""), []string{"-"}, printer) } else { cmd.Println(cmd.UsageString()) return nil } case 1: if nullInput { - err = streamEvaluator.EvaluateNew(args[0], printer) + err = streamEvaluator.EvaluateNew(processExpression(args[0]), printer) } else { - err = streamEvaluator.EvaluateFiles("", []string{args[0]}, printer) + err = streamEvaluator.EvaluateFiles(processExpression(""), []string{args[0]}, printer) } default: err = streamEvaluator.EvaluateFiles(args[0], args[1:], printer) diff --git a/cmd/root.go b/cmd/root.go index 9f380d4f..da53ede8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -48,6 +48,7 @@ func New() *cobra.Command { rootCmd.Flags().BoolVarP(&version, "version", "V", false, "Print version information and quit") rootCmd.PersistentFlags().BoolVarP(&writeInplace, "inplace", "i", false, "update the yaml file inplace of first yaml file given.") rootCmd.PersistentFlags().BoolVarP(&unwrapScalar, "unwrapScalar", "", true, "unwrap scalar, print the value with no quotes, colors or comments") + rootCmd.PersistentFlags().BoolVarP(&prettyPrint, "prettyPrint", "P", false, "pretty print, shorthand for '... style = \"\"'") rootCmd.PersistentFlags().BoolVarP(&exitStatus, "exit-status", "e", false, "set exit status if there are no matches or null or false is returned") rootCmd.PersistentFlags().BoolVarP(&forceColor, "colors", "C", false, "force print with colors") diff --git a/cmd/version.go b/cmd/version.go index 59bd7f13..6f2d6b52 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -11,7 +11,7 @@ var ( GitDescribe string // Version is main version number that is being run at the moment. - Version = "4.1.0" + Version = "4.2.0" // VersionPrerelease is a pre-release marker for the version. If this is "" (empty string) // then it means that it is a final release. Otherwise, this is a pre-release diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 93089993..56ae6488 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -19,7 +19,7 @@ type OperationType struct { } // operators TODO: -// - cookbook doc for common things +// - keys operator for controlling key metadata (particularly anchors/aliases) // - mergeEmpty (sets only if the document is empty, do I do that now?) var Or = &OperationType{Type: "OR", NumArgs: 2, Precedence: 20, Handler: OrOperator} diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index c6390f4d..569bfb76 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -1,5 +1,5 @@ name: yq -version: '4.1.0' +version: '4.2.0' summary: A lightweight and portable command-line YAML processor description: | The aim of the project is to be the jq or sed of yaml files.