From 57a648139d891c36ea43a113d22545bb3efda0f3 Mon Sep 17 00:00:00 2001 From: Christopher Hunter Date: Fri, 16 Jun 2023 12:16:21 -0700 Subject: [PATCH] feat: parse non-standard metadata filenames tile-generator creates metadata filenames that are not "metadata/metadata.yml" with this change you can parse those tiles with the tile.ReadMetadata* functions fixes: #403 --- pkg/tile/metadata.go | 10 +++++++++- pkg/tile/metadata_test.go | 11 +++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/pkg/tile/metadata.go b/pkg/tile/metadata.go index 2363b1e25..56c65816e 100644 --- a/pkg/tile/metadata.go +++ b/pkg/tile/metadata.go @@ -30,7 +30,15 @@ func ReadMetadataFromZip(ra io.ReaderAt, zipFileSize int64) ([]byte, error) { } func ReadMetadataFromFS(dir fs.FS) ([]byte, error) { - metadataFile, err := dir.Open("metadata/metadata.yml") + const pattern = "metadata/*.yml" + matches, err := fs.Glob(dir, pattern) + if err != nil { + return nil, err + } + if len(matches) == 0 { + return nil, fmt.Errorf("metadata file not found in the tile: expected a file matching glob %q", pattern) + } + metadataFile, err := dir.Open(matches[0]) if err != nil { return nil, fmt.Errorf("failed to do open metadata zip file: %w", err) } diff --git a/pkg/tile/metadata_test.go b/pkg/tile/metadata_test.go index a4cf011bd..b1a2cc332 100644 --- a/pkg/tile/metadata_test.go +++ b/pkg/tile/metadata_test.go @@ -2,6 +2,7 @@ package tile_test import ( "testing" + "testing/fstest" . "github.com/onsi/gomega" "gopkg.in/yaml.v2" @@ -23,3 +24,13 @@ func TestReadMetadataFromFile(t *testing.T) { please.Expect(metadata.Name).To(Equal("hello"), string(metadataBytes)) } + +func TestNonStandardMetadataFilename(t *testing.T) { + fileFS := fstest.MapFS{ + "metadata/banana.yml": &fstest.MapFile{Data: []byte(`{name: "banana"}`)}, + } + buf, err := tile.ReadMetadataFromFS(fileFS) + please := NewWithT(t) + please.Expect(err).NotTo(HaveOccurred()) + please.Expect(string(buf)).To(Equal(`{name: "banana"}`)) +}