packr
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
packr (v1)
Packr has been updated tov2! Please read the./v2/README.md file for more details.
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.
Installation
To install Packr utility
$ go get -u github.com/gobuffalo/packr/packrTo get the dependency
$ go get -u github.com/gobuffalo/packrUsage
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 getstring or[]byte representations of the file.
// 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, or an error if it doesn't exist:html, err := box.FindString("index.html")// Get the []byte representation of a file, or an error if it doesn't exist:html, err := box.FindBytes("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 thepackr 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.htmlThe following program will read the./templates/admin/index.html file and print it out.
package mainimport ( "fmt" "github.com/gobuffalo/packr")func main() { box := packr.NewBox("./templates") s, err := box.FindString("admin/index.html") if err != nil { log.Fatal(err) } 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:
- Look for the file in-memory (inside a Go binary)
- 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 receives 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 thepwd for each package, making relative paths difficult to work with. This is not a problem when using Packr.
Usage with HTTP
A box implements thehttp.FileSystem interface, meaning it can be used to serve static files.
package mainimport ( "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 usepackr build orpackr install just as you wouldgo build orgo install. All flags for thego 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 thepackr 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.
$ packrThen run yourgo 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 runpackr clean after running thepackr tool.
Cleaning Up
When you're done it is recommended that you run thepackr clean command. This will remove all of the generated files that Packr created for you.
$ packr cleanWhy 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.
Building/Moving a portable release
When it comes to building multiple releases you typically want that release to be built in a specific directory.
For example:./releases
However, because passing a.go file requires absolute paths, we must compile the release in the appropriate absolute path.
GOOS=linux GOARCH=amd64 packr buildNow yourproject_name binary will be built at the root of your project dir. Great!
All that is left to do is to move that binary to your release dir:
Linux/macOS/Windows (bash)
mv ./project_name ./releasesWindows (cmd):
move ./project_name ./releasesPowershell:
Move-Item -Path .\project_name -Destination .\releases\If youtarget for Windows when building don't forget that it'sproject_name.exe
Now you can make multiple releases and all of your needed static files will be available!
Summing it up:
Example Script for building to 3 common targets:
GOOS=darwin GOARCH=amd64 packr build && mv ./project_name ./releases/darwin-project_name \ && GOOS=linux GOARCH=amd64 packr build && mv ./project_name ./releases/linux-project_name \ && GOOS=windows GOARCH=386 packr build && mv ./project_name.exe ./releases/project_name.exe \ && packr cleanDebugging
Thepackr command passes all arguments down to the underlyinggo command, this includes the-v flag to print outgo 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 thepackr command is doing when it is run.
Documentation¶
Index¶
- Constants
- Variables
- func PackBytes(box string, name string, bb []byte)
- func PackBytesGzip(box string, name string, bb []byte) error
- func PackJSONBytes(box string, name string, jbb string) error
- func UnpackBytes(box string)
- type Box
- func (b Box) AddBytes(path string, t []byte) error
- func (b Box) AddString(path string, t string) error
- func (b Box) Bytes(name string) []bytedeprecated
- func (b Box) Find(name string) ([]byte, error)
- func (b Box) FindString(name string) (string, error)
- func (b Box) Has(name string) bool
- func (b Box) List() []string
- func (b Box) MustBytes(name string) ([]byte, error)deprecated
- func (b Box) MustString(name string) (string, error)deprecated
- func (b Box) Open(name string) (http.File, error)
- func (b Box) String(name string) stringdeprecated
- func (b Box) Walk(wf WalkFunc) error
- func (b Box) WalkPrefix(prefix string, wf WalkFunc) error
- type File
- type WalkFunc
Constants¶
const Version = "v1.30.1"Variables¶
var (// ErrResOutsideBox gets returned in case of the requested resources being outside the boxErrResOutsideBox =fmt.Errorf("Can't find a resource outside the box"))
var GoBin =envy.GoBinGoBin returns the current GO_BIN env varor if it's missing, a default of "go"
var GoPath =envy.GoPathGoPath returns the current GOPATH env varor if it's missing, the default.
Functions¶
funcPackBytesGzip¶added inv1.5.0
PackBytesGzip packets the gzipped compressed bytes into a box.
funcPackJSONBytes¶
PackJSONBytes packs JSON encoded bytes for a file into a box.
funcUnpackBytes¶added inv1.12.0
func UnpackBytes(boxstring)
UnpackBytes unpacks bytes for specific box.
Types¶
typeBox¶
type Box struct {Pathstring// contains filtered or unexported fields}Box represent a folder on a disk you want tohave access to in the built Go binary.
funcNewBox¶
NewBox returns a Box that can be used toretrieve files from either disk or the embeddedbinary.
func (Box)AddString¶added inv1.10.6
AddString converts t to a byteslice and delegates to AddBytes to add to b.data
func (Box)Find¶added inv1.14.0
Find returns either the byte slice of the requestedfile or an error if it can not be found.
func (Box)FindString¶added inv1.14.0
FindString returns either the string of the requestedfile or an error if it can not be found.