Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
got walk and list working
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 7, 2018
1 parent 9c2c31f commit efe7803
Show file tree
Hide file tree
Showing 17 changed files with 417 additions and 142 deletions.
9 changes: 6 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ builds:
- windows
env:
- CGO_ENABLED=0
main: ./packr/main.go
binary: packr
# main: ./packr/main.go
# binary: {{.Binary}}
# hooks:
# pre: packr
# post: packr clean

checksum:
name_template: 'checksums.txt'
Expand All @@ -21,5 +24,5 @@ changelog:
- '^test:'
brew:
github:
owner: gobuffalo
owner: markbates
name: homebrew-tap
38 changes: 38 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TAGS ?= "sqlite"
GO_BIN ?= go

install: deps
packr
$(GO_BIN) install -v .

deps:
# $(GO_BIN) get github.com/gobuffalo/packr/packr
$(GO_BIN) get -tags ${TAGS} -t ./...

build: deps
packr
$(GO_BIN) build -v .

test:
packr
$(GO_BIN) test -tags ${TAGS} ./...

ci-test: deps
$(GO_BIN) test -tags ${TAGS} -race ./...

lint:
gometalinter --vendor ./... --deadline=1m --skip=internal

update:
$(GO_BIN) get -u
$(GO_BIN) mod tidy
packr
make test
make install

release-test:
$(GO_BIN) test -tags ${TAGS} -race ./...

release:
$(GO_BIN) get github.com/gobuffalo/release
release -y -f version.go
Empty file added _fixtures/list_test/a.txt
Empty file.
Empty file added _fixtures/list_test/b/b.txt
Empty file.
Empty file added _fixtures/list_test/b/b2.txt
Empty file.
Empty file added _fixtures/list_test/c/c.txt
Empty file.
133 changes: 24 additions & 109 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"

"github.com/gobuffalo/packr/file"
"github.com/gobuffalo/packr/file/resolver"
"github.com/pkg/errors"
)

func New(path string) Box {
Expand All @@ -36,7 +36,7 @@ func New(path string) Box {
// Box represent a folder on a disk you want to
// have access to in the built Go binary.
type Box struct {
Path string
Path string // Path is deprecated and should no longer be used
Name resolver.Ident
callingDir resolver.Ident
// data map[string][]byte
Expand All @@ -50,11 +50,11 @@ func (b Box) AddString(path string, t string) error {

// AddBytes sets t in b.data by the given path
func (b Box) AddBytes(path string, t []byte) error {
p, ok := resolver.DefaultResolver.(resolver.Packable)
if !ok {
return errors.Errorf("unable to pack %s - resolver.DefaultResolver is not resolver.Packable", path)
}
return p.Pack(resolver.Key(b.Name, resolver.Ident(path)), file.NewFile(path, t))
ipath := resolver.Ident(path)
m := map[resolver.Ident]file.File{}
m[ipath] = file.NewFile(path, t)
res := resolver.NewInMemory(m)
return resolver.Register(b.Name, ipath, res)
}

// String of the file asked for or an empty string.
Expand Down Expand Up @@ -87,114 +87,29 @@ func (b Box) MustBytes(name string) ([]byte, error) {

// Has returns true if the resource exists in the box
func (b Box) Has(name string) bool {
panic("not implemented")
// _, err := b.find(name)
// if err != nil {
// return false
// }
// return true
_, err := resolver.Resolve(b.Name, resolver.Ident(name))
if err != nil {
return false
}
return true
}

// Open returns a File using the http.File interface
func (b Box) Open(name string) (http.File, error) {
panic("not implemented")
// return b.find(name)
return resolver.Resolve(b.Name, resolver.Ident(name))
}

// List shows "What's in the box?"
func (b Box) List() []string {
panic("not implemented")
// var keys []string
//
// if b.data == nil || len(b.data) == 0 {
// b.Walk(func(path string, info File) error {
// finfo, _ := info.FileInfo()
// if !finfo.IsDir() {
// keys = append(keys, finfo.Name())
// }
// return nil
// })
// } else {
// for k := range b.data {
// keys = append(keys, k)
// }
// }
// return keys
var keys []string

b.Walk(func(path string, info File) error {
finfo, _ := info.FileInfo()
if !finfo.IsDir() {
keys = append(keys, finfo.Name())
}
return nil
})
sort.Strings(keys)
return keys
}

// func (b Box) decompress(bb []byte) []byte {
// reader, err := gzip.NewReader(bytes.NewReader(bb))
// if err != nil {
// return bb
// }
// data, err := ioutil.ReadAll(reader)
// if err != nil {
// return bb
// }
// return data
// }

// func (b Box) find(name string) (File, error) {
// if bb, ok := b.data[name]; ok {
// return newVirtualFile(name, bb), nil
// }
// if b.directories == nil {
// b.indexDirectories()
// }
//
// cleanName := filepath.ToSlash(filepath.Clean(name))
// // Ensure name is not outside the box
// if strings.HasPrefix(cleanName, "../") {
// return nil, ErrResOutsideBox
// }
// // Absolute name is considered as relative to the box root
// cleanName = strings.TrimPrefix(cleanName, "/")
//
// // Try to get the resource from the box
// if _, ok := data[b.Path]; ok {
// if bb, ok := data[b.Path][cleanName]; ok {
// bb = b.decompress(bb)
// return newVirtualFile(cleanName, bb), nil
// }
// if _, ok := b.directories[cleanName]; ok {
// return newVirtualDir(cleanName), nil
// }
// if filepath.Ext(cleanName) != "" {
// // The Handler created by http.FileSystem checks for those errors and
// // returns http.StatusNotFound instead of http.StatusInternalServerError.
// return nil, os.ErrNotExist
// }
// return nil, os.ErrNotExist
// }
//
// // Not found in the box virtual fs, try to get it from the file system
// cleanName = filepath.FromSlash(cleanName)
// p := filepath.Join(b.callingDir, b.Path, cleanName)
// return fileFor(p, cleanName)
// }

// func (b *Box) indexDirectories() {
// b.directories = map[string]bool{}
// if _, ok := data[b.Path]; ok {
// for name := range data[b.Path] {
// prefix, _ := path.Split(name)
// // Even on Windows the suffix appears to be a /
// prefix = strings.TrimSuffix(prefix, "/")
// b.directories[prefix] = true
// }
// }
// }

// func fileFor(p string, name string) (File, error) {
// fi, err := os.Stat(p)
// if err != nil {
// return nil, err
// }
// if fi.IsDir() {
// return newVirtualDir(p), nil
// }
// if bb, err := ioutil.ReadFile(p); err == nil {
// return newVirtualFile(name, bb), nil
// }
// return nil, os.ErrNotExist
// }
Loading

0 comments on commit efe7803

Please sign in to comment.