bindata

package module
v0.0.0-...-275c61d Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2021 License: Apache-2.0 Imports: 14 Imported by: 0

README

bindata

This package converts any file into go code. It is heavily inspired by the excellent go-bindata.

Why another library?

I found myself trying a archive quite a few (as in 1000s) of small files. Other libraries make a trade off to compress all files individually in order to speed up access. This library will archive and compress all files together to decrease the size of the resulting file. On access, it will decompress all files and keep them in memory for frequent access.

Usage

bindata can be used as library or as a commandline tool.

Usage as Library

Import the following package to use bindata in your code:

github.com/mhelmich/bindata

The following snippet explains how to use bindata to create a bindata file from your Go program.

import bd "github.com/mhelmich/bindata"

...

err = bd.New(
    []string{"path/to/files/*.json"},
    bd.PackageName("test"),
    bd.OutputFile("test/bindata_files.go"),
    bd.Archiver(bd.Tar),
    bd.Compressor(bd.Bz),
).Archive()

Usage as Cmd Tool

Run the following to install bindata:

> go get -u github.com/mhelmich/bindata/cmd/bindata

The simplest call of bindata passes only file paths to it. In this invocation, the package name and the output file will be defaulted to bindata and bindata/bindata.go respectively.

> bindata dir/file1.json dir/file2.json

However bindata paths can contain wildcards/globs:

> bindata dir/*.json dir/**/*.json

bindata can be configured to write the output file to a particular path. In this case, bindata automatically uses the last element of the path as package name:

> bindata -o mypackage/bindata_files.go dir/*.json dir/**/*.json

If for some reason you need to configure the package name of the generated bindata file independently from the given path, use the package flag:

> bindata -o mypackage/bindata_files.go -package otherpackage dir/*.json dir/**/*.json

Configuration

Right now bindata supports the following configurations:

  • TarBz - a bz compressed tar archive

Accessing a File

The generated bindata file exports two public functions FileNames and ReadFile.

ReadFile

ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

It can be used like this:

data, err := bindata.ReadFile(an)
if err != nil {
    // err != nil if the file wasn't found in the bindata file
}
FileNames

FileNames returns a list of all files in this bindata file.

It can be used like this:

for _, fileName := range bindata.FileNames() {
    // fileName contains a file name now
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoFilesMatched is returned if no files could be found in the given paths
	ErrNoFilesMatched = fmt.Errorf("no files matched the path provided")
)

Functions

func FileNames

func FileNames() []string

FileNames returns a list of all files in this bindata file.

func ReadFile

func ReadFile(path string) ([]byte, error)

ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

Types

type ArchiverLayer

type ArchiverLayer int

ArchiverLayer -

const (
	// NoArchiver -
	NoArchiver ArchiverLayer = iota
	// Tar -
	Tar
)

func (ArchiverLayer) String

func (a ArchiverLayer) String() string

type Bindata

type Bindata interface {
	Archive() error
}

Bindata -

func New

func New(paths []string, setters ...Option) Bindata

New creates and configures a new instance of bindata.

type CompressorLayer

type CompressorLayer int

CompressorLayer -

const (
	// NoCompressor -
	NoCompressor CompressorLayer = iota
	// Bz -
	Bz
)

func (CompressorLayer) String

func (c CompressorLayer) String() string

type Option

type Option func(*Options)

Option -

func Archiver

func Archiver(a ArchiverLayer) Option

Archiver defines the archiver to use (defaults to tar).

func Compressor

func Compressor(c CompressorLayer) Option

Compressor defines the compressor to use (default to bz).

func OutputFile

func OutputFile(output string) Option

OutputFile defines the name of the output file that is being generated.

func PackageName

func PackageName(packageName string) Option

PackageName defines the package name that should appear in the generated file.

type Options

type Options struct {
	Paths       []string
	PackageName string
	OutputFile  string
	Archiver    ArchiverLayer
	Compressor  CompressorLayer
}

Options -

type Type

type Type int

Type -

const (
	// TarBz -
	TarBz Type = iota
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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