mage: generate assetfs
This commit is contained in:
parent
814f75be29
commit
8aa459b429
|
@ -11,7 +11,7 @@
|
|||
branch = "master"
|
||||
name = "github.com/Xe/eclier"
|
||||
packages = ["."]
|
||||
revision = "d6e616ba8bbeba4bbcad640f00ca897524936927"
|
||||
revision = "f56dc59db6b1cf87ae6bbaaf6aa985a285da33a1"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
|
@ -229,6 +229,12 @@
|
|||
packages = ["."]
|
||||
revision = "3acf1b3de25d89c7688c63bb45f6b07f566555ec"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/elazarl/go-bindata-assetfs"
|
||||
packages = ["."]
|
||||
revision = "30f82fa23fd844bd5bb1e5f216db87fd77b5eb43"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/facebookgo/flagenv"
|
||||
|
@ -977,6 +983,6 @@
|
|||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "16ca48f15cb2a6b7b52c758e9cc9aacc15a290406fc78798595d4cb5a513720b"
|
||||
inputs-digest = "7ecddb07636e18d58d1f17cba24e6d08f8480c86449337ca64ac0812b1af1cf5"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"git.xeserv.us/xena/route/internal/gluaroute"
|
||||
edata "git.xeserv.us/xena/route/proto/eclier"
|
||||
"github.com/Xe/eclier"
|
||||
"github.com/Xe/gluanetrc"
|
||||
"github.com/Xe/x/tools/glue/libs/gluaexpect"
|
||||
|
@ -72,6 +73,7 @@ func main() {
|
|||
opts := []eclier.RouterOption{
|
||||
eclier.WithGluaCreationHook(preload),
|
||||
eclier.WithScriptHome(scriptsLoc),
|
||||
eclier.WithFilesystem("protogen", edata.AssetFS()),
|
||||
}
|
||||
|
||||
err := filepath.Walk(pluginLoc, func(path string, info os.FileInfo, err error) error {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// +build ignore
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
|
@ -13,10 +15,6 @@ import (
|
|||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Scanner interface {
|
||||
Scan(...interface{}) error
|
||||
}
|
||||
|
||||
type PostgresStorage struct {
|
||||
ds *dotsql.DotSql
|
||||
db *sql.DB
|
||||
|
|
|
@ -8,3 +8,7 @@ type Storage interface {
|
|||
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Scanner interface {
|
||||
Scan(...interface{}) error
|
||||
}
|
||||
|
|
8
mage.go
8
mage.go
|
@ -17,6 +17,7 @@ import (
|
|||
|
||||
var wd string
|
||||
var arches []string
|
||||
var bins []string
|
||||
|
||||
func init() {
|
||||
lwd, err := os.Getwd()
|
||||
|
@ -25,6 +26,7 @@ func init() {
|
|||
wd = lwd
|
||||
|
||||
arches = []string{"amd64", "ppc64", "386", "arm", "arm64"}
|
||||
bins = []string{"route-httpagent", "route-cli", "routed", "terraform-provider-route", "construct"}
|
||||
}
|
||||
|
||||
const pkgBase = "git.xeserv.us/xena/route/"
|
||||
|
@ -37,7 +39,7 @@ func buildBins(goos, goarch string) {
|
|||
|
||||
os.MkdirAll(filepath.Join(d, goos, goarch), 0777)
|
||||
|
||||
for _, pkg := range []string{"route-httpagent", "route-cli", "routed", "terraform-provider-route"} {
|
||||
for _, pkg := range bins {
|
||||
env := []string{"GOOS=" + goos, "GOARCH=" + goarch}
|
||||
goBuild(ctx, env, filepath.Join(d, goos, goarch), "cmd/"+pkg)
|
||||
goInstall(ctx, env, "cmd/"+pkg)
|
||||
|
@ -167,6 +169,8 @@ func Tools(ctx context.Context) {
|
|||
"github.com/golang/protobuf/protoc-gen-go",
|
||||
"github.com/twitchtv/twirp/protoc-gen-twirp",
|
||||
"github.com/Xe/twirp-codegens/cmd/protoc-gen-twirp_eclier",
|
||||
"github.com/jteeuwen/go-bindata/go-bindata",
|
||||
"github.com/elazarl/go-bindata-assetfs/go-bindata-assetfs",
|
||||
}
|
||||
|
||||
for _, t := range tools {
|
||||
|
@ -192,6 +196,8 @@ func Generate(ctx context.Context) {
|
|||
defer fout.Close()
|
||||
|
||||
e.EncodeTo(fout)
|
||||
|
||||
shouldWork(ctx, nil, filepath.Join("./proto/eclier"), "go-bindata-assetfs", "-pkg", "eclier_data", ".")
|
||||
}
|
||||
|
||||
// Vars shows the various variables that this magefile uses.
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
package eclier_data
|
||||
|
||||
import "net/http"
|
||||
|
||||
func AssetFS() http.FileSystem {
|
||||
return assetFS()
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -13,9 +13,9 @@ local svc = require "svc"
|
|||
local fs = flag.new()
|
||||
|
||||
-- flags for Route
|
||||
fs:string("host", "", "value for message arg host")
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("creator", "", "value for message arg creator")
|
||||
fs:string("host", "", "value for message arg host")
|
||||
|
||||
script.usage = fs:usage()
|
||||
|
||||
|
|
|
@ -13,9 +13,9 @@ local svc = require "svc"
|
|||
local fs = flag.new()
|
||||
|
||||
-- flags for Route
|
||||
fs:string("host", "", "value for message arg host")
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("creator", "", "value for message arg creator")
|
||||
fs:string("host", "", "value for message arg host")
|
||||
|
||||
script.usage = fs:usage()
|
||||
|
||||
|
|
|
@ -13,10 +13,10 @@ local svc = require "svc"
|
|||
local fs = flag.new()
|
||||
|
||||
-- flags for Token
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("body", "", "value for message arg body")
|
||||
fs:strings("scopes", "value for message arg scopes")
|
||||
fs:bool("active", false, "value for message arg active")
|
||||
fs:string("id", "", "value for message arg id")
|
||||
|
||||
script.usage = fs:usage()
|
||||
|
||||
|
|
|
@ -13,10 +13,10 @@ local svc = require "svc"
|
|||
local fs = flag.new()
|
||||
|
||||
-- flags for Token
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("body", "", "value for message arg body")
|
||||
fs:strings("scopes", "value for message arg scopes")
|
||||
fs:bool("active", false, "value for message arg active")
|
||||
fs:string("id", "", "value for message arg id")
|
||||
|
||||
script.usage = fs:usage()
|
||||
|
||||
|
|
|
@ -13,10 +13,10 @@ local svc = require "svc"
|
|||
local fs = flag.new()
|
||||
|
||||
-- flags for Token
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("body", "", "value for message arg body")
|
||||
fs:strings("scopes", "value for message arg scopes")
|
||||
fs:bool("active", false, "value for message arg active")
|
||||
fs:string("id", "", "value for message arg id")
|
||||
fs:string("body", "", "value for message arg body")
|
||||
|
||||
script.usage = fs:usage()
|
||||
|
||||
|
|
|
@ -150,7 +150,7 @@ func WithFilesystem(shortName string, fs http.FileSystem) RouterOption {
|
|||
}
|
||||
defer fin.Close()
|
||||
|
||||
childs, err := fin.ReadDir(-1)
|
||||
childs, err := fin.Readdir(-1)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
Copyright (c) 2014, Elazar Leibovich
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -0,0 +1,46 @@
|
|||
# go-bindata-assetfs
|
||||
|
||||
Serve embedded files from [jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata) with `net/http`.
|
||||
|
||||
[GoDoc](http://godoc.org/github.com/elazarl/go-bindata-assetfs)
|
||||
|
||||
### Installation
|
||||
|
||||
Install with
|
||||
|
||||
$ go get github.com/jteeuwen/go-bindata/...
|
||||
$ go get github.com/elazarl/go-bindata-assetfs/...
|
||||
|
||||
### Creating embedded data
|
||||
|
||||
Usage is identical to [jteeuwen/go-bindata](https://github.com/jteeuwen/go-bindata) usage,
|
||||
instead of running `go-bindata` run `go-bindata-assetfs`.
|
||||
|
||||
The tool will create a `bindata_assetfs.go` file, which contains the embedded data.
|
||||
|
||||
A typical use case is
|
||||
|
||||
$ go-bindata-assetfs data/...
|
||||
|
||||
### Using assetFS in your code
|
||||
|
||||
The generated file provides an `assetFS()` function that returns a `http.Filesystem`
|
||||
wrapping the embedded files. What you usually want to do is:
|
||||
|
||||
http.Handle("/", http.FileServer(assetFS()))
|
||||
|
||||
This would run an HTTP server serving the embedded files.
|
||||
|
||||
## Without running binary tool
|
||||
|
||||
You can always just run the `go-bindata` tool, and then
|
||||
|
||||
use
|
||||
|
||||
import "github.com/elazarl/go-bindata-assetfs"
|
||||
...
|
||||
http.Handle("/",
|
||||
http.FileServer(
|
||||
&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: AssetInfo, Prefix: "data"}))
|
||||
|
||||
to serve files embedded from the `data` directory.
|
|
@ -0,0 +1,167 @@
|
|||
package assetfs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultFileTimestamp = time.Now()
|
||||
)
|
||||
|
||||
// FakeFile implements os.FileInfo interface for a given path and size
|
||||
type FakeFile struct {
|
||||
// Path is the path of this file
|
||||
Path string
|
||||
// Dir marks of the path is a directory
|
||||
Dir bool
|
||||
// Len is the length of the fake file, zero if it is a directory
|
||||
Len int64
|
||||
// Timestamp is the ModTime of this file
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
func (f *FakeFile) Name() string {
|
||||
_, name := filepath.Split(f.Path)
|
||||
return name
|
||||
}
|
||||
|
||||
func (f *FakeFile) Mode() os.FileMode {
|
||||
mode := os.FileMode(0644)
|
||||
if f.Dir {
|
||||
return mode | os.ModeDir
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
func (f *FakeFile) ModTime() time.Time {
|
||||
return f.Timestamp
|
||||
}
|
||||
|
||||
func (f *FakeFile) Size() int64 {
|
||||
return f.Len
|
||||
}
|
||||
|
||||
func (f *FakeFile) IsDir() bool {
|
||||
return f.Mode().IsDir()
|
||||
}
|
||||
|
||||
func (f *FakeFile) Sys() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AssetFile implements http.File interface for a no-directory file with content
|
||||
type AssetFile struct {
|
||||
*bytes.Reader
|
||||
io.Closer
|
||||
FakeFile
|
||||
}
|
||||
|
||||
func NewAssetFile(name string, content []byte, timestamp time.Time) *AssetFile {
|
||||
if timestamp.IsZero() {
|
||||
timestamp = defaultFileTimestamp
|
||||
}
|
||||
return &AssetFile{
|
||||
bytes.NewReader(content),
|
||||
ioutil.NopCloser(nil),
|
||||
FakeFile{name, false, int64(len(content)), timestamp}}
|
||||
}
|
||||
|
||||
func (f *AssetFile) Readdir(count int) ([]os.FileInfo, error) {
|
||||
return nil, errors.New("not a directory")
|
||||
}
|
||||
|
||||
func (f *AssetFile) Size() int64 {
|
||||
return f.FakeFile.Size()
|
||||
}
|
||||
|
||||
func (f *AssetFile) Stat() (os.FileInfo, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// AssetDirectory implements http.File interface for a directory
|
||||
type AssetDirectory struct {
|
||||
AssetFile
|
||||
ChildrenRead int
|
||||
Children []os.FileInfo
|
||||
}
|
||||
|
||||
func NewAssetDirectory(name string, children []string, fs *AssetFS) *AssetDirectory {
|
||||
fileinfos := make([]os.FileInfo, 0, len(children))
|
||||
for _, child := range children {
|
||||
_, err := fs.AssetDir(filepath.Join(name, child))
|
||||
fileinfos = append(fileinfos, &FakeFile{child, err == nil, 0, time.Time{}})
|
||||
}
|
||||
return &AssetDirectory{
|
||||
AssetFile{
|
||||
bytes.NewReader(nil),
|
||||
ioutil.NopCloser(nil),
|
||||
FakeFile{name, true, 0, time.Time{}},
|
||||
},
|
||||
0,
|
||||
fileinfos}
|
||||
}
|
||||
|
||||
func (f *AssetDirectory) Readdir(count int) ([]os.FileInfo, error) {
|
||||
if count <= 0 {
|
||||
return f.Children, nil
|
||||
}
|
||||
if f.ChildrenRead+count > len(f.Children) {
|
||||
count = len(f.Children) - f.ChildrenRead
|
||||
}
|
||||
rv := f.Children[f.ChildrenRead : f.ChildrenRead+count]
|
||||
f.ChildrenRead += count
|
||||
return rv, nil
|
||||
}
|
||||
|
||||
func (f *AssetDirectory) Stat() (os.FileInfo, error) {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// AssetFS implements http.FileSystem, allowing
|
||||
// embedded files to be served from net/http package.
|
||||
type AssetFS struct {
|
||||
// Asset should return content of file in path if exists
|
||||
Asset func(path string) ([]byte, error)
|
||||
// AssetDir should return list of files in the path
|
||||
AssetDir func(path string) ([]string, error)
|
||||
// AssetInfo should return the info of file in path if exists
|
||||
AssetInfo func(path string) (os.FileInfo, error)
|
||||
// Prefix would be prepended to http requests
|
||||
Prefix string
|
||||
}
|
||||
|
||||
func (fs *AssetFS) Open(name string) (http.File, error) {
|
||||
name = path.Join(fs.Prefix, name)
|
||||
if len(name) > 0 && name[0] == '/' {
|
||||
name = name[1:]
|
||||
}
|
||||
if b, err := fs.Asset(name); err == nil {
|
||||
timestamp := defaultFileTimestamp
|
||||
if fs.AssetInfo != nil {
|
||||
if info, err := fs.AssetInfo(name); err == nil {
|
||||
timestamp = info.ModTime()
|
||||
}
|
||||
}
|
||||
return NewAssetFile(name, b, timestamp), nil
|
||||
}
|
||||
if children, err := fs.AssetDir(name); err == nil {
|
||||
return NewAssetDirectory(name, children, fs), nil
|
||||
} else {
|
||||
// If the error is not found, return an error that will
|
||||
// result in a 404 error. Otherwise the server returns
|
||||
// a 500 error for files not found.
|
||||
if strings.Contains(err.Error(), "not found") {
|
||||
return nil, os.ErrNotExist
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
// assetfs allows packages to serve static content embedded
|
||||
// with the go-bindata tool with the standard net/http package.
|
||||
//
|
||||
// See https://github.com/jteeuwen/go-bindata for more information
|
||||
// about embedding binary data with go-bindata.
|
||||
//
|
||||
// Usage example, after running
|
||||
// $ go-bindata data/...
|
||||
// use:
|
||||
// http.Handle("/",
|
||||
// http.FileServer(
|
||||
// &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "data"}))
|
||||
package assetfs
|
Loading…
Reference in New Issue