mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 16:55:40 +00:00
refactor and extract func InputFormatFromFilename
This commit is contained in:
parent
66af38d4ca
commit
b7561adfa6
@ -8,7 +8,7 @@ var writeInplace = false
|
|||||||
var outputToJSON = false
|
var outputToJSON = false
|
||||||
var outputFormat = "yaml"
|
var outputFormat = "yaml"
|
||||||
var inputFormatDefault = "yaml"
|
var inputFormatDefault = "yaml"
|
||||||
var inputFormat = "" // default to "" so that we can (lazily) detect inputFormat from input filename
|
var inputFormat = ""
|
||||||
|
|
||||||
var exitStatus = false
|
var exitStatus = false
|
||||||
var forceColor = false
|
var forceColor = false
|
||||||
|
|||||||
@ -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|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.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).")
|
||||||
|
|||||||
12
cmd/utils.go
12
cmd/utils.go
@ -4,7 +4,6 @@ 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"
|
||||||
@ -58,13 +57,10 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func configureDecoder(evaluateTogether bool, inputFilename string) (yqlib.Decoder, error) {
|
func configureDecoder(evaluateTogether bool, inputFilename string) (yqlib.Decoder, error) {
|
||||||
if inputFormat == "" && inputFilename != "" {
|
if inputFormat == "" {
|
||||||
nPos := strings.LastIndex(inputFilename, ".")
|
inputFormat = yqlib.InputFormatFromFilename(inputFilename, inputFormatDefault)
|
||||||
if nPos > -1 {
|
} else {
|
||||||
inputFormat = inputFilename[nPos+1:]
|
yqlib.GetLogger().Debugf("user specified inputFormat '%s'", inputFormat)
|
||||||
} else {
|
|
||||||
inputFormat = inputFormatDefault
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
|
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package yqlib
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InputFormat uint
|
type InputFormat uint
|
||||||
@ -29,7 +30,7 @@ func InputFormatFromString(format string) (InputFormat, error) {
|
|||||||
return YamlInputFormat, nil
|
return YamlInputFormat, nil
|
||||||
case "xml", "x":
|
case "xml", "x":
|
||||||
return XMLInputFormat, nil
|
return XMLInputFormat, nil
|
||||||
case "props", "p":
|
case "properties", "props", "p":
|
||||||
return PropertiesInputFormat, nil
|
return PropertiesInputFormat, nil
|
||||||
case "json", "ndjson", "j":
|
case "json", "ndjson", "j":
|
||||||
return JsonInputFormat, nil
|
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)
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user