diff options
Diffstat (limited to 'resource')
-rw-r--r-- | resource/create/create.go | 16 | ||||
-rw-r--r-- | resource/readers.go | 53 | ||||
-rw-r--r-- | resource/resource.go | 9 | ||||
-rw-r--r-- | resource/transform.go | 16 |
4 files changed, 55 insertions, 39 deletions
diff --git a/resource/create/create.go b/resource/create/create.go index 1c7894232..e9d7b6de4 100644 --- a/resource/create/create.go +++ b/resource/create/create.go @@ -16,12 +16,10 @@ package create import ( - "io" "path/filepath" "github.com/spf13/afero" - "github.com/dsnet/golib/memfile" "github.com/gohugoio/hugo/resource" ) @@ -36,15 +34,6 @@ func New(rs *resource.Spec) *Client { return &Client{rs: rs} } -type memFileCloser struct { - *memfile.File - io.Closer -} - -func (m *memFileCloser) Close() error { - return nil -} - // 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) { filename = filepath.Clean(filename) @@ -53,7 +42,6 @@ func (c *Client) Get(fs afero.Fs, filename string) (resource.Resource, error) { resource.ResourceSourceDescriptor{ LazyPublish: true, SourceFilename: filename}) - }) } @@ -66,9 +54,7 @@ func (c *Client) FromString(targetPath, content string) (resource.Resource, erro resource.ResourceSourceDescriptor{ LazyPublish: true, OpenReadSeekCloser: func() (resource.ReadSeekCloser, error) { - return &memFileCloser{ - File: memfile.New([]byte(content)), - }, nil + return resource.NewReadSeekerNoOpCloserFromString(content), nil }, RelTargetFilename: filepath.Clean(targetPath)}) diff --git a/resource/readers.go b/resource/readers.go new file mode 100644 index 000000000..56142c141 --- /dev/null +++ b/resource/readers.go @@ -0,0 +1,53 @@ +// Copyright 2018 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package resource + +import ( + "io" + "strings" +) + +// ReadSeeker wraps io.Reader and io.Seeker. +type ReadSeeker interface { + io.Reader + io.Seeker +} + +// ReadSeekCloser is implemented by afero.File. We use this as the common type for +// content in Resource objects, even for strings. +type ReadSeekCloser interface { + ReadSeeker + io.Closer +} + +// ReadSeekerNoOpCloser implements ReadSeekCloser by doing nothing in Close. +type ReadSeekerNoOpCloser struct { + ReadSeeker +} + +// Close does nothing. +func (r ReadSeekerNoOpCloser) Close() error { + return nil +} + +// NewReadSeekerNoOpCloser creates a new ReadSeekerNoOpCloser with the given ReadSeeker. +func NewReadSeekerNoOpCloser(r ReadSeeker) ReadSeekerNoOpCloser { + return ReadSeekerNoOpCloser{r} +} + +// NewReadSeekerNoOpCloserFromString uses strings.NewReader to create a new ReadSeekerNoOpCloser +// from the given string. +func NewReadSeekerNoOpCloserFromString(content string) ReadSeekerNoOpCloser { + return ReadSeekerNoOpCloser{strings.NewReader(content)} +} diff --git a/resource/resource.go b/resource/resource.go index a7a9cb878..718d4d303 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -16,7 +16,6 @@ package resource import ( "errors" "fmt" - "io" "io/ioutil" "mime" "os" @@ -134,14 +133,6 @@ type ContentResource interface { Content() (interface{}, error) } -// ReadSeekCloser is implemented by afero.File. We use this as the common type for -// content in Resource objects, even for strings. -type ReadSeekCloser interface { - io.Reader - io.Seeker - io.Closer -} - // OpenReadSeekeCloser allows setting some other way (than reading from a filesystem) // to open or create a ReadSeekCloser. type OpenReadSeekCloser func() (ReadSeekCloser, error) diff --git a/resource/transform.go b/resource/transform.go index 93ace2ba0..5ba5ec821 100644 --- a/resource/transform.go +++ b/resource/transform.go @@ -188,25 +188,11 @@ type transformedResource struct { Resource } -type readSeeker struct { - io.Reader - io.Seeker - io.Closer -} - -func (r *readSeeker) Close() error { - return nil -} - -func (r *readSeeker) Seek(offset int64, whence int) (int64, error) { - panic("Seek is not supported by this io.Reader") -} - func (r *transformedResource) ReadSeekCloser() (ReadSeekCloser, error) { if err := r.initContent(); err != nil { return nil, err } - return &readSeeker{Reader: strings.NewReader(r.content)}, nil + return NewReadSeekerNoOpCloserFromString(r.content), nil } func (r *transformedResource) transferTransformedValues(another *transformedResource) { |