From 570986485f50359424975deff8f4a554fa4bcc02 Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 5 Aug 2024 09:52:50 +0800 Subject: [PATCH 1/3] convert file ext to lowercase for format detection To ensure proper file format detection with case-insensitive file systems. --- pkg/yqlib/format.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/yqlib/format.go b/pkg/yqlib/format.go index 49c47dfd299..a0e0d02b054 100644 --- a/pkg/yqlib/format.go +++ b/pkg/yqlib/format.go @@ -112,7 +112,7 @@ func FormatStringFromFilename(filename string) string { GetLogger().Debugf("checking file extension '%s' for auto format detection", filename) nPos := strings.LastIndex(filename, ".") if nPos > -1 { - format := filename[nPos+1:] + format := strings.ToLower(filename[nPos+1:]) GetLogger().Debugf("detected format '%s'", format) return format } From 0e7f9ad4f6af138acd4c4ccca743f7b831e26f3a Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 5 Aug 2024 11:35:34 +0800 Subject: [PATCH 2/3] 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 a0e0d02b054..144aac08d0e 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 } From 6298fe0f1ab12359bf1cbb87f9c31d3553c071f5 Mon Sep 17 00:00:00 2001 From: ryenus Date: Mon, 5 Aug 2024 11:45:34 +0800 Subject: [PATCH 3/3] add a test for file ext based format detection --- pkg/yqlib/format_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/yqlib/format_test.go b/pkg/yqlib/format_test.go index e6db16441db..6060ccdf617 100644 --- a/pkg/yqlib/format_test.go +++ b/pkg/yqlib/format_test.go @@ -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("")) +}