From cad809515cc935a29a0e17e157c1be9266ca5376 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Thu, 10 Nov 2022 22:07:53 +1100 Subject: [PATCH] Refactoring --- cmd/constant.go | 47 +--------------------------------------------- cmd/root.go | 17 +++++++++++++---- cmd/unwrap_flag.go | 46 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 cmd/unwrap_flag.go diff --git a/cmd/constant.go b/cmd/constant.go index 1c6e216bbdc..573a72fbd50 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -1,51 +1,6 @@ package cmd -import ( - "strconv" - - "github.com/spf13/pflag" -) - -type boolFlag interface { - pflag.Value - IsExplicitySet() bool - IsSet() bool -} - -type unwrapScalarFlagStrc struct { - explicitySet bool - value bool -} - -func newFlag() boolFlag { - return &unwrapScalarFlagStrc{value: true} -} - -func (f *unwrapScalarFlagStrc) IsExplicitySet() bool { - return f.explicitySet -} - -func (f *unwrapScalarFlagStrc) IsSet() bool { - return f.value -} - -func (f *unwrapScalarFlagStrc) String() string { - return strconv.FormatBool(f.value) -} - -func (f *unwrapScalarFlagStrc) Set(value string) error { - - v, err := strconv.ParseBool(value) - f.value = v - f.explicitySet = true - return err -} - -func (*unwrapScalarFlagStrc) Type() string { - return "bool" -} - -var unwrapScalarFlag = newFlag() +var unwrapScalarFlag = newUnwrapFlag() var unwrapScalar = false diff --git a/cmd/root.go b/cmd/root.go index 558a90b46e9..dbde993e712 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -35,7 +35,7 @@ yq -P sample.json return evaluateSequence(cmd, args) }, - PersistentPreRun: func(cmd *cobra.Command, args []string) { + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { cmd.SetOut(cmd.OutOrStdout()) var format = logging.MustStringFormatter( @@ -52,16 +52,23 @@ yq -P sample.json logging.SetBackend(backend) yqlib.InitExpressionParser() + + outputFormatType, err := yqlib.OutputFormatFromString(outputFormat) + + if err != nil { + return err + } + if (inputFormat == "x" || inputFormat == "xml") && - outputFormat != "x" && outputFormat != "xml" && + outputFormatType != yqlib.XMLOutputFormat && yqlib.ConfiguredXMLPreferences.AttributePrefix == "+" { yqlib.GetLogger().Warning("The default xml-attribute-prefix will change in the v4.30 to `+@` to avoid " + "naming conflicts with the default content name, directive name and proc inst prefix. If you need to keep " + "`+` please set that value explicityly with --xml-attribute-prefix.") } - if outputFormat == "y" || outputFormat == "yaml" || - outputFormat == "p" || outputFormat == "props" { + if outputFormatType == yqlib.YamlOutputFormat || + outputFormatType == yqlib.PropsOutputFormat { unwrapScalar = true } if unwrapScalarFlag.IsExplicitySet() { @@ -72,6 +79,8 @@ yq -P sample.json yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators + + return nil }, } diff --git a/cmd/unwrap_flag.go b/cmd/unwrap_flag.go new file mode 100644 index 00000000000..e844f2fa37b --- /dev/null +++ b/cmd/unwrap_flag.go @@ -0,0 +1,46 @@ +package cmd + +import ( + "strconv" + + "github.com/spf13/pflag" +) + +type boolFlag interface { + pflag.Value + IsExplicitySet() bool + IsSet() bool +} + +type unwrapScalarFlagStrc struct { + explicitySet bool + value bool +} + +func newUnwrapFlag() boolFlag { + return &unwrapScalarFlagStrc{value: true} +} + +func (f *unwrapScalarFlagStrc) IsExplicitySet() bool { + return f.explicitySet +} + +func (f *unwrapScalarFlagStrc) IsSet() bool { + return f.value +} + +func (f *unwrapScalarFlagStrc) String() string { + return strconv.FormatBool(f.value) +} + +func (f *unwrapScalarFlagStrc) Set(value string) error { + + v, err := strconv.ParseBool(value) + f.value = v + f.explicitySet = true + return err +} + +func (*unwrapScalarFlagStrc) Type() string { + return "bool" +}