• This repository has been archived on 22/Aug/2022
  • Stars
    star
    1,185
  • Rank 39,473 (Top 0.8 %)
  • Language
    Go
  • License
    MIT License
  • Created over 5 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Embed static files in Go binaries (replacement for gobuffalo/packr)

Pkger

github.com/markbates/pkger is a tool for embedding static files into Go binaries. It will, hopefully, be a replacement for github.com/gobuffalo/packr/v2.

Requirements

  • Go 1.13+
  • Go Modules

How it Works (Module Aware Pathing)

Pkger is powered by the dark magic of Go Modules, so they're like, totally required.

With Go Modules pkger can resolve packages with accuracy. No more guessing and trying to figure out build paths, GOPATHS, etc... for this tired old lad.

With the module's path correctly resolved, it can serve as the "root" directory for that module, and all files in that module's directory are available.

Paths:

  • Paths should use UNIX style paths: /cmd/pkger/main.go
  • If unspecified the path's package is assumed to be the current module.
  • Packages can specified in at the beginning of a path with a : seperator. github.com/markbates/pkger:/cmd/pkger/main.go
  • There are no relative paths. All paths are absolute to the modules root.
  • Fully-qualified paths are embedded into the metadata of your static assets. If this behavior is undesirable, a preference is to build in a containerized environ, like docker, where the path strings are not ex-filtrating data about your development environment.
"github.com/gobuffalo/buffalo:/go.mod" => /go/pkg/mod/github.com/gobuffalo/[email protected]/go.mod

CLI

Installation

$ go get github.com/markbates/pkger/cmd/pkger
$ pkger -h

Usage

$ pkger

The result will be a pkged.go file in the root of the module with the embedded information and the package name of the module.

// ./pkged.go
package <.>

// Pkger stuff here

The -o flag can be used to specify the directory of the pkged.go file.

$ pkger -o cmd/reader

The result will be a pkged.go file in the cmd/reader folder with the embedded information and the package name of that folder.

// cmd/reader/pkged.go
package <reader>

// Pkger stuff here

Including Files at Package Time

There may be reasons where you don't reference a particular file, or folder, that you want embedded in your application, such as a build artifact.

To do this you may use either the github.com/markbates/pkger#Include function to set a no-op parser directive to include the specified path.

Alternatively, you may use the -include flag with the pkger and pkger list commands.

$ pkger list -include /actions -include github.com/gobuffalo/buffalo:/app.go

app
 > app:/actions
 > app:/actions/actions.go
 > app:/assets
 > app:/assets/css
 > app:/assets/css/_buffalo.scss
 > app:/assets/css/application.scss
 > app:/assets/images
 > app:/assets/images/favicon.ico
 > app:/assets/images/logo.svg
 > app:/assets/js
 > app:/assets/js/application.js
 > app:/go.mod
 > app:/locales/all.en-us.yaml
 > app:/public
 > app:/public/assets
 > app:/public/assets/.keep
 > app:/public/assets/app.css
 > app:/public/images
 > app:/public/images/img1.png
 > app:/public/index.html
 > app:/public/robots.txt
 > app:/templates
 > app:/templates/_flash.plush.html
 > app:/templates/application.plush.html
 > app:/templates/index.plush.html
 > app:/web
 > app:/web/web.go
 > github.com/gobuffalo/buffalo:/app.go
 > github.com/gobuffalo/buffalo:/logo.svg

Reference Application

The reference application for the README examples, as well as all testing, can be found at https://github.com/markbates/pkger/tree/master/pkging/pkgtest/testdata/ref.

├── actions
│   └── actions.go
├── assets
│   ├── css
│   │   ├── _buffalo.scss
│   │   └── application.scss
│   ├── images
│   │   ├── favicon.ico
│   │   └── logo.svg
│   └── js
│       └── application.js
├── go.mod
├── go.sum
├── locales
│   └── all.en-us.yaml
├── main.go
├── mod
│   └── mod.go
├── models
│   └── models.go
├── public
│   ├── assets
│   │   └── app.css
│   ├── images
│   │   └── img1.png
│   ├── index.html
│   └── robots.txt
├── templates
│   ├── _flash.plush.html
│   ├── application.plush.html
│   └── index.plush.html
└── web
    └── web.go

13 directories, 20 files

API Usage

Pkger's API is modeled on that of the os package in Go's standard library. This makes Pkger usage familiar to Go developers.

The two most important interfaces are github.com/markbates/pkger/pkging#Pkger and github.com/markbates/pkger/pkging#File.

type Pkger interface {
  Parse(p string) (Path, error)
  Current() (here.Info, error)
  Info(p string) (here.Info, error)
  Create(name string) (File, error)
  MkdirAll(p string, perm os.FileMode) error
  Open(name string) (File, error)
  Stat(name string) (os.FileInfo, error)
  Walk(p string, wf filepath.WalkFunc) error
  Remove(name string) error
  RemoveAll(path string) error
}

type File interface {
  Close() error
  Info() here.Info
  Name() string
  Open(name string) (http.File, error)
  Path() Path
  Read(p []byte) (int, error)
  Readdir(count int) ([]os.FileInfo, error)
  Seek(offset int64, whence int) (int64, error)
  Stat() (os.FileInfo, error)
  Write(b []byte) (int, error)
}

These two interfaces, along with the os#FileInfo, provide the bulk of the API surface area.

Open

func run() error {
	f, err := pkger.Open("/public/index.html")
	if err != nil {
		return err
	}
	defer f.Close()

	info, err := f.Stat()
	if err != nil {
		return err
	}

	fmt.Println("Name: ", info.Name())
	fmt.Println("Size: ", info.Size())
	fmt.Println("Mode: ", info.Mode())
	fmt.Println("ModTime: ", info.ModTime())

	if _, err := io.Copy(os.Stdout, f); err != nil {
		return err
	}
	return nil
}

Stat

func run() error {
	info, err := pkger.Stat("/public/index.html")
	if err != nil {
		return err
	}

	fmt.Println("Name: ", info.Name())
	fmt.Println("Size: ", info.Size())
	fmt.Println("Mode: ", info.Mode())
	fmt.Println("ModTime: ", info.ModTime())

	return nil
}

Walk

func run() error {
	w := tabwriter.NewWriter(os.Stdout, 0, 0, 0, ' ', tabwriter.Debug)
	defer w.Flush()

	return pkger.Walk("/public", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		fmt.Fprintf(w,
			"%s \t %d \t %s \t %s \t\n",
			info.Name(),
			info.Size(),
			info.Mode(),
			info.ModTime().Format(time.RFC3339),
		)

		return nil
	})

}

Understanding the Parser

The github.com/markbates/pkger/parser#Parser works by statically analyzing the source code of your module using the go/parser to find a selection of declarations.

The following declarations in your source code will tell the parser to embed files or folders.

  • pkger.Dir("<path>") - Embeds all files under the specified path.
  • pkger.Open("<path>") - Embeds the file, or folder, of the specified path.
  • pkger.Stat("<path>") - Embeds the file, or folder, of the specified path.
  • pkger.Walk("<path>", filepath.WalkFunc) - Embeds all files under the specified path.
  • pkger.Include("<path>") - Include is a no-op that directs the pkger tool to include the desired file or folder.

CLI Usage

To see what declarations the parser has found, you can use the pkger parse command to get a JSON list of the declarations.

$ pkger parse

{
 ".": [
  {
   "file": {
    "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz",
    "Path": {
     "Pkg": "app",
     "Name": "/foo/bar/baz"
    },
    "Here": {
     "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
     "ImportPath": "app",
     "Module": {
      "Path": "app",
      "Main": true,
      "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
      "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
      "GoVersion": "1.13"
     },
     "Name": "main"
    }
   },
   "pos": {
    "Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
    "Offset": 629,
    "Line": 47,
    "Column": 27
   },
   "type": "pkger.MkdirAll",
   "value": "/foo/bar/baz"
  },
  {
   "file": {
    "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/foo/bar/baz/biz.txt",
    "Path": {
     "Pkg": "app",
     "Name": "/foo/bar/baz/biz.txt"
    },
    "Here": {
     "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
     "ImportPath": "app",
     "Module": {
      "Path": "app",
      "Main": true,
      "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
      "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
      "GoVersion": "1.13"
     },
     "Name": "main"
    }
   },
   "pos": {
    "Filename": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/main.go",
    "Offset": 706,
    "Line": 51,
    "Column": 25
   },
   "type": "pkger.Create",
   "value": "/foo/bar/baz/biz.txt"
  },
  ...
 ]
}

For a module aware list use the pkger list command.

$ pkger list

app
 > app:/assets
 > app:/assets/css
 > app:/assets/css/_buffalo.scss
 > app:/assets/css/application.scss
 > app:/assets/images
 > app:/assets/images/favicon.ico
 > app:/assets/images/logo.svg
 > app:/assets/js
 > app:/assets/js/application.js
 > app:/go.mod
 > app:/locales/all.en-us.yaml
 > app:/public
 > app:/public/assets
 > app:/public/assets/.keep
 > app:/public/assets/app.css
 > app:/public/images
 > app:/public/images/img1.png
 > app:/public/index.html
 > app:/public/robots.txt
 > app:/templates
 > app:/templates/_flash.plush.html
 > app:/templates/application.plush.html
 > app:/templates/index.plush.html
 > app:/web
 > app:/web/web.go
 > github.com/gobuffalo/buffalo:/logo.svg

The -json flag can be used to get a more detailed list in JSON.

$ pkger list -json

{
 "ImportPath": "app",
 "Files": [
  {
   "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
   "Path": {
    "Pkg": "app",
    "Name": "/assets"
   },
   "Here": {
    "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
    "ImportPath": "",
    "Module": {
     "Path": "app",
     "Main": true,
     "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
     "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
     "GoVersion": "1.13"
    },
    "Name": "assets"
   }
  },
  {
   "Abs": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets/css",
   "Path": {
    "Pkg": "app",
    "Name": "/assets/css"
   },
   "Here": {
    "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/assets",
    "ImportPath": "",
    "Module": {
     "Path": "app",
     "Main": true,
     "Dir": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref",
     "GoMod": "/go/src/github.com/markbates/pkger/pkging/pkgtest/testdata/ref/go.mod",
     "GoVersion": "1.13"
    },
    "Name": "assets"
   }
  },
  ...
}

More Repositories

1

goth

Package goth provides a simple, clean, and idiomatic way to write authentication packages for Go web applications.
Go
5,512
star
2

configatron

A super cool, simple, and feature rich configuration system for Ruby apps.
Ruby
600
star
3

grift

Go based task runner
Go
433
star
4

coffee-rails-source-maps

DO NOT USE THIS!! I DO NOT MAINTAIN IT!
Ruby
241
star
5

coffeebeans

The parts of CoffeeScript they forgot in Rails 3.1!
Ruby
228
star
6

cover_me

An RCov-esque coverage tool for Ruby 1.9
Ruby
204
star
7

refresh

Go
191
star
8

jquery-bootstrap-pagination

CoffeeScript
90
star
9

Programming-In-CoffeeScript

Source code for the Programming in CoffeeScript book.
CoffeeScript
63
star
10

going

Some helpful packages for writing Go apps.
Go
55
star
11

dj_remixes

Enhancements and improvements for Delayed Job 2.x
Ruby
42
star
12

mack

A Ruby web application framework
Ruby
41
star
13

warp_drive

Screw Rails Engines! Why not install a Warp Drive! Completely bundle up an ENTIRE Rails application into a gem, then load it into another application! It's that easy!
Ruby
34
star
14

myvim

My VIM setup. Use at your own risk.
Vim Script
34
star
15

buffla

URL Shortner written in Buffalo
Go
30
star
16

inflect

DEPRECATED: use github.com/gobuffalo/flect instead
Go
24
star
17

fluent-2014

Source code for the Angular Fundamentals workshop at FluentConf 2014.
JavaScript
22
star
18

ghi

Offline GitHub Issues Client
Go
19
star
19

cachetastic

A very simple, yet very powerful caching framework for Ruby
Ruby
18
star
20

delayed_job_extras

This project has moved to markbates/dj_remixes. Please do not follow this project!
Ruby
17
star
21

buffalo-heroku

Archived use github.com/gobuffalo/buffalo-heroku
Go
17
star
22

mongoid-tags-arent-hard

A tagging gem for Mongoid 3 that doesn't actually suck.
Ruby
16
star
23

distribunaut

A framework agnostic port of the mack-distributed package.
Ruby
16
star
24

yamler

Tools for using YAML w/ Ruby easier.
Ruby
11
star
25

gormrecipe

An example of using Gorm with Buffalo
Go
10
star
26

mack-more

All the extra stuff you could want for the Mack Framework.
Ruby
10
star
27

genosaurus

A simple and easy to use generator system for Ruby
Ruby
9
star
28

mongoid_delorean

THIS PROJECT IS NOT MAINTAINED. USE AT YOUR OWN RISK. NO PRs ACCEPTED.
Ruby
9
star
29

gocker

Go
9
star
30

informit_articles

InformIT Articles
Ruby
7
star
31

macker-s-guide

A User's Guide to the Mack Framework
JavaScript
7
star
32

gemstub

A simple gem for creating the stub of other gems
Ruby
7
star
33

api_doc

Easily generate API docs from RSpec/Rails
Ruby
7
star
34

faraday_bang

Adds error raising ! methods to Faraday
Ruby
6
star
35

wailsx

WIP: Tools for working with Wails.io
Go
6
star
36

gem-freezer

A simple gem that helps you freeze gems, and their dependencies into your project.
Ruby
5
star
37

contextual

context.Context utilities
Go
5
star
38

nor-dev

Development Playground for Node on Rails (NOR)
CoffeeScript
4
star
39

willie

Easy testing of Go web type stuff
Go
4
star
40

cash

A Go package for working with cash.Money :)
Go
4
star
41

oncer

Go
4
star
42

markdownr

Go
4
star
43

gobular

Go Regular Expression Tester
Go
4
star
44

breach

Ruby
3
star
45

gemtronics

A much better of managing your gem dependencies
Ruby
3
star
46

buffalo-bootstrap

A plugin for github.com/gobuffalo/buffalo for generating Bootstrap stuff
Go
3
star
47

braai

Fully Extensible Templates
Ruby
3
star
48

deplist

Get a list of all the Go dependencies for the current directory
Go
3
star
49

mack-user_auth_portlet

A collection of Portlets for the Mack Framework
Ruby
3
star
50

deano

A starter template for Sinatra Applications
Ruby
3
star
51

buffalo-rails

Go
3
star
52

sigtx

a context implementation for signal capturing
Go
3
star
53

micro-mack

A 'micro' version of the Mack Framework
Ruby
3
star
54

miki

A lightweight, Mack-based Wiki powering www.mackwiki.com
Ruby
3
star
55

jim

Go
3
star
56

mocha-coffeescript-tmbundle

TextMate/Sublime Text 2 bundle for Mocha (CoffeeScript)
2
star
57

errx

Makefile
2
star
58

coke

Go
2
star
59

bluffalo

Go
2
star
60

assoc

Go
2
star
61

chalk

Quickly and easily create a pool of `go routines`.
Go
2
star
62

control

Go
2
star
63

rails_templates

Templates for building Rails applications
Ruby
2
star
64

ekar

Ekar, a Rake replacement, maybe?
Ruby
2
star
65

rack-cachely

Rack Middleware for working with the CachelyApp Page Cache Service
Ruby
2
star
66

mark_facets

Just a collection of common extensions and add-ons I use in most Ruby/Rails projects.
Ruby
2
star
67

snooze_force

SnoozeForce - A Client for Saleforce.com's REST API
Ruby
2
star
68

syncx

Go
2
star
69

opal-rb-example

A "todo" example using the Opal.rb Project.
Ruby
2
star
70

blabber_mouth

Easy model-like notification system for Ruby. (A framework agnostic port of the mack-notifier package.)
Ruby
2
star
71

bostongo

Go
2
star
72

ringo

Go
1
star
73

labs

Go
1
star
74

gjs

HTML
1
star
75

haste

Go
1
star
76

ek

A simple echo server
Go
1
star
77

FakeWeb.js

A port of FakeWeb (Ruby) to help test JavaScript AJAX apps
CoffeeScript
1
star
78

content_o_matic

A gem to download the contents of a page and include them into your application.
Ruby
1
star
79

mytmux

1
star
80

gurl

Go
1
star
81

tt

T.T. The Test Running Bear!
Go
1
star
82

gonit

Go
1
star
83

cleo

Go
1
star
84

buffalo-trash

Go
1
star
85

bloggy

HTML
1
star
86

markbates.github.com

HTML
1
star
87

gentronics

Go
1
star
88

clio

Go
1
star
89

bert

Go
1
star
90

cobble

A very simple and easy library to help facilitate the building and creating of objects.
Ruby
1
star
91

clam

Go
1
star
92

konacha-sinatra

Ruby
1
star
93

hmax

A Golang package to make working with HMAC requests easier.
Go
1
star
94

safe

Go
1
star