diff --git a/file.go b/file.go index a69ce94..3432ddf 100644 --- a/file.go +++ b/file.go @@ -29,14 +29,18 @@ import ( "os" "path" "path/filepath" + "time" ) type file struct { path string Meta map[string]interface{} - reader *bytes.Reader - asset string + reader *bytes.Reader + size int64 + modTime time.Time + + asset string } func (f *file) export(dstDir string) error { @@ -99,10 +103,26 @@ func (f *file) Path() string { return f.path } +func (f *file) Name() string { + return path.Base(f.path) +} + func (f *file) Dir() string { return path.Dir(f.path) } +func (f *file) Ext() string { + return path.Ext(f.path) +} + +func (f *file) Size() int64 { + return f.size +} + +func (f *file) ModTime() time.Time { + return f.modTime +} + func (f *file) Value(key string) (interface{}, bool) { value, ok := f.Meta[key] return value, ok diff --git a/goldsmith.go b/goldsmith.go index c4186f2..cce2b5e 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -25,6 +25,8 @@ package goldsmith import ( "bytes" "io" + "os" + "time" ) type Goldsmith interface { @@ -40,7 +42,11 @@ func Begin(srcDir string) Goldsmith { type File interface { Path() string + Name() string Dir() string + Ext() string + Size() int64 + ModTime() time.Time Value(key string) (interface{}, bool) SetValue(key string, value interface{}) @@ -53,18 +59,29 @@ type File interface { func NewFileFromData(path string, data []byte) File { return &file{ - path: path, - Meta: make(map[string]interface{}), - reader: bytes.NewReader(data), + path: path, + Meta: make(map[string]interface{}), + reader: bytes.NewReader(data), + size: int64(len(data)), + modTime: time.Now(), } } -func NewFileFromAsset(path, asset string) File { - return &file{ - path: path, - Meta: make(map[string]interface{}), - asset: asset, +func NewFileFromAsset(path, asset string) (File, error) { + info, err := os.Stat(asset) + if err != nil { + return nil, err } + + f := &file{ + path: path, + Meta: make(map[string]interface{}), + size: info.Size(), + modTime: info.ModTime(), + asset: asset, + } + + return f, nil } type Context interface { diff --git a/loader.go b/loader.go index 0187d08..e38dc84 100644 --- a/loader.go +++ b/loader.go @@ -36,7 +36,15 @@ func (*loader) Initialize(ctx Context) error { } relPath, _ := filepath.Rel(ctx.SrcDir(), info.path) - f := NewFileFromAsset(relPath, info.path) + + f := &file{ + path: relPath, + Meta: make(map[string]interface{}), + modTime: info.ModTime(), + size: info.Size(), + asset: info.path, + } + ctx.DispatchFile(f) }