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" "os"
"path" "path"
"path/filepath" "path/filepath"
"time"
) )
type file struct { type file struct {
@ -36,6 +37,9 @@ type file struct {
Meta map[string]interface{} Meta map[string]interface{}
reader *bytes.Reader reader *bytes.Reader
size int64
modTime time.Time
asset string asset string
} }
@ -99,10 +103,26 @@ func (f *file) Path() string {
return f.path return f.path
} }
func (f *file) Name() string {
return path.Base(f.path)
}
func (f *file) Dir() string { func (f *file) Dir() string {
return path.Dir(f.path) 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) { func (f *file) Value(key string) (interface{}, bool) {
value, ok := f.Meta[key] value, ok := f.Meta[key]
return value, ok return value, ok

View File

@ -25,6 +25,8 @@ package goldsmith
import ( import (
"bytes" "bytes"
"io" "io"
"os"
"time"
) )
type Goldsmith interface { type Goldsmith interface {
@ -40,7 +42,11 @@ func Begin(srcDir string) Goldsmith {
type File interface { type File interface {
Path() string Path() string
Name() string
Dir() string Dir() string
Ext() string
Size() int64
ModTime() time.Time
Value(key string) (interface{}, bool) Value(key string) (interface{}, bool)
SetValue(key string, value interface{}) SetValue(key string, value interface{})
@ -56,15 +62,26 @@ func NewFileFromData(path string, data []byte) File {
path: path, path: path,
Meta: make(map[string]interface{}), Meta: make(map[string]interface{}),
reader: bytes.NewReader(data), reader: bytes.NewReader(data),
size: int64(len(data)),
modTime: time.Now(),
} }
} }
func NewFileFromAsset(path, asset string) File { func NewFileFromAsset(path, asset string) (File, error) {
return &file{ info, err := os.Stat(asset)
if err != nil {
return nil, err
}
f := &file{
path: path, path: path,
Meta: make(map[string]interface{}), Meta: make(map[string]interface{}),
size: info.Size(),
modTime: info.ModTime(),
asset: asset, asset: asset,
} }
return f, nil
} }
type Context interface { type Context interface {

View File

@ -36,7 +36,15 @@ func (*loader) Initialize(ctx Context) error {
} }
relPath, _ := filepath.Rel(ctx.SrcDir(), info.path) 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) ctx.DispatchFile(f)
} }