use filepath.Ext for more reliable file ext detection

especially for paths like index.js/foo
This commit is contained in:
ryenus 2024-08-05 11:35:34 +08:00
parent 570986485f
commit 0e7f9ad4f6

View File

@ -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 := strings.ToLower(filename[nPos+1:]) format := strings.ToLower(ext[1:])
GetLogger().Debugf("detected format '%s'", format) GetLogger().Debugf("detected format '%s'", format)
return format return format
} }