goldsmith/goldsmith.go

119 lines
1.9 KiB
Go
Raw Normal View History

2015-10-29 09:24:47 +00:00
package goldsmith
2015-10-29 14:26:43 +00:00
import (
2016-01-13 03:21:30 +00:00
"bytes"
2016-06-12 02:38:17 +00:00
"errors"
2016-08-21 19:54:44 +00:00
"fmt"
2016-01-13 03:21:30 +00:00
"io"
2016-06-11 23:24:06 +00:00
"os"
"time"
2015-10-29 14:26:43 +00:00
)
2016-01-13 03:21:30 +00:00
type Goldsmith interface {
2018-01-07 23:42:32 +00:00
Chain(p Plugin) Goldsmith
FilterPush(f Filter) Goldsmith
FilterPop() Goldsmith
2016-01-13 03:21:30 +00:00
End(dstDir string) []error
}
2015-12-18 11:30:14 +00:00
2018-01-07 23:42:32 +00:00
func Begin(srcDir string) Goldsmith {
2016-01-13 03:21:30 +00:00
gs := &goldsmith{srcDir: srcDir, refs: make(map[string]bool)}
2018-01-07 23:42:32 +00:00
gs.Chain(new(loader))
2016-01-13 03:21:30 +00:00
return gs
2015-12-18 04:14:39 +00:00
}
2016-01-13 03:21:30 +00:00
type File interface {
Path() string
2016-06-11 23:24:06 +00:00
Name() string
2016-06-06 05:02:09 +00:00
Dir() string
2016-06-11 23:24:06 +00:00
Ext() string
Size() int64
ModTime() time.Time
2016-01-13 03:21:30 +00:00
Value(key string) (interface{}, bool)
2017-12-28 21:10:50 +00:00
SetValue(key string, value interface{}) bool
2018-01-01 20:32:26 +00:00
InheritValues(src File)
2015-11-01 05:09:46 +00:00
2016-01-13 03:21:30 +00:00
Read(p []byte) (int, error)
WriteTo(w io.Writer) (int64, error)
Seek(offset int64, whence int) (int64, error)
2015-10-29 14:26:43 +00:00
}
2016-01-13 03:21:30 +00:00
func NewFileFromData(path string, data []byte) File {
return &file{
2016-06-11 23:24:06 +00:00
path: path,
Meta: make(map[string]interface{}),
reader: bytes.NewReader(data),
size: int64(len(data)),
modTime: time.Now(),
2015-11-02 09:07:34 +00:00
}
}
2016-06-11 23:24:06 +00:00
func NewFileFromAsset(path, asset string) (File, error) {
info, err := os.Stat(asset)
if err != nil {
return nil, err
}
2016-06-12 02:38:17 +00:00
if info.IsDir() {
return nil, errors.New("assets must be files")
}
2016-06-11 23:24:06 +00:00
f := &file{
path: path,
Meta: make(map[string]interface{}),
size: info.Size(),
modTime: info.ModTime(),
asset: asset,
2015-11-02 09:23:13 +00:00
}
2016-06-11 23:24:06 +00:00
return f, nil
2015-11-02 09:23:13 +00:00
}
2016-01-13 03:21:30 +00:00
type Context interface {
DispatchFile(f File)
2015-12-17 03:37:11 +00:00
2016-01-13 03:21:30 +00:00
SrcDir() string
DstDir() string
2015-10-31 05:12:03 +00:00
}
2016-01-13 03:21:30 +00:00
type Error struct {
2016-08-21 19:54:44 +00:00
Name string
2016-01-13 03:21:30 +00:00
Path string
2016-08-21 19:54:44 +00:00
Err error
2015-12-18 04:37:32 +00:00
}
2016-01-13 03:21:30 +00:00
func (e Error) Error() string {
2016-08-21 19:54:44 +00:00
var path string
if len(e.Path) > 0 {
path = "@" + e.Path
}
return fmt.Sprintf("[%s%s]: %s", e.Name, path, e.Err.Error())
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
type Initializer interface {
2018-01-07 23:42:32 +00:00
Initialize(ctx Context) ([]Filter, error)
2016-01-13 03:21:30 +00:00
}
2016-01-10 11:17:35 +00:00
2016-01-13 03:21:30 +00:00
type Processor interface {
Process(ctx Context, f File) error
}
2015-10-31 03:30:41 +00:00
2016-01-13 03:21:30 +00:00
type Finalizer interface {
Finalize(ctx Context) error
2015-10-29 09:24:47 +00:00
}
2016-01-13 03:21:30 +00:00
2018-01-07 23:42:32 +00:00
type Component interface {
2016-08-21 19:54:44 +00:00
Name() string
}
2018-01-07 23:42:32 +00:00
type Filter interface {
Component
Accept(ctx Context, f File) (bool, error)
}
type Plugin interface {
Component
}