From c727a21239bd3d2758723c2a818a43b6125e3e61 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 9 Nov 2015 13:35:24 +0900 Subject: [PATCH] Improved file handling --- goldsmith.go | 44 ++++++++++++++++++++++---------------------- types.go | 8 ++++---- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/goldsmith.go b/goldsmith.go index 14187b2..d1d3473 100644 --- a/goldsmith.go +++ b/goldsmith.go @@ -23,8 +23,6 @@ package goldsmith import ( - "bytes" - "fmt" "os" "path" "path/filepath" @@ -41,7 +39,6 @@ type stage struct { type goldsmith struct { srcDir, dstDir string stages []stage - files chan *File refs map[string]bool err error } @@ -74,7 +71,7 @@ func (gs *goldsmith) scanFs() error { panic(err) } - file, _ := gs.NewFile(relPath) + file := gs.NewFile(relPath) var f *os.File if f, file.Err = os.Open(match); file.Err == nil { @@ -116,7 +113,7 @@ func (gs *goldsmith) cleanupFiles() error { } func (gs *goldsmith) exportFile(file *File) { - defer func() { file.Buff = nil }() + defer file.Buff.Reset() if file.Err != nil { return @@ -163,41 +160,44 @@ func (gs *goldsmith) chain(s stage, c Chainer) { } } -func (gs *goldsmith) NewFile(path string) (*File, error) { +func (gs *goldsmith) NewFile(path string) *File { if filepath.IsAbs(path) { - return nil, fmt.Errorf("absolute paths are not supported: %s", path) + var err error + path, err = filepath.Rel("/", path) + if err != nil { + panic(err) + } } - file := &File{ - Path: path, + return &File{ + Path: filepath.Clean(path), Meta: make(map[string]interface{}), - Buff: new(bytes.Buffer), } - - return file, nil } -func (gs *goldsmith) NewFileStatic(path string) (*File, error) { - file, err := gs.NewFile(path) - if err != nil { - return nil, err - } - +func (gs *goldsmith) NewFileStatic(path string) *File { + file := gs.NewFile(path) file.flags |= FileFlagStatic - return file, nil + return file } -func (gs *goldsmith) RefFile(path string) error { +func (gs *goldsmith) RefFile(path string) { if filepath.IsAbs(path) { - return fmt.Errorf("absolute paths are not supported: %s", path) + var err error + path, err = filepath.Rel("/", path) + if err != nil { + panic(err) + } } path = filepath.Clean(path) + for { gs.refs[path] = true if path == "." { - return nil + break } + path = filepath.Dir(path) } } diff --git a/types.go b/types.go index f7e5e1f..e6b3d64 100644 --- a/types.go +++ b/types.go @@ -40,17 +40,17 @@ type Filterer interface { type File struct { Path string Meta map[string]interface{} - Buff *bytes.Buffer + Buff bytes.Buffer Err error flags uint32 } type Context interface { - NewFileStatic(path string) (*File, error) - NewFile(path string) (*File, error) + NewFileStatic(path string) *File + NewFile(path string) *File - RefFile(path string) error + RefFile(path string) SrcDir() string DstDir() string