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:
ryenus
2024-08-05 15:14:43 +10:00
committed by GitHub
parent b80e1cb35e
commit b9c3ff6f0a
2 changed files with 15 additions and 5 deletions
+5 -5
View File
@@ -2,6 +2,7 @@ package yqlib
import (
"fmt"
"path/filepath"
"strings"
)
@@ -107,12 +108,11 @@ func (f *Format) GetConfiguredEncoder() Encoder {
}
func FormatStringFromFilename(filename string) string {
if filename != "" {
GetLogger().Debugf("checking file extension '%s' for auto format detection", filename)
nPos := strings.LastIndex(filename, ".")
if nPos > -1 {
format := filename[nPos+1:]
GetLogger().Debugf("checking filename '%s' for auto format detection", filename)
ext := filepath.Ext(filename)
if ext != "" && ext[0] == '.' {
format := strings.ToLower(ext[1:])
GetLogger().Debugf("detected format '%s'", format)
return format
}