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 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
|
||||
|
||||
@ -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).")
|
||||
|
||||
10
cmd/utils.go
10
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:]
|
||||
if inputFormat == "" {
|
||||
inputFormat = yqlib.InputFormatFromFilename(inputFilename, inputFormatDefault)
|
||||
} else {
|
||||
inputFormat = inputFormatDefault
|
||||
}
|
||||
yqlib.GetLogger().Debugf("user specified inputFormat '%s'", inputFormat)
|
||||
}
|
||||
yqlibInputFormat, err := yqlib.InputFormatFromString(inputFormat)
|
||||
if err != nil {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user