zpack

package module
Version: v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2020 License: MIT Imports: 13 Imported by: 0

README

Build Status codecov GoDoc

zpack is yet another way to pack static data in Go binaries.

Why? Because I don't like relying on external binaries, and many other solutions do. zpack just writes data to the specified file as []byte().

Basic usage:

// +build go_run_only

package main

import (
    "fmt"
    "os"

    "zgo.at/zpack"
)

func main() {
    err := zpack.Pack(map[string]map[string]string{
        // Pack ./db/schema.sql in ./db/pack.go as the variable "Schema".
        "./db/pack.go": map[string]string{
            "Schema": "./db/schema.sql",
        },

        // Pack all files in the "./public" and "./tpl" directories.
        "./handlers/pack.go": map[string]string{
            "packPublic": "./public",
            "packTpl":    "./tpl",
        },
    })
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

Then go generate ./... and presto!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dir

func Dir(fp io.Writer, varname, dir string, ignore ...string) error

Dir recursively writes all files in a directory as variables.

The ignore patterns are matched by strings.HasSuffix().

func File

func File(fp io.Writer, varname, path string) error

File writes a single file as a variable.

func Format

func Format(path string) error

Format the given file with gofmt.

func Header(fp io.Writer, pkg string) error

Header writes a file header, which is a code generation comment and package declaration.

func Pack

func Pack(data map[string]map[string]string, ignore ...string) error

Pack data to a Go file.

The data is a map where the key is the filename to store the packed data, and the contents a variable -> contents mapping.

Example:

err := zpack.Pack(map[string]map[string]string{
    "./db/pack.go": map[string]string{
        "Schema": "./db/schema.sql",
    },
    "./handlers/pack.go": map[string]string{
        "packPublic": "./public",
        "packTpl":    "./tpl",
    },
})
if err != nil {
    fmt.Fprintln(os.Stderr, err)
    os.Exit(1)
}

The ignore patterns are matched by strings.HasSuffix().

func Varname

func Varname(s string) string

Varname replaces any sequence of invalid identifier characters with an _.

Types

This section is empty.

Source Files

Jump to

Keyboard shortcuts

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