From 66af38d4ca237cccc81aed901261ff2045acfac8 Mon Sep 17 00:00:00 2001 From: ryenus Date: Thu, 2 Mar 2023 17:04:34 +0800 Subject: [PATCH] detect inputFormat from filename --- cmd/constant.go | 3 ++- cmd/evaluate_all_command.go | 2 +- cmd/evalute_sequence_command.go | 2 +- cmd/root.go | 2 +- cmd/utils.go | 11 ++++++++++- pkg/yqlib/decoder.go | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/constant.go b/cmd/constant.go index 573a72fb..daacf6fa 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -7,7 +7,8 @@ var unwrapScalar = false var writeInplace = false var outputToJSON = false var outputFormat = "yaml" -var inputFormat = "yaml" +var inputFormatDefault = "yaml" +var inputFormat = "" // default to "" so that we can (lazily) detect inputFormat from input filename var exitStatus = false var forceColor = false diff --git a/cmd/evaluate_all_command.go b/cmd/evaluate_all_command.go index fff64fa5..d3589cb8 100644 --- a/cmd/evaluate_all_command.go +++ b/cmd/evaluate_all_command.go @@ -75,7 +75,7 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) { return err } - decoder, err := configureDecoder(true) + decoder, err := configureDecoder(true, args[0]) if err != nil { return err } diff --git a/cmd/evalute_sequence_command.go b/cmd/evalute_sequence_command.go index dab803c3..94df3b56 100644 --- a/cmd/evalute_sequence_command.go +++ b/cmd/evalute_sequence_command.go @@ -100,7 +100,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) { printer := yqlib.NewPrinter(encoder, printerWriter) - decoder, err := configureDecoder(false) + decoder, err := configureDecoder(false, args[0]) if err != nil { return err } diff --git a/cmd/root.go b/cmd/root.go index 97d9beaf..d832aca2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -85,7 +85,7 @@ yq -P sample.json } rootCmd.PersistentFlags().StringVarP(&outputFormat, "output-format", "o", "yaml", "[yaml|y|json|j|props|p|xml|x] output format type.") - rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "yaml", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.") + rootCmd.PersistentFlags().StringVarP(&inputFormat, "input-format", "p", "", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.") // use "" as default instead of "yaml", to be able to detect from input filename rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.AttributePrefix, "xml-attribute-prefix", yqlib.ConfiguredXMLPreferences.AttributePrefix, "prefix for xml attributes") rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", yqlib.ConfiguredXMLPreferences.ContentName, "name for xml content (if no attribute name is present).") diff --git a/cmd/utils.go b/cmd/utils.go index 671dd86b..3da4de41 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "strings" "github.com/mikefarah/yq/v4/pkg/yqlib" "github.com/spf13/cobra" @@ -56,7 +57,15 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) { return expression, args, nil } -func configureDecoder(evaluateTogether bool) (yqlib.Decoder, error) { +func configureDecoder(evaluateTogether bool, inputFilename string) (yqlib.Decoder, error) { + if inputFormat == "" && inputFilename != "" { + nPos := strings.LastIndex(inputFilename, ".") + if nPos > -1 { + inputFormat = inputFilename[nPos+1:] + } else { + inputFormat = inputFormatDefault + } + } yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat) if err != nil { return nil, err diff --git a/pkg/yqlib/decoder.go b/pkg/yqlib/decoder.go index ff7ab2db..dbc1d768 100644 --- a/pkg/yqlib/decoder.go +++ b/pkg/yqlib/decoder.go @@ -25,7 +25,7 @@ type Decoder interface { func InputFormatFromString(format string) (InputFormat, error) { switch format { - case "yaml", "y": + case "yaml", "yml", "y": return YamlInputFormat, nil case "xml", "x": return XMLInputFormat, nil