Skip to content

Commit 48b481b

Browse files
committed
Unknown file type should default to yaml, Fixes #1609
1 parent 7305b50 commit 48b481b

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

acceptance_tests/inputs-format-auto.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
setUp() {
44
rm test*.yml 2>/dev/null || true
5+
rm test*.tfstate 2>/dev/null || true
56
rm test*.json 2>/dev/null || true
67
rm test*.properties 2>/dev/null || true
78
rm test*.csv 2>/dev/null || true
@@ -29,6 +30,22 @@ EOM
2930
assertEquals "$expected" "$X"
3031
}
3132

33+
testInputTfstate() {
34+
cat >test.tfstate <<EOL
35+
{ "mike" : { "things": "cool" } }
36+
EOL
37+
38+
read -r -d '' expected << EOM
39+
{"mike": {"things": "cool"}}
40+
EOM
41+
42+
X=$(./yq test.tfstate)
43+
assertEquals "$expected" "$X"
44+
45+
X=$(./yq ea test.tfstate)
46+
assertEquals "$expected" "$X"
47+
}
48+
3249
testInputJsonOutputYaml() {
3350
cat >test.json <<EOL
3451
{ "mike" : { "things": "cool" } }

cmd/utils.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"gopkg.in/op/go-logging.v1"
1111
)
1212

13+
func isAutomaticOutputFormat() bool {
14+
return outputFormat == "" || outputFormat == "auto" || outputFormat == "a"
15+
}
16+
1317
func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
1418
cmd.SilenceUsage = true
1519

@@ -60,10 +64,20 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
6064
if inputFormat == "" || inputFormat == "auto" || inputFormat == "a" {
6165

6266
inputFormat = yqlib.FormatFromFilename(inputFilename)
63-
if outputFormat == "" || outputFormat == "auto" || outputFormat == "a" {
64-
outputFormat = yqlib.FormatFromFilename(inputFilename)
67+
68+
_, err := yqlib.InputFormatFromString(inputFormat)
69+
if err != nil {
70+
// unknown file type, default to yaml
71+
yqlib.GetLogger().Debug("Unknown file format extension '%v', defaulting to yaml", inputFormat)
72+
inputFormat = "yaml"
73+
if isAutomaticOutputFormat() {
74+
outputFormat = "yaml"
75+
}
76+
} else if isAutomaticOutputFormat() {
77+
// automatic input worked, we can do it for output too unless specified
78+
outputFormat = inputFormat
6579
}
66-
} else if outputFormat == "" || outputFormat == "auto" || outputFormat == "a" {
80+
} else if isAutomaticOutputFormat() {
6781
// backwards compatibility -
6882
// before this was introduced, `yq -pcsv things.csv`
6983
// would produce *yaml* output.
@@ -80,7 +94,8 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
8094
if err != nil {
8195
return "", nil, err
8296
}
83-
yqlib.GetLogger().Debug("Using outputformat %v", outputFormat)
97+
yqlib.GetLogger().Debug("Using input format %v", inputFormat)
98+
yqlib.GetLogger().Debug("Using output format %v", outputFormat)
8499

85100
if outputFormatType == yqlib.YamlOutputFormat ||
86101
outputFormatType == yqlib.PropsOutputFormat {

0 commit comments

Comments
 (0)