mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-05 00:54:51 +08:00
Default to yaml when a file's extension is not a recognised format (#2785)
* Default to yaml for unrecognised file extensions in format auto-detection * Add test for unrecognised extension defaulting to yaml
This commit is contained in:
+5
-2
@@ -128,8 +128,11 @@ func FormatStringFromFilename(filename string) string {
|
||||
ext := filepath.Ext(filename)
|
||||
if len(ext) >= 2 && ext[0] == '.' {
|
||||
format := strings.ToLower(ext[1:])
|
||||
GetLogger().Debugf("detected format '%s'", format)
|
||||
return format
|
||||
if _, err := FormatFromString(format); err == nil {
|
||||
GetLogger().Debugf("detected format '%s'", format)
|
||||
return format
|
||||
}
|
||||
GetLogger().Debugf("extension '%s' is not a recognised format, defaulting to yaml", format)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,4 +59,7 @@ func TestFormatStringFromFilename(t *testing.T) {
|
||||
test.AssertResult(t, "json", FormatStringFromFilename("TEST.JSON"))
|
||||
test.AssertResult(t, "yaml", FormatStringFromFilename("test.json/foo"))
|
||||
test.AssertResult(t, "yaml", FormatStringFromFilename(""))
|
||||
// unrecognised extensions should default to yaml instead of being passed through verbatim
|
||||
// (see https://github.com/mikefarah/yq/issues/1608)
|
||||
test.AssertResult(t, "yaml", FormatStringFromFilename("test.tfstate"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user