Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 10, 2022
1 parent 1d35134 commit cad8095
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 50 deletions.
47 changes: 1 addition & 46 deletions cmd/constant.go
Original file line number Diff line number Diff line change
@@ -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

Expand Down
17 changes: 13 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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() {
Expand All @@ -72,6 +79,8 @@ yq -P sample.json
yqlib.ConfiguredYamlPreferences.UnwrapScalar = unwrapScalar

yqlib.ConfiguredYamlPreferences.PrintDocSeparators = !noDocSeparators

return nil
},
}

Expand Down
46 changes: 46 additions & 0 deletions cmd/unwrap_flag.go
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit cad8095

Please sign in to comment.