mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
convert file ext to lowercase for format detection (#2121)
* convert file ext to lowercase for format detection To ensure proper file format detection with case-insensitive file systems. * use filepath.Ext for more reliable file ext detection especially for paths like index.js/foo * add a test for file ext based format detection
This commit is contained in:
parent
b80e1cb35e
commit
b9c3ff6f0a
@ -2,6 +2,7 @@ package yqlib
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -107,12 +108,11 @@ func (f *Format) GetConfiguredEncoder() Encoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FormatStringFromFilename(filename string) string {
|
func FormatStringFromFilename(filename string) string {
|
||||||
|
|
||||||
if filename != "" {
|
if filename != "" {
|
||||||
GetLogger().Debugf("checking file extension '%s' for auto format detection", filename)
|
GetLogger().Debugf("checking filename '%s' for auto format detection", filename)
|
||||||
nPos := strings.LastIndex(filename, ".")
|
ext := filepath.Ext(filename)
|
||||||
if nPos > -1 {
|
if ext != "" && ext[0] == '.' {
|
||||||
format := filename[nPos+1:]
|
format := strings.ToLower(ext[1:])
|
||||||
GetLogger().Debugf("detected format '%s'", format)
|
GetLogger().Debugf("detected format '%s'", format)
|
||||||
return format
|
return format
|
||||||
}
|
}
|
||||||
|
@ -50,3 +50,13 @@ func TestFormatFromString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFormatStringFromFilename(t *testing.T) {
|
||||||
|
test.AssertResult(t, "yaml", FormatStringFromFilename("test.Yaml"))
|
||||||
|
test.AssertResult(t, "yaml", FormatStringFromFilename("test.index.Yaml"))
|
||||||
|
test.AssertResult(t, "yaml", FormatStringFromFilename("test"))
|
||||||
|
test.AssertResult(t, "json", FormatStringFromFilename("test.json"))
|
||||||
|
test.AssertResult(t, "json", FormatStringFromFilename("TEST.JSON"))
|
||||||
|
test.AssertResult(t, "yaml", FormatStringFromFilename("test.json/foo"))
|
||||||
|
test.AssertResult(t, "yaml", FormatStringFromFilename(""))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user