mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Unknown file type should default to yaml, Fixes #1609
This commit is contained in:
parent
7305b50ffe
commit
48b481b68d
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
setUp() {
|
setUp() {
|
||||||
rm test*.yml 2>/dev/null || true
|
rm test*.yml 2>/dev/null || true
|
||||||
|
rm test*.tfstate 2>/dev/null || true
|
||||||
rm test*.json 2>/dev/null || true
|
rm test*.json 2>/dev/null || true
|
||||||
rm test*.properties 2>/dev/null || true
|
rm test*.properties 2>/dev/null || true
|
||||||
rm test*.csv 2>/dev/null || true
|
rm test*.csv 2>/dev/null || true
|
||||||
@ -29,6 +30,22 @@ EOM
|
|||||||
assertEquals "$expected" "$X"
|
assertEquals "$expected" "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testInputTfstate() {
|
||||||
|
cat >test.tfstate <<EOL
|
||||||
|
{ "mike" : { "things": "cool" } }
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
{"mike": {"things": "cool"}}
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq test.tfstate)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea test.tfstate)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
testInputJsonOutputYaml() {
|
testInputJsonOutputYaml() {
|
||||||
cat >test.json <<EOL
|
cat >test.json <<EOL
|
||||||
{ "mike" : { "things": "cool" } }
|
{ "mike" : { "things": "cool" } }
|
||||||
|
21
cmd/utils.go
21
cmd/utils.go
@ -10,6 +10,10 @@ import (
|
|||||||
"gopkg.in/op/go-logging.v1"
|
"gopkg.in/op/go-logging.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func isAutomaticOutputFormat() bool {
|
||||||
|
return outputFormat == "" || outputFormat == "auto" || outputFormat == "a"
|
||||||
|
}
|
||||||
|
|
||||||
func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
|
|
||||||
@ -60,10 +64,20 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
|||||||
if inputFormat == "" || inputFormat == "auto" || inputFormat == "a" {
|
if inputFormat == "" || inputFormat == "auto" || inputFormat == "a" {
|
||||||
|
|
||||||
inputFormat = yqlib.FormatFromFilename(inputFilename)
|
inputFormat = yqlib.FormatFromFilename(inputFilename)
|
||||||
if outputFormat == "" || outputFormat == "auto" || outputFormat == "a" {
|
|
||||||
outputFormat = yqlib.FormatFromFilename(inputFilename)
|
_, err := yqlib.InputFormatFromString(inputFormat)
|
||||||
|
if err != nil {
|
||||||
|
// unknown file type, default to yaml
|
||||||
|
yqlib.GetLogger().Debug("Unknown file format extension '%v', defaulting to yaml", inputFormat)
|
||||||
|
inputFormat = "yaml"
|
||||||
|
if isAutomaticOutputFormat() {
|
||||||
|
outputFormat = "yaml"
|
||||||
}
|
}
|
||||||
} else if outputFormat == "" || outputFormat == "auto" || outputFormat == "a" {
|
} else if isAutomaticOutputFormat() {
|
||||||
|
// automatic input worked, we can do it for output too unless specified
|
||||||
|
outputFormat = inputFormat
|
||||||
|
}
|
||||||
|
} else if isAutomaticOutputFormat() {
|
||||||
// backwards compatibility -
|
// backwards compatibility -
|
||||||
// before this was introduced, `yq -pcsv things.csv`
|
// before this was introduced, `yq -pcsv things.csv`
|
||||||
// would produce *yaml* output.
|
// would produce *yaml* output.
|
||||||
@ -80,6 +94,7 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil, err
|
return "", nil, err
|
||||||
}
|
}
|
||||||
|
yqlib.GetLogger().Debug("Using input format %v", inputFormat)
|
||||||
yqlib.GetLogger().Debug("Using output format %v", outputFormat)
|
yqlib.GetLogger().Debug("Using output format %v", outputFormat)
|
||||||
|
|
||||||
if outputFormatType == yqlib.YamlOutputFormat ||
|
if outputFormatType == yqlib.YamlOutputFormat ||
|
||||||
|
Loading…
Reference in New Issue
Block a user