Improved file handling

This commit is contained in:
Alex Yatskov 2015-11-09 13:35:24 +09:00
parent 449384a02d
commit c727a21239
2 changed files with 26 additions and 26 deletions

View File

@ -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)
}
file := &File{
Path: 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)
var err error
path, err = filepath.Rel("/", path)
if err != nil {
return nil, err
panic(err)
}
}
file.flags |= FileFlagStatic
return file, nil
return &File{
Path: filepath.Clean(path),
Meta: make(map[string]interface{}),
}
}
func (gs *goldsmith) RefFile(path string) error {
func (gs *goldsmith) NewFileStatic(path string) *File {
file := gs.NewFile(path)
file.flags |= FileFlagStatic
return file
}
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)
}
}

View File

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