@@ -10,6 +10,10 @@ import (
10
10
"gopkg.in/op/go-logging.v1"
11
11
)
12
12
13
+ func isAutomaticOutputFormat () bool {
14
+ return outputFormat == "" || outputFormat == "auto" || outputFormat == "a"
15
+ }
16
+
13
17
func initCommand (cmd * cobra.Command , args []string ) (string , []string , error ) {
14
18
cmd .SilenceUsage = true
15
19
@@ -60,10 +64,20 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
60
64
if inputFormat == "" || inputFormat == "auto" || inputFormat == "a" {
61
65
62
66
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
65
79
}
66
- } else if outputFormat == "" || outputFormat == "auto" || outputFormat == "a" {
80
+ } else if isAutomaticOutputFormat () {
67
81
// backwards compatibility -
68
82
// before this was introduced, `yq -pcsv things.csv`
69
83
// would produce *yaml* output.
@@ -80,7 +94,8 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
80
94
if err != nil {
81
95
return "" , nil , err
82
96
}
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 )
84
99
85
100
if outputFormatType == yqlib .YamlOutputFormat ||
86
101
outputFormatType == yqlib .PropsOutputFormat {
0 commit comments