assets

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2017 License: MIT Imports: 23 Imported by: 0

README

go-assets

Build Status Go Report Card Coverage Status GoDoc

Go-assets collects assets from multiple sources. Assets can be stored in memory as a http.FileSystem and provided for your application. They can also be combined into a go source code using vfsgen. This is best used with go generate to embed the assets into binaries.

Usage

The Retrieve function collects assets into memory and returns them as a http.FileSystem. The Compile function creates a go source file from the collected assets. This source code contains a single variable that points to the file system.

func Retrieve(sources []*Source) (http.FileSystem, error)

func Compile(sources []*Source, filePath string, pkgName string, varName string, opts *Opts) error

With Compile, the filePath argument specifies the location of the asset source. pkgName and varName specifies the package and variable name to use. The optional opts parameter can specify build tags to be added to the source file (BuildTags) and a custom comment text for the variable (VariableComment).

Each asset source is described with the below structure.

type Source struct {
  Path     string
  Location string
  Checksum *Checksum
  Archive  *Archive
}

Here Path tells the path of the resulting asset(s) in the output file system (details below), while Location, Checksum and Archive each correspond to one of the threep rocessing steps below.

Path strings in Path and other field should only use forward slashes (/) for separator.

1. Retrieval

The asset source file is retrieved from the specified Location.

If Location starts with http:// or https://, it is considered a URL, and the file is downloaded.
If Location contains a glob pattern, the pattern is applied to the local file system, and all matching files are retrieved.
Otherwise, Location is assumed to contain a file path, and that file is retrieved from the local file system.

If multiple files were retrieved (this can only happen when using a glob pattern), processing stops here. The path for these assets is calculated as follows.

  1. take the longest path prefix in Location that does not contain glob patterns
  2. trim this prefix from the file paths (as matched by the pattern)
  3. prefix the previous value with the value in Path

As an example, if Path is set to "assets", and Location is set to "input/data/data[0-9].txt", the resulting files will be located at "assets/data0.txt", "assets/data1.txt", and so on.

If only a single file was retrieved, processing continues. If the Checksum field is not nil, the file checksum is verified and processing halts with an error on mismatch. Then, if Archive is not nil, the file is processed as an archive.

If Archive is nil, processing stops here, and thefile is stored at the path specified by Path.

2. Checksum

Checksum verification can be requested with the following structure.

type Checksum struct {
	Algo  ChecksumAlgo
	Value string
}

Here Algo specifies the checksum algorithm to use, and Value is the precalculated checksum value. If a mismatch is found during verification, an error is returned. The currently supported checksum algorithms are: MD5, SHA1, SHA256 and SHA512.

3. Archive extraction

Archive extraction can be requested with the following structure.

type Archive struct {
	Format     ArchiveFormat
	PathMapper PathMapper
}

Here Format specifies the type of archive. Currently Zip and TarGz are supported.

The PathMapper function can be used to filter files from the archive and specify custom paths for them. If it is set to nil, all files are kept and are stored at the same path they were found in the archive.

type PathMapper func(string) string

The PathMapper function is invoked for each file path in the archive. If it returns "" the file is dropped. Otherwise the file is kept and stored at the path returned by the function.

For common cases the ReMap function can be used for generating a PathMapper function.

func ReMap(pattern string, replacement string) PathMapper

The pattern argument takes a regular expression. This pattern is matched against all file paths in the archive and only matching files are kept. For these files the replacement string specifies the storage path. In order to ensure unique paths for files, the pattern should contain capturing groups and the replacement string should contain backreferences.

Example

See a usage example in the gmdd project.

Documentation

Index

Constants

View Source
const (
	// Zip is the zip file format.
	Zip = iota
	// TarGz is the tar.gz file format.
	TarGz
)
View Source
const (
	// MD5 is the MD5 algorithm.
	MD5 = iota
	// SHA1 is the SHA1 algorithm.
	SHA1
	// SHA256 is the SHA256 algorithm.
	SHA256
	// SHA512 is the SHA512 algorithm.
	SHA512
)

Variables

View Source
var (
	// ErrChecksumMismatch is returned when the expected checksum differs from the calculated one
	ErrChecksumMismatch = errors.New("checksum mismatch")
	// ErrChecksumUnknown is returned when an invalid checksum algorithm is specified
	ErrChecksumUnknown = errors.New("unknown checksum algorithm")
)
View Source
var (
	// ErrArchiveUnknown is returned when an invalid archive format is specified
	ErrArchiveUnknown = errors.New("unknown archive format")
)
View Source
var (
	// ErrNoMatch is returned when the glob does not match any file.
	ErrNoMatch = errors.New("no match")
)

Functions

func Compile

func Compile(sources []*Source, filePath string, pkgName string, varName string, opts *Opts) error

Compile retrieves and processes the specified asset sources, and compiles them to the specified variable in the source file.

func Retrieve added in v0.2.1

func Retrieve(sources []*Source) (http.FileSystem, error)

Retrieve retrieves and processes the specified asset sources, and returns them using a http.FileSystem interface.

Types

type Archive

type Archive struct {
	Format     ArchiveFormat
	PathMapper PathMapper
}

Archive describes an archive format for the asset source.

type ArchiveError

type ArchiveError struct {
	Path string
	Err  error
}

ArchiveError is returned when there is a problem processing the archive

func (*ArchiveError) Error

func (e *ArchiveError) Error() string

type ArchiveFormat

type ArchiveFormat int

ArchiveFormat enumerates archive formats.

type Checksum

type Checksum struct {
	Algo  ChecksumAlgo
	Value string
}

Checksum describes a checksum verification for an asset source.

type ChecksumAlgo

type ChecksumAlgo int

ChecksumAlgo enumerates checksum algorihms.

type ChecksumError added in v0.3.0

type ChecksumError struct {
	Location string
	Err      error
}

ChecksumError is returned when there is a checksum problem with an asset source

func (*ChecksumError) Error added in v0.3.0

func (e *ChecksumError) Error() string

type Opts added in v0.2.0

type Opts struct {
	// BuildTags are the build tags in the generated source code.
	// Defaults to no tags.
	BuildTags string

	// VariableComment is the comment of the variable in the generated source code.
	// Defaults to "<VariableName> implements a http.FileSystem.".
	VariableComment string
}

Opts provides optional parameters to the Compile function.

type PathMapper

type PathMapper func(string) string

PathMapper specifies a function that is executed on all files in the archive. The mapper receives the full path to each file in the archive and returns the path to use in the asset file system. If "" is returned, the file is dropped.

func ReMap added in v0.2.2

func ReMap(pattern string, replacement string) PathMapper

ReMap returns a PathMapper that compares file paths to the input pattern and maps matches to the replacement string (see Regexp.ReplaceAllString).

type RetrieveError

type RetrieveError struct {
	Location string
	Err      error
}

RetrieveError is returned when there is a problem retrieving an asset source

func (*RetrieveError) Error

func (e *RetrieveError) Error() string

type Source added in v0.2.0

type Source struct {
	Path     string
	Location string
	Checksum *Checksum
	Archive  *Archive
}

Source describes an asset source to be retrieved and processed.

Jump to

Keyboard shortcuts

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