diff options
Diffstat (limited to 'vendor/github.com/gobuffalo')
17 files changed, 1161 insertions, 0 deletions
diff --git a/vendor/github.com/gobuffalo/packr/LICENSE.txt b/vendor/github.com/gobuffalo/packr/LICENSE.txt new file mode 100644 index 0000000..3ccb336 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/LICENSE.txt @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright (c) 2016 Mark Bates + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/vendor/github.com/gobuffalo/packr/README.md b/vendor/github.com/gobuffalo/packr/README.md new file mode 100644 index 0000000..d1144b1 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/README.md @@ -0,0 +1,145 @@ +# packr + +[![GoDoc](https://godoc.org/github.com/gobuffalo/packr?status.svg)](https://godoc.org/github.com/gobuffalo/packr) + +Packr is a simple solution for bundling static assets inside of Go binaries. Most importantly it does it in a way that is friendly to developers while they are developing. + +## Intro Video + +To get an idea of the what and why of packr, please enjoy this short video: [https://vimeo.com/219863271](https://vimeo.com/219863271). + +## Installation + +```text +$ go get -u github.com/gobuffalo/packr/... +``` + +## Usage + +### In Code + +The first step in using Packr is to create a new box. A box represents a folder on disk. Once you have a box you can get `string` or `[]byte` representations of the file. + +```go +// set up a new box by giving it a (relative) path to a folder on disk: +box := packr.NewBox("./templates") + +// Get the string representation of a file: +html := box.String("index.html") + +// Get the string representation of a file, or an error if it doesn't exist: +html, err := box.MustString("index.html") + +// Get the []byte representation of a file: +html := box.Bytes("index.html") + +// Get the []byte representation of a file, or an error if it doesn't exist: +html, err := box.MustBytes("index.html") +``` + +### What is a Box? + +A box represents a folder, and any sub-folders, on disk that you want to have access to in your binary. When compiling a binary using the `packr` CLI the contents of the folder will be converted into Go files that can be compiled inside of a "standard" go binary. Inside of the compiled binary the files will be read from memory. When working locally the files will be read directly off of disk. This is a seamless switch that doesn't require any special attention on your part. + +#### Example + +Assume the follow directory structure: + +``` +├── main.go +└── templates + ├── admin + │ └── index.html + └── index.html +``` + +The following program will read the `./templates/admin/index.html` file and print it out. + +```go +package main + +import ( + "fmt" + + "github.com/gobuffalo/packr" +) + +func main() { + box := packr.NewBox("./templates") + + s := box.String("admin/index.html") + fmt.Println(s) +} +``` + +### Development Made Easy + +In order to get static files into a Go binary, those files must first be converted to Go code. To do that, Packr, ships with a few tools to help build binaries. See below. + +During development, however, it is painful to have to keep running a tool to compile those files. + +Packr uses the following resolution rules when looking for a file: + +1. Look for the file in-memory (inside a Go binary) +1. Look for the file on disk (during development) + +Because Packr knows how to fall through to the file system, developers don't need to worry about constantly compiling their static files into a binary. They can work unimpeded. + +Packr takes file resolution a step further. When declaring a new box you use a relative path, `./templates`. When Packr recieves this call it calculates out the absolute path to that directory. By doing this it means you can be guaranteed that Packr can find your files correctly, even if you're not running in the directory that the box was created in. This helps with the problem of testing, where Go changes the `pwd` for each package, making relative paths difficult to work with. This is not a problem when using Packr. + +--- + +## Usage with HTTP + +A box implements the [`http.FileSystem`](https://golang.org/pkg/net/http/#FileSystemhttps://golang.org/pkg/net/http/#FileSystem) interface, meaning it can be used to serve static files. + +```go +package main + +import ( + "net/http" + + "github.com/gobuffalo/packr" +) + +func main() { + box := packr.NewBox("./templates") + + http.Handle("/", http.FileServer(box)) + http.ListenAndServe(":3000", nil) +} +``` + +--- + +## Building a Binary (the easy way) + +When it comes time to build, or install, your Go binary, simply use `packr build` or `packr install` just as you would `go build` or `go install`. All flags for the `go` tool are supported and everything works the way you expect, the only difference is your static assets are now bundled in the generated binary. If you want more control over how this happens, looking at the following section on building binaries (the hard way). + +## Building a Binary (the hard way) + +Before you build your Go binary, run the `packr` command first. It will look for all the boxes in your code and then generate `.go` files that pack the static files into bytes that can be bundled into the Go binary. + +``` +$ packr +``` + +Then run your `go build command` like normal. + +*NOTE*: It is not recommended to check-in these generated `-packr.go` files. They can be large, and can easily become out of date if not careful. It is recommended that you always run `packr clean` after running the `packr` tool. + +#### Cleaning Up + +When you're done it is recommended that you run the `packr clean` command. This will remove all of the generated files that Packr created for you. + +``` +$ packr clean +``` + +Why do you want to do this? Packr first looks to the information stored in these generated files, if the information isn't there it looks to disk. This makes it easy to work with in development. + +--- + +## Debugging + +The `packr` command passes all arguments down to the underlying `go` command, this includes the `-v` flag to print out `go build` information. Packr looks for the `-v` flag, and will turn on its own verbose logging. This is very useful for trying to understand what the `packr` command is doing when it is run. diff --git a/vendor/github.com/gobuffalo/packr/box.go b/vendor/github.com/gobuffalo/packr/box.go new file mode 100644 index 0000000..30eb6e2 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/box.go @@ -0,0 +1,241 @@ +package packr + +import ( + "bytes" + "compress/gzip" + "io/ioutil" + "net/http" + "os" + "path" + "path/filepath" + "runtime" + "strings" + + "github.com/pkg/errors" +) + +var ( + ErrResOutsideBox = errors.New("Can't find a resource outside the box") +) + +// NewBox returns a Box that can be used to +// retrieve files from either disk or the embedded +// binary. +func NewBox(path string) Box { + var cd string + if !filepath.IsAbs(path) { + _, filename, _, _ := runtime.Caller(1) + cd = filepath.Dir(filename) + } + + // this little hack courtesy of the `-cover` flag!! + cov := filepath.Join("_test", "_obj_test") + cd = strings.Replace(cd, string(filepath.Separator)+cov, "", 1) + if !filepath.IsAbs(cd) && cd != "" { + cd = filepath.Join(GoPath(), "src", cd) + } + + return Box{ + Path: path, + callingDir: cd, + data: map[string][]byte{}, + } +} + +// Box represent a folder on a disk you want to +// have access to in the built Go binary. +type Box struct { + Path string + callingDir string + data map[string][]byte + directories map[string]bool +} + +func (b Box) AddString(path string, t string) { + b.AddBytes(path, []byte(t)) +} + +func (b Box) AddBytes(path string, t []byte) { + b.data[path] = t +} + +// String of the file asked for or an empty string. +func (b Box) String(name string) string { + return string(b.Bytes(name)) +} + +// MustString returns either the string of the requested +// file or an error if it can not be found. +func (b Box) MustString(name string) (string, error) { + bb, err := b.MustBytes(name) + return string(bb), err +} + +// Bytes of the file asked for or an empty byte slice. +func (b Box) Bytes(name string) []byte { + bb, _ := b.MustBytes(name) + return bb +} + +// MustBytes returns either the byte slice of the requested +// file or an error if it can not be found. +func (b Box) MustBytes(name string) ([]byte, error) { + f, err := b.find(name) + if err == nil { + bb := &bytes.Buffer{} + bb.ReadFrom(f) + return bb.Bytes(), err + } + return nil, err +} + +// Has returns true if the resource exists in the box +func (b Box) Has(name string) bool { + _, err := b.find(name) + if err != nil { + return false + } + return true +} + +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 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 + } + if _, ok := b.directories[cleanName]; ok { + return newVirtualDir(cleanName), nil + } + 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) +} + +type WalkFunc func(string, File) error + +func (b Box) Walk(wf WalkFunc) error { + if data[b.Path] == nil { + base, err := filepath.EvalSymlinks(filepath.Join(b.callingDir, b.Path)) + if err != nil { + return errors.WithStack(err) + } + return filepath.Walk(base, func(path string, info os.FileInfo, err error) error { + cleanName, err := filepath.Rel(base, path) + if err != nil { + cleanName = strings.TrimPrefix(path, base) + } + cleanName = filepath.ToSlash(filepath.Clean(cleanName)) + cleanName = strings.TrimPrefix(cleanName, "/") + cleanName = filepath.FromSlash(cleanName) + if info == nil || info.IsDir() { + return nil + } + + file, err := fileFor(path, cleanName) + if err != nil { + return err + } + return wf(cleanName, file) + }) + } + for n := range data[b.Path] { + f, err := b.find(n) + if err != nil { + return err + } + err = wf(n, f) + if err != nil { + return err + } + } + return nil +} + +// Open returns a File using the http.File interface +func (b Box) Open(name string) (http.File, error) { + return b.find(name) +} + +// List shows "What's in the box?" +func (b Box) List() []string { + 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 +} + +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 +} diff --git a/vendor/github.com/gobuffalo/packr/builder/box.go b/vendor/github.com/gobuffalo/packr/builder/box.go new file mode 100644 index 0000000..8490455 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/box.go @@ -0,0 +1,76 @@ +package builder + +import ( + "bytes" + "compress/gzip" + "encoding/json" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/pkg/errors" +) + +type box struct { + Name string + Files []file + compress bool +} + +func (b *box) Walk(root string) error { + root, err := filepath.EvalSymlinks(root) + if err != nil { + return errors.WithStack(err) + } + if _, err := os.Stat(root); err != nil { + // return nil + return errors.Errorf("could not find folder for box: %s", root) + } + return filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if info == nil || info.IsDir() || strings.HasSuffix(info.Name(), "-packr.go") { + return nil + } + name := strings.Replace(path, root+string(os.PathSeparator), "", 1) + name = strings.Replace(name, "\\", "/", -1) + f := file{ + Name: name, + } + + DebugLog("packing file %s\n", f.Name) + + bb, err := ioutil.ReadFile(path) + if err != nil { + return errors.WithStack(err) + } + if b.compress { + bb, err = compressFile(bb) + if err != nil { + return errors.WithStack(err) + } + } + bb, err = json.Marshal(bb) + if err != nil { + return errors.WithStack(err) + } + f.Contents = strings.Replace(string(bb), "\"", "\\\"", -1) + + DebugLog("packed file %s\n", f.Name) + b.Files = append(b.Files, f) + return nil + }) +} + +func compressFile(bb []byte) ([]byte, error) { + var buf bytes.Buffer + writer := gzip.NewWriter(&buf) + _, err := writer.Write(bb) + if err != nil { + return bb, errors.WithStack(err) + } + err = writer.Close() + if err != nil { + return bb, errors.WithStack(err) + } + return buf.Bytes(), nil +} diff --git a/vendor/github.com/gobuffalo/packr/builder/builder.go b/vendor/github.com/gobuffalo/packr/builder/builder.go new file mode 100644 index 0000000..c689500 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/builder.go @@ -0,0 +1,168 @@ +package builder + +import ( + "context" + "os" + "path/filepath" + "regexp" + "strings" + "sync" + "text/template" + + "github.com/pkg/errors" + "golang.org/x/sync/errgroup" +) + +var DebugLog func(string, ...interface{}) + +func init() { + DebugLog = func(string, ...interface{}) {} +} + +var invalidFilePattern = regexp.MustCompile(`(_test|-packr).go$`) + +// Builder scans folders/files looking for `packr.NewBox` and then compiling +// the required static files into `<package-name>-packr.go` files so they can +// be built into Go binaries. +type Builder struct { + context.Context + RootPath string + IgnoredBoxes []string + IgnoredFolders []string + pkgs map[string]pkg + moot *sync.Mutex + Compress bool +} + +// Run the builder. +func (b *Builder) Run() error { + wg := &errgroup.Group{} + root, err := filepath.EvalSymlinks(b.RootPath) + if err != nil { + return errors.WithStack(err) + } + err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if info == nil { + return filepath.SkipDir + } + + base := strings.ToLower(filepath.Base(path)) + if strings.HasPrefix(base, "_") { + return filepath.SkipDir + } + for _, f := range b.IgnoredFolders { + if strings.ToLower(f) == base { + return filepath.SkipDir + } + } + if !info.IsDir() { + wg.Go(func() error { + return b.process(path) + }) + } + return nil + }) + if err != nil { + return errors.WithStack(err) + } + if err := wg.Wait(); err != nil { + return errors.WithStack(err) + } + return b.dump() +} + +func (b *Builder) dump() error { + for _, p := range b.pkgs { + name := filepath.Join(p.Dir, "a_"+p.Name+"-packr.go") + f, err := os.Create(name) + defer f.Close() + if err != nil { + return errors.WithStack(err) + } + t, err := template.New("").Parse(tmpl) + + if err != nil { + return errors.WithStack(err) + } + err = t.Execute(f, p) + if err != nil { + return errors.WithStack(err) + } + } + return nil +} + +func (b *Builder) process(path string) error { + ext := filepath.Ext(path) + if ext != ".go" || invalidFilePattern.MatchString(path) { + return nil + } + + v := newVisitor(path) + if err := v.Run(); err != nil { + return errors.WithStack(err) + } + + pk := pkg{ + Dir: filepath.Dir(path), + Boxes: []box{}, + Name: v.Package, + } + + for _, n := range v.Boxes { + var ignored bool + for _, i := range b.IgnoredBoxes { + if n == i { + // this is an ignored box + ignored = true + break + } + } + if ignored { + continue + } + bx := &box{ + Name: n, + Files: []file{}, + compress: b.Compress, + } + DebugLog("building box %s\n", bx.Name) + p := filepath.Join(pk.Dir, bx.Name) + if err := bx.Walk(p); err != nil { + return errors.WithStack(err) + } + if len(bx.Files) > 0 { + pk.Boxes = append(pk.Boxes, *bx) + } + DebugLog("built box %s with %q\n", bx.Name, bx.Files) + } + + if len(pk.Boxes) > 0 { + b.addPkg(pk) + } + return nil +} + +func (b *Builder) addPkg(p pkg) { + b.moot.Lock() + defer b.moot.Unlock() + if _, ok := b.pkgs[p.Name]; !ok { + b.pkgs[p.Name] = p + return + } + pp := b.pkgs[p.Name] + pp.Boxes = append(pp.Boxes, p.Boxes...) + b.pkgs[p.Name] = pp +} + +// New Builder with a given context and path +func New(ctx context.Context, path string) *Builder { + return &Builder{ + Context: ctx, + RootPath: path, + IgnoredBoxes: []string{}, + IgnoredFolders: []string{"vendor", ".git", "node_modules", ".idea"}, + pkgs: map[string]pkg{}, + moot: &sync.Mutex{}, + } +} diff --git a/vendor/github.com/gobuffalo/packr/builder/clean.go b/vendor/github.com/gobuffalo/packr/builder/clean.go new file mode 100644 index 0000000..688ebf5 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/clean.go @@ -0,0 +1,32 @@ +package builder + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/pkg/errors" +) + +// Clean up an *-packr.go files +func Clean(root string) { + root, _ = filepath.EvalSymlinks(root) + filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + base := filepath.Base(path) + if base == ".git" || base == "vendor" || base == "node_modules" { + return filepath.SkipDir + } + if info == nil || info.IsDir() { + return nil + } + if strings.Contains(base, "-packr.go") { + err := os.Remove(path) + if err != nil { + fmt.Println(err) + return errors.WithStack(err) + } + } + return nil + }) +} diff --git a/vendor/github.com/gobuffalo/packr/builder/file.go b/vendor/github.com/gobuffalo/packr/builder/file.go new file mode 100644 index 0000000..d439d49 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/file.go @@ -0,0 +1,10 @@ +package builder + +type file struct { + Name string + Contents string +} + +func (f file) String() string { + return f.Name +} diff --git a/vendor/github.com/gobuffalo/packr/builder/pkg.go b/vendor/github.com/gobuffalo/packr/builder/pkg.go new file mode 100644 index 0000000..fbad2ac --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/pkg.go @@ -0,0 +1,7 @@ +package builder + +type pkg struct { + Name string + Dir string + Boxes []box +} diff --git a/vendor/github.com/gobuffalo/packr/builder/tmpl.go b/vendor/github.com/gobuffalo/packr/builder/tmpl.go new file mode 100644 index 0000000..ffe47c1 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/tmpl.go @@ -0,0 +1,18 @@ +package builder + +var tmpl = `// Code generated by github.com/gobuffalo/packr. DO NOT EDIT + +package {{.Name}} + +import "github.com/gobuffalo/packr" + +// You can use the "packr clean" command to clean up this, +// and any other packr generated files. +func init() { + {{- range $box := .Boxes }} + {{- range .Files }} + packr.PackJSONBytes("{{$box.Name}}", "{{.Name}}", "{{.Contents}}") + {{- end }} + {{- end }} +} +` diff --git a/vendor/github.com/gobuffalo/packr/builder/visitor.go b/vendor/github.com/gobuffalo/packr/builder/visitor.go new file mode 100644 index 0000000..ea24bde --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/builder/visitor.go @@ -0,0 +1,248 @@ +package builder + +import ( + "go/ast" + "go/parser" + "go/token" + "io/ioutil" + "sort" + "strings" + + "github.com/pkg/errors" +) + +type visitor struct { + Path string + Package string + Boxes []string + Errors []error +} + +func newVisitor(path string) *visitor { + return &visitor{ + Path: path, + Boxes: []string{}, + Errors: []error{}, + } +} + +func (v *visitor) Run() error { + b, err := ioutil.ReadFile(v.Path) + if err != nil { + return errors.WithStack(err) + } + + fset := token.NewFileSet() + file, err := parser.ParseFile(fset, v.Path, string(b), parser.ParseComments) + if err != nil { + return errors.WithStack(err) + } + + v.Package = file.Name.Name + ast.Walk(v, file) + + m := map[string]string{} + for _, s := range v.Boxes { + m[s] = s + } + v.Boxes = []string{} + for k := range m { + v.Boxes = append(v.Boxes, k) + } + + sort.Strings(v.Boxes) + + if len(v.Errors) > 0 { + s := make([]string, len(v.Errors)) + for i, e := range v.Errors { + s[i] = e.Error() + } + return errors.New(strings.Join(s, "\n")) + } + return nil +} + +func (v *visitor) Visit(node ast.Node) ast.Visitor { + if node == nil { + return v + } + if err := v.eval(node); err != nil { + v.Errors = append(v.Errors, err) + } + return v +} + +func (v *visitor) eval(node ast.Node) error { + switch t := node.(type) { + case *ast.CallExpr: + return v.evalExpr(t) + case *ast.Ident: + return v.evalIdent(t) + case *ast.GenDecl: + for _, n := range t.Specs { + if err := v.eval(n); err != nil { + return errors.WithStack(err) + } + } + case *ast.FuncDecl: + if t.Body == nil { + return nil + } + for _, b := range t.Body.List { + if err := v.evalStmt(b); err != nil { + return errors.WithStack(err) + } + } + return nil + case *ast.ValueSpec: + for _, e := range t.Values { + if err := v.evalExpr(e); err != nil { + return errors.WithStack(err) + } + } + } + return nil +} + +func (v *visitor) evalStmt(stmt ast.Stmt) error { + switch t := stmt.(type) { + case *ast.ExprStmt: + return v.evalExpr(t.X) + case *ast.AssignStmt: + for _, e := range t.Rhs { + if err := v.evalArgs(e); err != nil { + return errors.WithStack(err) + } + } + } + return nil +} + +func (v *visitor) evalExpr(expr ast.Expr) error { + switch t := expr.(type) { + case *ast.CallExpr: + if t.Fun == nil { + return nil + } + for _, a := range t.Args { + switch at := a.(type) { + case *ast.CallExpr: + if sel, ok := t.Fun.(*ast.SelectorExpr); ok { + return v.evalSelector(at, sel) + } + + if err := v.evalArgs(at); err != nil { + return errors.WithStack(err) + } + case *ast.CompositeLit: + for _, e := range at.Elts { + if err := v.evalExpr(e); err != nil { + return errors.WithStack(err) + } + } + } + } + if ft, ok := t.Fun.(*ast.SelectorExpr); ok { + return v.evalSelector(t, ft) + } + case *ast.KeyValueExpr: + return v.evalExpr(t.Value) + } + return nil +} + +func (v *visitor) evalArgs(expr ast.Expr) error { + switch at := expr.(type) { + case *ast.CompositeLit: + for _, e := range at.Elts { + if err := v.evalExpr(e); err != nil { + return errors.WithStack(err) + } + } + // case *ast.BasicLit: + // fmt.Println("evalArgs", at.Value) + // v.addBox(at.Value) + case *ast.CallExpr: + if at.Fun == nil { + return nil + } + switch st := at.Fun.(type) { + case *ast.SelectorExpr: + if err := v.evalSelector(at, st); err != nil { + return errors.WithStack(err) + } + case *ast.Ident: + return v.evalIdent(st) + } + for _, a := range at.Args { + if err := v.evalArgs(a); err != nil { + return errors.WithStack(err) + } + } + } + return nil +} + +func (v *visitor) evalSelector(expr *ast.CallExpr, sel *ast.SelectorExpr) error { + x, ok := sel.X.(*ast.Ident) + if !ok { + return nil + } + if x.Name == "packr" && sel.Sel.Name == "NewBox" { + for _, e := range expr.Args { + switch at := e.(type) { + case *ast.Ident: + switch at.Obj.Kind { + case ast.Var: + if as, ok := at.Obj.Decl.(*ast.AssignStmt); ok { + v.addVariable(as) + } + case ast.Con: + if vs, ok := at.Obj.Decl.(*ast.ValueSpec); ok { + v.addConstant(vs) + } + } + return v.evalIdent(at) + case *ast.BasicLit: + v.addBox(at.Value) + case *ast.CallExpr: + return v.evalExpr(at) + } + } + } + + return nil +} + +func (v *visitor) evalIdent(i *ast.Ident) error { + if i.Obj == nil { + return nil + } + if s, ok := i.Obj.Decl.(*ast.AssignStmt); ok { + return v.evalStmt(s) + } + return nil +} + +func (v *visitor) addBox(b string) { + b = strings.Replace(b, "\"", "", -1) + v.Boxes = append(v.Boxes, b) +} + +func (v *visitor) addVariable(as *ast.AssignStmt) error { + if len(as.Rhs) == 1 { + if bs, ok := as.Rhs[0].(*ast.BasicLit); ok { + v.addBox(bs.Value) + } + } + return nil +} + +func (v *visitor) addConstant(vs *ast.ValueSpec) error { + if len(vs.Values) == 1 { + if bs, ok := vs.Values[0].(*ast.BasicLit); ok { + v.addBox(bs.Value) + } + } + return nil +} diff --git a/vendor/github.com/gobuffalo/packr/env.go b/vendor/github.com/gobuffalo/packr/env.go new file mode 100644 index 0000000..2c744e7 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/env.go @@ -0,0 +1,27 @@ +package packr + +import ( + "go/build" + "os" + "strings" +) + +// GoPath returns the current GOPATH env var +// or if it's missing, the default. +func GoPath() string { + go_path := strings.Split(os.Getenv("GOPATH"), string(os.PathListSeparator)) + if len(go_path) == 0 || go_path[0] == "" { + return build.Default.GOPATH + } + return go_path[0] +} + +// GoBin returns the current GO_BIN env var +// or if it's missing, a default of "go" +func GoBin() string { + go_bin := os.Getenv("GO_BIN") + if go_bin == "" { + return "go" + } + return go_bin +} diff --git a/vendor/github.com/gobuffalo/packr/file.go b/vendor/github.com/gobuffalo/packr/file.go new file mode 100644 index 0000000..8337d62 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/file.go @@ -0,0 +1,15 @@ +package packr + +import ( + "io" + "os" +) + +type File interface { + io.ReadCloser + io.Writer + FileInfo() (os.FileInfo, error) + Readdir(count int) ([]os.FileInfo, error) + Seek(offset int64, whence int) (int64, error) + Stat() (os.FileInfo, error) +} diff --git a/vendor/github.com/gobuffalo/packr/file_info.go b/vendor/github.com/gobuffalo/packr/file_info.go new file mode 100644 index 0000000..e7931d2 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/file_info.go @@ -0,0 +1,38 @@ +package packr + +import ( + "os" + "time" +) + +type fileInfo struct { + Path string + Contents []byte + size int64 + modTime time.Time + isDir bool +} + +func (f fileInfo) Name() string { + return f.Path +} + +func (f fileInfo) Size() int64 { + return f.size +} + +func (f fileInfo) Mode() os.FileMode { + return 0444 +} + +func (f fileInfo) ModTime() time.Time { + return f.modTime +} + +func (f fileInfo) IsDir() bool { + return f.isDir +} + +func (f fileInfo) Sys() interface{} { + return nil +} diff --git a/vendor/github.com/gobuffalo/packr/go.mod b/vendor/github.com/gobuffalo/packr/go.mod new file mode 100644 index 0000000..482c1a8 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/go.mod @@ -0,0 +1,3 @@ +module github.com/gobuffalo/packr + +require github.com/pkg/errors v0.8.0 diff --git a/vendor/github.com/gobuffalo/packr/packr.go b/vendor/github.com/gobuffalo/packr/packr.go new file mode 100644 index 0000000..d282994 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/packr.go @@ -0,0 +1,55 @@ +package packr + +import ( + "bytes" + "compress/gzip" + "encoding/json" + "sync" +) + +var gil = &sync.Mutex{} +var data = map[string]map[string][]byte{} + +// PackBytes packs bytes for a file into a box. +func PackBytes(box string, name string, bb []byte) { + gil.Lock() + defer gil.Unlock() + if _, ok := data[box]; !ok { + data[box] = map[string][]byte{} + } + data[box][name] = bb +} + +// PackBytesGzip packets the gzipped compressed bytes into a box. +func PackBytesGzip(box string, name string, bb []byte) error { + var buf bytes.Buffer + w := gzip.NewWriter(&buf) + _, err := w.Write(bb) + if err != nil { + return err + } + err = w.Close() + if err != nil { + return err + } + PackBytes(box, name, buf.Bytes()) + return nil +} + +// PackJSONBytes packs JSON encoded bytes for a file into a box. +func PackJSONBytes(box string, name string, jbb string) error { + var bb []byte + err := json.Unmarshal([]byte(jbb), &bb) + if err != nil { + return err + } + PackBytes(box, name, bb) + return nil +} + +// UnpackBytes unpacks bytes for specific box. +func UnpackBytes(box string) { + gil.Lock() + defer gil.Unlock() + delete(data, box) +} diff --git a/vendor/github.com/gobuffalo/packr/physical_file.go b/vendor/github.com/gobuffalo/packr/physical_file.go new file mode 100644 index 0000000..bf2d817 --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/physical_file.go @@ -0,0 +1,13 @@ +package packr + +import "os" + +var _ File = physicalFile{} + +type physicalFile struct { + *os.File +} + +func (p physicalFile) FileInfo() (os.FileInfo, error) { + return os.Stat(p.Name()) +} diff --git a/vendor/github.com/gobuffalo/packr/virtual_file.go b/vendor/github.com/gobuffalo/packr/virtual_file.go new file mode 100644 index 0000000..955db8c --- /dev/null +++ b/vendor/github.com/gobuffalo/packr/virtual_file.go @@ -0,0 +1,57 @@ +package packr + +import ( + "bytes" + "fmt" + "os" + "time" +) + +var virtualFileModTime = time.Now() +var _ File = virtualFile{} + +type virtualFile struct { + *bytes.Reader + Name string + info fileInfo +} + +func (f virtualFile) FileInfo() (os.FileInfo, error) { + return f.info, nil +} + +func (f virtualFile) Close() error { + return nil +} + +func (f virtualFile) Write(p []byte) (n int, err error) { + return 0, fmt.Errorf("not implemented") +} + +func (f virtualFile) Readdir(count int) ([]os.FileInfo, error) { + return []os.FileInfo{f.info}, nil +} + +func (f virtualFile) Stat() (os.FileInfo, error) { + return f.info, nil +} + +func newVirtualFile(name string, b []byte) File { + return virtualFile{ + Reader: bytes.NewReader(b), + Name: name, + info: fileInfo{ + Path: name, + Contents: b, + size: int64(len(b)), + modTime: virtualFileModTime, + }, + } +} + +func newVirtualDir(name string) File { + var b []byte + v := newVirtualFile(name, b).(virtualFile) + v.info.isDir = true + return v +} |