diff --git a/cmd/constant.go b/cmd/constant.go index daacf6fa..8b94dc2c 100644 --- a/cmd/constant.go +++ b/cmd/constant.go @@ -8,7 +8,7 @@ var writeInplace = false var outputToJSON = false var outputFormat = "yaml" var inputFormatDefault = "yaml" -var inputFormat = "" // default to "" so that we can (lazily) detect inputFormat from input filename +var inputFormat = "" var exitStatus = false var forceColor = false diff --git a/cmd/root.go b/cmd/root.go index d832aca2..76782ccb 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|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().StringVarP(&inputFormat, "input-format", "p", "", "[yaml|y|props|p|xml|x] parse format for input. Note that json is a subset of yaml.") 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 3da4de41..cc20a827 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "strings" "github.com/mikefarah/yq/v4/pkg/yqlib" "github.com/spf13/cobra" @@ -58,13 +57,10 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, 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 - } + if inputFormat == "" { + inputFormat = yqlib.InputFormatFromFilename(inputFilename, inputFormatDefault) + } else { + yqlib.GetLogger().Debugf("user specified inputFormat '%s'", inputFormat) } yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat) if err != nil { diff --git a/pkg/yqlib/decoder.go b/pkg/yqlib/decoder.go index dbc1d768..c06a5a72 100644 --- a/pkg/yqlib/decoder.go +++ b/pkg/yqlib/decoder.go @@ -3,6 +3,7 @@ package yqlib import ( "fmt" "io" + "strings" ) type InputFormat uint @@ -29,7 +30,7 @@ func InputFormatFromString(format string) (InputFormat, error) { return YamlInputFormat, nil case "xml", "x": return XMLInputFormat, nil - case "props", "p": + case "properties", "props", "p": return PropertiesInputFormat, nil case "json", "ndjson", "j": return JsonInputFormat, nil @@ -41,3 +42,18 @@ func InputFormatFromString(format string) (InputFormat, error) { return 0, fmt.Errorf("unknown format '%v' please use [yaml|xml|props]", format) } } + +func InputFormatFromFilename(filename string, defaultFormat string) string { + if filename != "" { + GetLogger().Debugf("checking filename '%s' for inputFormat", filename) + nPos := strings.LastIndex(filename, ".") + if nPos > -1 { + inputFormat := filename[nPos+1:] + GetLogger().Debugf("detected inputFormat '%s'", inputFormat) + return inputFormat + } + } + + GetLogger().Debugf("using default inputFormat '%s'", defaultFormat) + return defaultFormat +}