pack

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2020 License: MIT Imports: 10 Imported by: 0

README

Build Status Go Report GoDoc

Go Pack

Lightweight dependency-free embedding of static files into Go executables.

There are other "embedding" type modules, namely:

However, those implementation did not fit the use case I was targeting.

NOTE: Go Pack currently breaks is using code signing on macOS, there is a fix possible but it will break the existing steps.

Implementation

The implementation assumes the following binary file structure

executable + zip contents + zip size

offset 0
Executable Data
offset x - y
Zip Data
offset x - 10
Zip Size (y)
offset x

The logic attempts to find a string represented size marker (10 bytes) at the end of the Go executable. This marker is used to calculate the offset of the zip contents from the end of the executable. The zip contents are optionally buffered and used to access the static files in the zip contents or extracted to the directory containing the executable.

Usage

For the demo, the following steps were used to generate the expected executable file format:

    zip pack cmd/index.html
    printf "%010d" `stat -f%z pack.zip` >> pack.zip
    mv TheExecutable main.pack; cat main.pack pack.zip > TheExecutable
    chmod +x TheExecutable

The API is simply:

package main

import "github.com/mlavergn/gopack/src/pack"

func main() {
    pack := gopack.NewPack()
    // A) extract to working directory
    pack.Extract()
    // -or-
    // B) read from memory buffer
    pack.Load()
    // b1) string value
    stringValue := pack.String("cmd/index.html")
    // b2) []byte value
    byteValue := pack.Bytes("cmd/index.html")
    // b3) pipe value (eg. http.resp)
    reader := pack.Pipe("cmd/index.html")
    ioutil.Copy(resp, reader)
}

Documentation

Index

Constants

View Source
const Version = "1.2.0"

Version export

Variables

View Source
var DEBUG = false

DEBUG toggle

Functions

This section is empty.

Types

type Pack

type Pack struct {
	ZipFiles map[string]*zip.File
}

Pack export

func NewPack

func NewPack() *Pack

NewPack init

func (*Pack) Bytes

func (id *Pack) Bytes(filePath string) ([]byte, error)

Bytes export

func (*Pack) Container

func (id *Pack) Container() string

Container export

func (*Pack) Extract

func (id *Pack) Extract() ([]string, error)

Extract export

func (*Pack) File

func (id *Pack) File(filePath string) (*string, error)

File export

func (*Pack) Load

func (id *Pack) Load() ([]string, error)

Load export

func (*Pack) LoadedPaths

func (id *Pack) LoadedPaths() []string

LoadedPaths export

func (*Pack) Pipe

func (id *Pack) Pipe(filePath string) (io.Reader, error)

Pipe export

func (*Pack) Reader

func (id *Pack) Reader() (*zip.Reader, error)

Reader export

func (*Pack) String

func (id *Pack) String(filePath string) (*string, error)

String export

Jump to

Keyboard shortcuts

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