Adding more info fields to file object

This commit is contained in:
Alex Yatskov 2016-06-11 16:24:06 -07:00
parent 53ecefabcd
commit a7b084177e
3 changed files with 56 additions and 11 deletions

20
file.go
View File

@ -29,6 +29,7 @@ import (
"os"
"path"
"path/filepath"
"time"
)
type file struct {
@ -36,6 +37,9 @@ type file struct {
Meta map[string]interface{}
reader *bytes.Reader
size int64
modTime time.Time
asset string
}
@ -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

View File

@ -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{})
@ -56,15 +62,26 @@ func NewFileFromData(path string, data []byte) File {
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{
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 {

View File

@ -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)
}