packr

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2017 License: MIT Imports: 12 Imported by: 0

README

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.

Installation

$ 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.

// set up a new box by giving it a 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")
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
--> packing foo/foo-packr.go
--> packing example-packr.go

Then run your go build command like normal.

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
----> cleaning up example-packr.go
----> cleaning up foo/foo-packr.go

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PackBytes

func PackBytes(box string, name string, bb []byte)

PackBytes packs bytes for a file into a box.

func PackJSONBytes

func PackJSONBytes(box string, name string, jbb string) error

PackJSONBytes packs JSON encoded bytes for a file into a box.

Types

type Box

type Box struct {
	Path string
	// contains filtered or unexported fields
}

Box represent a folder on a disk you want to have access to in the built Go binary.

func NewBox

func NewBox(path string) Box

NewBox returns a Box that can be used to retrieve files from either disk or the embedded binary.

func (Box) Bytes

func (b Box) Bytes(name string) []byte

Bytes of the file asked for or an empty byte slice.

func (Box) Has

func (b Box) Has(name string) bool

func (Box) MustBytes

func (b Box) MustBytes(name string) ([]byte, error)

MustBytes returns either the byte slice of the requested file or an error if it can not be found.

func (Box) MustString

func (b Box) MustString(name string) (string, error)

MustString returns either the string of the requested file or an error if it can not be found.

func (Box) Open

func (b Box) Open(name string) (http.File, error)

Open returns a File using the http.File interface

func (Box) String

func (b Box) String(name string) string

String of the file asked for or an empty string.

func (Box) Walk

func (b Box) Walk(wf WalkFunc) error

type File

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)
}

type WalkFunc

type WalkFunc func(string, File) error

Directories

Path Synopsis
foo
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL