diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-08-12 16:43:37 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-08-13 11:44:20 +0200 |
commit | b64617fe4f90da030bcf4a9c5a4913393ce96b14 (patch) | |
tree | 07240dbf51bb4afef9ea063f2310c1617be6bb0a /resources/resource_factories | |
parent | 17ca8f0c4c636752fb9da2ad551679275dc03dd3 (diff) |
Add resources.Match and resources.GetMatch
Fix #6190
Diffstat (limited to 'resources/resource_factories')
-rw-r--r-- | resources/resource_factories/create/create.go | 68 |
1 files changed, 64 insertions, 4 deletions
diff --git a/resources/resource_factories/create/create.go b/resources/resource_factories/create/create.go index 36a29e733..e42843c75 100644 --- a/resources/resource_factories/create/create.go +++ b/resources/resource_factories/create/create.go @@ -16,9 +16,12 @@ package create import ( + "path" "path/filepath" - "github.com/spf13/afero" + "github.com/gohugoio/hugo/hugofs/glob" + + "github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/resources" @@ -36,18 +39,75 @@ func New(rs *resources.Spec) *Client { return &Client{rs: rs} } -// Get creates a new Resource by opening the given filename in the given filesystem. -func (c *Client) Get(fs afero.Fs, filename string) (resource.Resource, error) { +// Get creates a new Resource by opening the given filename in the assets filesystem. +func (c *Client) Get(filename string) (resource.Resource, error) { filename = filepath.Clean(filename) return c.rs.ResourceCache.GetOrCreate(resources.ResourceKeyPartition(filename), filename, func() (resource.Resource, error) { return c.rs.New(resources.ResourceSourceDescriptor{ - Fs: fs, + Fs: c.rs.BaseFs.Assets.Fs, LazyPublish: true, SourceFilename: filename}) }) } +// Match gets the resources matching the given pattern from the assets filesystem. +func (c *Client) Match(pattern string) (resource.Resources, error) { + return c.match(pattern, false) +} + +// GetMatch gets first resource matching the given pattern from the assets filesystem. +func (c *Client) GetMatch(pattern string) (resource.Resource, error) { + res, err := c.match(pattern, true) + if err != nil || len(res) == 0 { + return nil, err + } + return res[0], err +} + +func (c *Client) match(pattern string, firstOnly bool) (resource.Resources, error) { + var partition string + if firstOnly { + partition = "__get-match" + } else { + partition = "__match" + } + + // TODO(bep) match will be improved as part of https://github.com/gohugoio/hugo/issues/6199 + partition = path.Join(resources.CACHE_OTHER, partition) + key := glob.NormalizePath(pattern) + + return c.rs.ResourceCache.GetOrCreateResources(partition, key, func() (resource.Resources, error) { + var res resource.Resources + + handle := func(info hugofs.FileMetaInfo) (bool, error) { + meta := info.Meta() + r, err := c.rs.New(resources.ResourceSourceDescriptor{ + LazyPublish: true, + OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) { + return meta.Open() + }, + RelTargetFilename: meta.Path()}) + + if err != nil { + return true, err + } + + res = append(res, r) + + return firstOnly, nil + + } + + if err := hugofs.Glob(c.rs.BaseFs.Assets.Fs, pattern, handle); err != nil { + return nil, err + } + + return res, nil + + }) +} + // FromString creates a new Resource from a string with the given relative target path. func (c *Client) FromString(targetPath, content string) (resource.Resource, error) { return c.rs.ResourceCache.GetOrCreate(resources.CACHE_OTHER, targetPath, func() (resource.Resource, error) { |