From 0e7f9ad4f6af138acd4c4ccca743f7b831e26f3a Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 5 Aug 2024 11:35:34 +0800 Subject: [PATCH] use filepath.Ext for more reliable file ext detection especially for paths like index.js/foo --- pkg/yqlib/format.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/yqlib/format.go b/pkg/yqlib/format.go index a0e0d02b..144aac08 100644 --- a/pkg/yqlib/format.go +++ b/pkg/yqlib/format.go @@ -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 := strings.ToLower(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 }