mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 16:55:40 +00:00
detect inputFormat from filename
This commit is contained in:
parent
fed96f67ea
commit
66af38d4ca
@ -7,7 +7,8 @@ var unwrapScalar = false
|
|||||||
var writeInplace = false
|
var writeInplace = false
|
||||||
var outputToJSON = false
|
var outputToJSON = false
|
||||||
var outputFormat = "yaml"
|
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 exitStatus = false
|
||||||
var forceColor = false
|
var forceColor = false
|
||||||
|
|||||||
@ -75,7 +75,7 @@ func evaluateAll(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder, err := configureDecoder(true)
|
decoder, err := configureDecoder(true, args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -100,7 +100,7 @@ func evaluateSequence(cmd *cobra.Command, args []string) (cmdError error) {
|
|||||||
|
|
||||||
printer := yqlib.NewPrinter(encoder, printerWriter)
|
printer := yqlib.NewPrinter(encoder, printerWriter)
|
||||||
|
|
||||||
decoder, err := configureDecoder(false)
|
decoder, err := configureDecoder(false, args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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(&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.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).")
|
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredXMLPreferences.ContentName, "xml-content-name", yqlib.ConfiguredXMLPreferences.ContentName, "name for xml content (if no attribute name is present).")
|
||||||
|
|||||||
11
cmd/utils.go
11
cmd/utils.go
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/mikefarah/yq/v4/pkg/yqlib"
|
"github.com/mikefarah/yq/v4/pkg/yqlib"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -56,7 +57,15 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
|||||||
return expression, args, nil
|
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)
|
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@ -25,7 +25,7 @@ type Decoder interface {
|
|||||||
|
|
||||||
func InputFormatFromString(format string) (InputFormat, error) {
|
func InputFormatFromString(format string) (InputFormat, error) {
|
||||||
switch format {
|
switch format {
|
||||||
case "yaml", "y":
|
case "yaml", "yml", "y":
|
||||||
return YamlInputFormat, nil
|
return YamlInputFormat, nil
|
||||||
case "xml", "x":
|
case "xml", "x":
|
||||||
return XMLInputFormat, nil
|
return XMLInputFormat, nil
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user