vendor deps for tests
This commit is contained in:
parent
3e0b2965b4
commit
9dcf11d07a
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2015 Dmitri Shuralyov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,21 @@
|
|||
package vfsutil
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// File implements http.FileSystem using the native file system restricted to a
|
||||
// specific file served at root.
|
||||
//
|
||||
// While the FileSystem.Open method takes '/'-separated paths, a File's string
|
||||
// value is a filename on the native file system, not a URL, so it is separated
|
||||
// by filepath.Separator, which isn't necessarily '/'.
|
||||
type File string
|
||||
|
||||
func (f File) Open(name string) (http.File, error) {
|
||||
if name != "/" {
|
||||
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
|
||||
}
|
||||
return os.Open(string(f))
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
// Package vfsutil implements some I/O utility functions for http.FileSystem.
|
||||
package vfsutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// ReadDir reads the contents of the directory associated with file and
|
||||
// returns a slice of FileInfo values in directory order.
|
||||
func ReadDir(fs http.FileSystem, name string) ([]os.FileInfo, error) {
|
||||
f, err := fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return f.Readdir(0)
|
||||
}
|
||||
|
||||
// Stat returns the FileInfo structure describing file.
|
||||
func Stat(fs http.FileSystem, name string) (os.FileInfo, error) {
|
||||
f, err := fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
return f.Stat()
|
||||
}
|
||||
|
||||
// ReadFile reads the file named by path from fs and returns the contents.
|
||||
func ReadFile(fs http.FileSystem, path string) ([]byte, error) {
|
||||
rc, err := fs.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rc.Close()
|
||||
return ioutil.ReadAll(rc)
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
package vfsutil
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Walk walks the filesystem rooted at root, calling walkFn for each file or
|
||||
// directory in the filesystem, including root. All errors that arise visiting files
|
||||
// and directories are filtered by walkFn. The files are walked in lexical
|
||||
// order.
|
||||
func Walk(fs http.FileSystem, root string, walkFn filepath.WalkFunc) error {
|
||||
info, err := Stat(fs, root)
|
||||
if err != nil {
|
||||
return walkFn(root, nil, err)
|
||||
}
|
||||
return walk(fs, root, info, walkFn)
|
||||
}
|
||||
|
||||
// readDirNames reads the directory named by dirname and returns
|
||||
// a sorted list of directory entries.
|
||||
func readDirNames(fs http.FileSystem, dirname string) ([]string, error) {
|
||||
fis, err := ReadDir(fs, dirname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, len(fis))
|
||||
for i := range fis {
|
||||
names[i] = fis[i].Name()
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// walk recursively descends path, calling walkFn.
|
||||
func walk(fs http.FileSystem, path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
|
||||
err := walkFn(path, info, nil)
|
||||
if err != nil {
|
||||
if info.IsDir() && err == filepath.SkipDir {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
names, err := readDirNames(fs, path)
|
||||
if err != nil {
|
||||
return walkFn(path, info, err)
|
||||
}
|
||||
|
||||
for _, name := range names {
|
||||
filename := pathpkg.Join(path, name)
|
||||
fileInfo, err := Stat(fs, filename)
|
||||
if err != nil {
|
||||
if err := walkFn(filename, fileInfo, err); err != nil && err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = walk(fs, filename, fileInfo, walkFn)
|
||||
if err != nil {
|
||||
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WalkFilesFunc is the type of the function called for each file or directory visited by WalkFiles.
|
||||
// It's like filepath.WalkFunc, except it provides an additional ReadSeeker parameter for file being visited.
|
||||
type WalkFilesFunc func(path string, info os.FileInfo, rs io.ReadSeeker, err error) error
|
||||
|
||||
// WalkFiles walks the filesystem rooted at root, calling walkFn for each file or
|
||||
// directory in the filesystem, including root. In addition to FileInfo, it passes an
|
||||
// ReadSeeker to walkFn for each file it visits.
|
||||
func WalkFiles(fs http.FileSystem, root string, walkFn WalkFilesFunc) error {
|
||||
file, info, err := openStat(fs, root)
|
||||
if err != nil {
|
||||
return walkFn(root, nil, nil, err)
|
||||
}
|
||||
return walkFiles(fs, root, info, file, walkFn)
|
||||
}
|
||||
|
||||
// walkFiles recursively descends path, calling walkFn.
|
||||
// It closes the input file after it's done with it, so the caller shouldn't.
|
||||
func walkFiles(fs http.FileSystem, path string, info os.FileInfo, file http.File, walkFn WalkFilesFunc) error {
|
||||
err := walkFn(path, info, file, nil)
|
||||
file.Close()
|
||||
if err != nil {
|
||||
if info.IsDir() && err == filepath.SkipDir {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
names, err := readDirNames(fs, path)
|
||||
if err != nil {
|
||||
return walkFn(path, info, nil, err)
|
||||
}
|
||||
|
||||
for _, name := range names {
|
||||
filename := pathpkg.Join(path, name)
|
||||
file, fileInfo, err := openStat(fs, filename)
|
||||
if err != nil {
|
||||
if err := walkFn(filename, nil, nil, err); err != nil && err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err = walkFiles(fs, filename, fileInfo, file, walkFn)
|
||||
// file is closed by walkFiles, so we don't need to close it here.
|
||||
if err != nil {
|
||||
if !fileInfo.IsDir() || err != filepath.SkipDir {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// openStat performs Open and Stat and returns results, or first error encountered.
|
||||
// The caller is responsible for closing the returned file when done.
|
||||
func openStat(fs http.FileSystem, name string) (http.File, os.FileInfo, error) {
|
||||
f, err := fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
fi, err := f.Stat()
|
||||
if err != nil {
|
||||
f.Close()
|
||||
return nil, nil, err
|
||||
}
|
||||
return f, fi, nil
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
sudo: false
|
||||
language: go
|
||||
go:
|
||||
- 1.x
|
||||
- master
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: master
|
||||
fast_finish: true
|
||||
install:
|
||||
- # Do nothing. This is needed to prevent default install action "go get -t -v ./..." from happening here (we want it to happen inside script step).
|
||||
script:
|
||||
- go get -t -v ./...
|
||||
- diff -n <(echo -n) <(gofmt -d -s .)
|
||||
- go vet ./...
|
||||
- go test -v -race ./...
|
|
@ -0,0 +1,10 @@
|
|||
Contributing
|
||||
============
|
||||
|
||||
vfsgen is open source, thanks for considering contributing!
|
||||
|
||||
Please note that vfsgen aims to be simple and minimalistic, with as little to configure as possible. If you'd like to remove or simplify code (while having tests continue to pass), fix bugs, or improve code (e.g., add missing error checking, etc.), PRs and issues are welcome.
|
||||
|
||||
However, if you'd like to add new functionality that increases complexity or scope, please make an issue and discuss your proposal first. I'm unlikely to accept such changes outright. It might be that your request is already a part of other similar packages, or it might fit in their scope better. See [Comparison and Alternatives](https://github.com/shurcooL/vfsgen/tree/README-alternatives-and-comparison-section#comparison) sections.
|
||||
|
||||
Thank you!
|
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2015 Dmitri Shuralyov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,201 @@
|
|||
vfsgen
|
||||
======
|
||||
|
||||
[![Build Status](https://travis-ci.org/shurcooL/vfsgen.svg?branch=master)](https://travis-ci.org/shurcooL/vfsgen) [![GoDoc](https://godoc.org/github.com/shurcooL/vfsgen?status.svg)](https://godoc.org/github.com/shurcooL/vfsgen)
|
||||
|
||||
Package vfsgen takes an http.FileSystem (likely at `go generate` time) and
|
||||
generates Go code that statically implements the provided http.FileSystem.
|
||||
|
||||
Features:
|
||||
|
||||
- Efficient generated code without unneccessary overhead.
|
||||
|
||||
- Uses gzip compression internally (selectively, only for files that compress well).
|
||||
|
||||
- Enables direct access to internal gzip compressed bytes via an optional interface.
|
||||
|
||||
- Outputs `gofmt`ed Go code.
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
```bash
|
||||
go get -u github.com/shurcooL/vfsgen
|
||||
```
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
Package `vfsgen` is a Go code generator library. It has a `Generate` function that takes an input filesystem (as a [`http.FileSystem`](https://godoc.org/net/http#FileSystem) type), and generates a Go code file that statically implements the contents of the input filesystem.
|
||||
|
||||
For example, we can use [`http.Dir`](https://godoc.org/net/http#Dir) as a `http.FileSystem` implementation that uses the contents of the `/path/to/assets` directory:
|
||||
|
||||
```Go
|
||||
var fs http.FileSystem = http.Dir("/path/to/assets")
|
||||
```
|
||||
|
||||
Now, when you execute the following code:
|
||||
|
||||
```Go
|
||||
err := vfsgen.Generate(fs, vfsgen.Options{})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
```
|
||||
|
||||
An assets_vfsdata.go file will be generated in the current directory:
|
||||
|
||||
```Go
|
||||
// Code generated by vfsgen; DO NOT EDIT.
|
||||
|
||||
package main
|
||||
|
||||
import ...
|
||||
|
||||
// assets statically implements the virtual filesystem provided to vfsgen.Generate.
|
||||
var assets http.FileSystem = ...
|
||||
```
|
||||
|
||||
Then, in your program, you can use `assets` as any other [`http.FileSystem`](https://godoc.org/net/http#FileSystem), for example:
|
||||
|
||||
```Go
|
||||
file, err := assets.Open("/some/file.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
```
|
||||
|
||||
```Go
|
||||
http.Handle("/assets/", http.FileServer(assets))
|
||||
```
|
||||
|
||||
`vfsgen` can be more useful when combined with build tags and go generate directives. This is described below.
|
||||
|
||||
### `go generate` Usage
|
||||
|
||||
vfsgen is great to use with go generate directives. The code invoking `vfsgen.Generate` can go in an assets_generate.go file, which can then be invoked via "//go:generate go run assets_generate.go". The input virtual filesystem can read directly from disk, or it can be more involved.
|
||||
|
||||
By using build tags, you can create a development mode where assets are loaded directly from disk via `http.Dir`, but then statically implemented for final releases.
|
||||
|
||||
For example, suppose your source filesystem is defined in a package with import path "example.com/project/data" as:
|
||||
|
||||
```Go
|
||||
// +build dev
|
||||
|
||||
package data
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Assets contains project assets.
|
||||
var Assets http.FileSystem = http.Dir("assets")
|
||||
```
|
||||
|
||||
When built with the "dev" build tag, accessing `data.Assets` will read from disk directly via `http.Dir`.
|
||||
|
||||
A generate helper file assets_generate.go can be invoked via "//go:generate go run -tags=dev assets_generate.go" directive:
|
||||
|
||||
```Go
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"example.com/project/data"
|
||||
"github.com/shurcooL/vfsgen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := vfsgen.Generate(data.Assets, vfsgen.Options{
|
||||
PackageName: "data",
|
||||
BuildTags: "!dev",
|
||||
VariableName: "Assets",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Note that "dev" build tag is used to access the source filesystem, and the output file will contain "!dev" build tag. That way, the statically implemented version will be used during normal builds and `go get`, when custom builds tags are not specified.
|
||||
|
||||
### `vfsgendev` Usage
|
||||
|
||||
`vfsgendev` is a binary that can be used to replace the need for the assets_generate.go file.
|
||||
|
||||
Make sure it's installed and available in your PATH.
|
||||
|
||||
```bash
|
||||
go get -u github.com/shurcooL/vfsgen/cmd/vfsgendev
|
||||
```
|
||||
|
||||
Then the "//go:generate go run -tags=dev assets_generate.go" directive can be replaced with:
|
||||
|
||||
```
|
||||
//go:generate vfsgendev -source="example.com/project/data".Assets
|
||||
```
|
||||
|
||||
vfsgendev accesses the source variable using "dev" build tag, and generates an output file with "!dev" build tag.
|
||||
|
||||
### Additional Embedded Information
|
||||
|
||||
All compressed files implement [`httpgzip.GzipByter` interface](https://godoc.org/github.com/shurcooL/httpgzip#GzipByter) for efficient direct access to the internal compressed bytes:
|
||||
|
||||
```Go
|
||||
// GzipByter is implemented by compressed files for
|
||||
// efficient direct access to the internal compressed bytes.
|
||||
type GzipByter interface {
|
||||
// GzipBytes returns gzip compressed contents of the file.
|
||||
GzipBytes() []byte
|
||||
}
|
||||
```
|
||||
|
||||
Files that have been determined to not be worth gzip compressing (their compressed size is larger than original) implement [`httpgzip.NotWorthGzipCompressing` interface](https://godoc.org/github.com/shurcooL/httpgzip#NotWorthGzipCompressing):
|
||||
|
||||
```Go
|
||||
// NotWorthGzipCompressing is implemented by files that were determined
|
||||
// not to be worth gzip compressing (the file size did not decrease as a result).
|
||||
type NotWorthGzipCompressing interface {
|
||||
// NotWorthGzipCompressing is a noop. It's implemented in order to indicate
|
||||
// the file is not worth gzip compressing.
|
||||
NotWorthGzipCompressing()
|
||||
}
|
||||
```
|
||||
|
||||
Comparison
|
||||
----------
|
||||
|
||||
vfsgen aims to be conceptually simple to use. The [`http.FileSystem`](https://godoc.org/net/http#FileSystem) abstraction is central to vfsgen. It's used as both input for code generation, and as output in the generated code.
|
||||
|
||||
That enables great flexibility through orthogonality, since helpers and wrappers can operate on `http.FileSystem` without knowing about vfsgen. If you want, you can perform pre-processing, minifying assets, merging folders, filtering out files and otherwise modifying input via generic `http.FileSystem` middleware.
|
||||
|
||||
It avoids unneccessary overhead by merging what was previously done with two distinct packages into a single package.
|
||||
|
||||
It strives to be the best in its class in terms of code quality and efficiency of generated code. However, if your use goals are different, there are other similar packages that may fit your needs better.
|
||||
|
||||
### Alternatives
|
||||
|
||||
- [`go-bindata`](https://github.com/jteeuwen/go-bindata) - Reads from disk, generates Go code that provides access to data via a [custom API](https://github.com/jteeuwen/go-bindata#accessing-an-asset).
|
||||
- [`go-bindata-assetfs`](https://github.com/elazarl/go-bindata-assetfs) - Takes output of go-bindata and provides a wrapper that implements `http.FileSystem` interface (the same as what vfsgen outputs directly).
|
||||
- [`becky`](https://github.com/tv42/becky) - Embeds assets as string literals in Go source.
|
||||
- [`statik`](https://github.com/rakyll/statik) - Embeds a directory of static files to be accessed via `http.FileSystem` interface (sounds very similar to vfsgen); implementation sourced from [camlistore](https://camlistore.org).
|
||||
- [`go.rice`](https://github.com/GeertJohan/go.rice) - Makes working with resources such as HTML, JS, CSS, images and templates very easy.
|
||||
- [`esc`](https://github.com/mjibson/esc) - Embeds files into Go programs and provides `http.FileSystem` interfaces to them.
|
||||
- [`staticfiles`](https://github.com/bouk/staticfiles) - Allows you to embed a directory of files into your Go binary.
|
||||
- [`togo`](https://github.com/flazz/togo) - Generates a Go source file with a `[]byte` var containing the given file's contents.
|
||||
- [`fileb0x`](https://github.com/UnnoTed/fileb0x) - Simple customizable tool to embed files in Go.
|
||||
- [`embedfiles`](https://github.com/leighmcculloch/embedfiles) - Simple tool for embedding files in Go code as a map.
|
||||
- [`packr`](https://github.com/gobuffalo/packr) - Simple solution for bundling static assets inside of Go binaries.
|
||||
- [`rsrc`](https://github.com/akavel/rsrc) - Tool for embedding .ico & manifest resources in Go programs for Windows.
|
||||
|
||||
Attribution
|
||||
-----------
|
||||
|
||||
This package was originally based on the excellent work by [@jteeuwen](https://github.com/jteeuwen) on [`go-bindata`](https://github.com/jteeuwen/go-bindata) and [@elazarl](https://github.com/elazarl) on [`go-bindata-assetfs`](https://github.com/elazarl/go-bindata-assetfs).
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
- [MIT License](LICENSE)
|
|
@ -0,0 +1,39 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type data struct {
|
||||
ImportPath string
|
||||
PackageName string
|
||||
BuildTags string
|
||||
VariableName string
|
||||
VariableComment string
|
||||
}
|
||||
|
||||
var generateTemplate = template.Must(template.New("").Funcs(template.FuncMap{
|
||||
"quote": strconv.Quote,
|
||||
}).Parse(`package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/shurcooL/vfsgen"
|
||||
|
||||
sourcepkg {{.ImportPath | quote}}
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := vfsgen.Generate(sourcepkg.{{.VariableName}}, vfsgen.Options{
|
||||
PackageName: {{.PackageName | quote}},
|
||||
BuildTags: {{.BuildTags | quote}},
|
||||
VariableName: {{.VariableName | quote}},
|
||||
VariableComment: {{.VariableComment | quote}},
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
`))
|
|
@ -0,0 +1,111 @@
|
|||
// vfsgendev is a convenience tool for using vfsgen in a common development configuration.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/build"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
var (
|
||||
sourceFlag = flag.String("source", "", "Specifies the http.FileSystem variable to use as source.")
|
||||
tagFlag = flag.String("tag", "dev", "Specifies a single build tag to use for source. The output will include a negated version.")
|
||||
nFlag = flag.Bool("n", false, "Print the generated source but do not run it.")
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Fprintln(os.Stderr, `Usage: vfsgendev [flags] -source="import/path".VariableName`)
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
if flag.NArg() != 0 {
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
importPath, variableName, err := parseSourceFlag(*sourceFlag)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "-source flag has invalid value:", err)
|
||||
fmt.Fprintln(os.Stderr)
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
tag, err := parseTagFlag(*tagFlag)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "-tag flag has invalid value:", err)
|
||||
fmt.Fprintln(os.Stderr)
|
||||
flag.Usage()
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
err = run(importPath, variableName, tag)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(importPath, variableName, tag string) error {
|
||||
bctx := build.Default
|
||||
bctx.BuildTags = []string{tag}
|
||||
packageName, variableComment, err := lookupNameAndComment(bctx, importPath, variableName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = generateTemplate.Execute(&buf, data{
|
||||
ImportPath: importPath,
|
||||
PackageName: packageName,
|
||||
BuildTags: "!" + tag,
|
||||
VariableName: variableName,
|
||||
VariableComment: variableComment,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *nFlag {
|
||||
io.Copy(os.Stdout, &buf)
|
||||
return nil
|
||||
}
|
||||
|
||||
err = goRun(buf.String(), tag)
|
||||
return err
|
||||
}
|
||||
|
||||
// goRun runs Go code src with build tags.
|
||||
func goRun(src string, tags string) error {
|
||||
// Create a temp folder.
|
||||
tempDir, err := ioutil.TempDir("", "vfsgendev_")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(tempDir)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "warning: error removing temp dir:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Write the source code file.
|
||||
tempFile := filepath.Join(tempDir, "generate.go")
|
||||
err = ioutil.WriteFile(tempFile, []byte(src), 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Compile and run the program.
|
||||
cmd := exec.Command("go", "run", "-tags="+tags, tempFile)
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
return cmd.Run()
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/build"
|
||||
"go/doc"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// parseSourceFlag parses the "-source" flag value. It must have "import/path".VariableName format.
|
||||
// It returns an error if the parsed import path is relative.
|
||||
func parseSourceFlag(sourceFlag string) (importPath, variableName string, err error) {
|
||||
// Parse sourceFlag as a Go expression, albeit a strange one:
|
||||
//
|
||||
// "import/path".VariableName
|
||||
//
|
||||
e, err := parser.ParseExpr(sourceFlag)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("invalid format, failed to parse %q as a Go expression", sourceFlag)
|
||||
}
|
||||
se, ok := e.(*ast.SelectorExpr)
|
||||
if !ok {
|
||||
return "", "", fmt.Errorf("invalid format, expression %v is not a selector expression but %T", sourceFlag, e)
|
||||
}
|
||||
importPath, err = stringValue(se.X)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("invalid format, expression %v is not a properly quoted Go string: %v", stringifyAST(se.X), err)
|
||||
}
|
||||
if build.IsLocalImport(importPath) {
|
||||
// Generated code is executed in a temporary directory,
|
||||
// and can't use relative import paths. So disallow them.
|
||||
return "", "", fmt.Errorf("relative import paths are not supported")
|
||||
}
|
||||
variableName = se.Sel.Name
|
||||
return importPath, variableName, nil
|
||||
}
|
||||
|
||||
// stringValue returns the string value of string literal e.
|
||||
func stringValue(e ast.Expr) (string, error) {
|
||||
lit, ok := e.(*ast.BasicLit)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("not a string, but %T", e)
|
||||
}
|
||||
if lit.Kind != token.STRING {
|
||||
return "", fmt.Errorf("not a string, but %v", lit.Kind)
|
||||
}
|
||||
return strconv.Unquote(lit.Value)
|
||||
}
|
||||
|
||||
// parseTagFlag parses the "-tag" flag value. It must be a single build tag.
|
||||
func parseTagFlag(tagFlag string) (tag string, err error) {
|
||||
tags := strings.Fields(tagFlag)
|
||||
if len(tags) != 1 {
|
||||
return "", fmt.Errorf("%q is not a valid single build tag, but %q", tagFlag, tags)
|
||||
}
|
||||
return tags[0], nil
|
||||
}
|
||||
|
||||
// lookupNameAndComment imports package using provided build context, and
|
||||
// returns the package name and variable comment.
|
||||
func lookupNameAndComment(bctx build.Context, importPath, variableName string) (packageName, variableComment string, err error) {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
bpkg, err := bctx.Import(importPath, wd, 0)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("can't import package %q: %v", importPath, err)
|
||||
}
|
||||
dpkg, err := computeDoc(bpkg)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("can't get godoc of package %q: %v", importPath, err)
|
||||
}
|
||||
for _, v := range dpkg.Vars {
|
||||
if len(v.Names) == 1 && v.Names[0] == variableName {
|
||||
variableComment = strings.TrimSuffix(v.Doc, "\n")
|
||||
break
|
||||
}
|
||||
}
|
||||
return bpkg.Name, variableComment, nil
|
||||
}
|
||||
|
||||
func stringifyAST(node interface{}) string {
|
||||
var buf bytes.Buffer
|
||||
err := printer.Fprint(&buf, token.NewFileSet(), node)
|
||||
if err != nil {
|
||||
return "printer.Fprint error: " + err.Error()
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// computeDoc computes the package documentation for the given package.
|
||||
func computeDoc(bpkg *build.Package) (*doc.Package, error) {
|
||||
fset := token.NewFileSet()
|
||||
files := make(map[string]*ast.File)
|
||||
for _, file := range append(bpkg.GoFiles, bpkg.CgoFiles...) {
|
||||
f, err := parser.ParseFile(fset, filepath.Join(bpkg.Dir, file), nil, parser.ParseComments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files[file] = f
|
||||
}
|
||||
apkg := &ast.Package{
|
||||
Name: bpkg.Name,
|
||||
Files: files,
|
||||
}
|
||||
return doc.New(apkg, bpkg.ImportPath, 0), nil
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package vfsgen
|
||||
|
||||
import "io"
|
||||
|
||||
// commentWriter writes a Go comment to the underlying io.Writer,
|
||||
// using line comment form (//).
|
||||
type commentWriter struct {
|
||||
W io.Writer
|
||||
wroteSlashes bool // Wrote "//" at the beginning of the current line.
|
||||
}
|
||||
|
||||
func (c *commentWriter) Write(p []byte) (int, error) {
|
||||
var n int
|
||||
for i, b := range p {
|
||||
if !c.wroteSlashes {
|
||||
s := "//"
|
||||
if b != '\n' {
|
||||
s = "// "
|
||||
}
|
||||
if _, err := io.WriteString(c.W, s); err != nil {
|
||||
return n, err
|
||||
}
|
||||
c.wroteSlashes = true
|
||||
}
|
||||
n0, err := c.W.Write(p[i : i+1])
|
||||
n += n0
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
if b == '\n' {
|
||||
c.wroteSlashes = false
|
||||
}
|
||||
}
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (c *commentWriter) Close() error {
|
||||
if !c.wroteSlashes {
|
||||
if _, err := io.WriteString(c.W, "//"); err != nil {
|
||||
return err
|
||||
}
|
||||
c.wroteSlashes = true
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
Package vfsgen takes an http.FileSystem (likely at `go generate` time) and
|
||||
generates Go code that statically implements the provided http.FileSystem.
|
||||
|
||||
Features:
|
||||
|
||||
- Efficient generated code without unneccessary overhead.
|
||||
|
||||
- Uses gzip compression internally (selectively, only for files that compress well).
|
||||
|
||||
- Enables direct access to internal gzip compressed bytes via an optional interface.
|
||||
|
||||
- Outputs `gofmt`ed Go code.
|
||||
*/
|
||||
package vfsgen
|
|
@ -0,0 +1,483 @@
|
|||
package vfsgen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"sort"
|
||||
"strconv"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/shurcooL/httpfs/vfsutil"
|
||||
)
|
||||
|
||||
// Generate Go code that statically implements input filesystem,
|
||||
// write the output to a file specified in opt.
|
||||
func Generate(input http.FileSystem, opt Options) error {
|
||||
opt.fillMissing()
|
||||
|
||||
// Use an in-memory buffer to generate the entire output.
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
err := t.ExecuteTemplate(buf, "Header", opt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var toc toc
|
||||
err = findAndWriteFiles(buf, input, &toc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = t.ExecuteTemplate(buf, "DirEntries", toc.dirs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = t.ExecuteTemplate(buf, "Trailer", toc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write output file (all at once).
|
||||
err = ioutil.WriteFile(opt.Filename, buf.Bytes(), 0644)
|
||||
return err
|
||||
}
|
||||
|
||||
type toc struct {
|
||||
dirs []*dirInfo
|
||||
|
||||
HasCompressedFile bool // There's at least one compressedFile.
|
||||
HasFile bool // There's at least one uncompressed file.
|
||||
}
|
||||
|
||||
// fileInfo is a definition of a file.
|
||||
type fileInfo struct {
|
||||
Path string
|
||||
Name string
|
||||
ModTime time.Time
|
||||
UncompressedSize int64
|
||||
}
|
||||
|
||||
// dirInfo is a definition of a directory.
|
||||
type dirInfo struct {
|
||||
Path string
|
||||
Name string
|
||||
ModTime time.Time
|
||||
Entries []string
|
||||
}
|
||||
|
||||
// findAndWriteFiles recursively finds all the file paths in the given directory tree.
|
||||
// They are added to the given map as keys. Values will be safe function names
|
||||
// for each file, which will be used when generating the output code.
|
||||
func findAndWriteFiles(buf *bytes.Buffer, fs http.FileSystem, toc *toc) error {
|
||||
walkFn := func(path string, fi os.FileInfo, r io.ReadSeeker, err error) error {
|
||||
if err != nil {
|
||||
// Consider all errors reading the input filesystem as fatal.
|
||||
return err
|
||||
}
|
||||
|
||||
switch fi.IsDir() {
|
||||
case false:
|
||||
file := &fileInfo{
|
||||
Path: path,
|
||||
Name: pathpkg.Base(path),
|
||||
ModTime: fi.ModTime().UTC(),
|
||||
UncompressedSize: fi.Size(),
|
||||
}
|
||||
|
||||
marker := buf.Len()
|
||||
|
||||
// Write CompressedFileInfo.
|
||||
err = writeCompressedFileInfo(buf, file, r)
|
||||
switch err {
|
||||
default:
|
||||
return err
|
||||
case nil:
|
||||
toc.HasCompressedFile = true
|
||||
// If compressed file is not smaller than original, revert and write original file.
|
||||
case errCompressedNotSmaller:
|
||||
_, err = r.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buf.Truncate(marker)
|
||||
|
||||
// Write FileInfo.
|
||||
err = writeFileInfo(buf, file, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
toc.HasFile = true
|
||||
}
|
||||
case true:
|
||||
entries, err := readDirPaths(fs, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dir := &dirInfo{
|
||||
Path: path,
|
||||
Name: pathpkg.Base(path),
|
||||
ModTime: fi.ModTime().UTC(),
|
||||
Entries: entries,
|
||||
}
|
||||
|
||||
toc.dirs = append(toc.dirs, dir)
|
||||
|
||||
// Write DirInfo.
|
||||
err = t.ExecuteTemplate(buf, "DirInfo", dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
err := vfsutil.WalkFiles(fs, "/", walkFn)
|
||||
return err
|
||||
}
|
||||
|
||||
// readDirPaths reads the directory named by dirname and returns
|
||||
// a sorted list of directory paths.
|
||||
func readDirPaths(fs http.FileSystem, dirname string) ([]string, error) {
|
||||
fis, err := vfsutil.ReadDir(fs, dirname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
paths := make([]string, len(fis))
|
||||
for i := range fis {
|
||||
paths[i] = pathpkg.Join(dirname, fis[i].Name())
|
||||
}
|
||||
sort.Strings(paths)
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
// writeCompressedFileInfo writes CompressedFileInfo.
|
||||
// It returns errCompressedNotSmaller if compressed file is not smaller than original.
|
||||
func writeCompressedFileInfo(w io.Writer, file *fileInfo, r io.Reader) error {
|
||||
err := t.ExecuteTemplate(w, "CompressedFileInfo-Before", file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sw := &stringWriter{Writer: w}
|
||||
gw, _ := gzip.NewWriterLevel(sw, gzip.BestCompression)
|
||||
_, err = io.Copy(gw, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = gw.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if sw.N >= file.UncompressedSize {
|
||||
return errCompressedNotSmaller
|
||||
}
|
||||
err = t.ExecuteTemplate(w, "CompressedFileInfo-After", file)
|
||||
return err
|
||||
}
|
||||
|
||||
var errCompressedNotSmaller = errors.New("compressed file is not smaller than original")
|
||||
|
||||
// Write FileInfo.
|
||||
func writeFileInfo(w io.Writer, file *fileInfo, r io.Reader) error {
|
||||
err := t.ExecuteTemplate(w, "FileInfo-Before", file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sw := &stringWriter{Writer: w}
|
||||
_, err = io.Copy(sw, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = t.ExecuteTemplate(w, "FileInfo-After", file)
|
||||
return err
|
||||
}
|
||||
|
||||
var t = template.Must(template.New("").Funcs(template.FuncMap{
|
||||
"quote": strconv.Quote,
|
||||
"comment": func(s string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
cw := &commentWriter{W: &buf}
|
||||
_, err := io.WriteString(cw, s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = cw.Close()
|
||||
return buf.String(), err
|
||||
},
|
||||
}).Parse(`{{define "Header"}}// Code generated by vfsgen; DO NOT EDIT.
|
||||
|
||||
{{with .BuildTags}}// +build {{.}}
|
||||
|
||||
{{end}}package {{.PackageName}}
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"time"
|
||||
)
|
||||
|
||||
{{comment .VariableComment}}
|
||||
var {{.VariableName}} = func() http.FileSystem {
|
||||
fs := vfsgen۰FS{
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "CompressedFileInfo-Before"}} {{quote .Path}}: &vfsgen۰CompressedFileInfo{
|
||||
name: {{quote .Name}},
|
||||
modTime: {{template "Time" .ModTime}},
|
||||
uncompressedSize: {{.UncompressedSize}},
|
||||
{{/* This blank line separating compressedContent is neccessary to prevent potential gofmt issues. See issue #19. */}}
|
||||
compressedContent: []byte("{{end}}{{define "CompressedFileInfo-After"}}"),
|
||||
},
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "FileInfo-Before"}} {{quote .Path}}: &vfsgen۰FileInfo{
|
||||
name: {{quote .Name}},
|
||||
modTime: {{template "Time" .ModTime}},
|
||||
content: []byte("{{end}}{{define "FileInfo-After"}}"),
|
||||
},
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "DirInfo"}} {{quote .Path}}: &vfsgen۰DirInfo{
|
||||
name: {{quote .Name}},
|
||||
modTime: {{template "Time" .ModTime}},
|
||||
},
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "DirEntries"}} }
|
||||
{{range .}}{{if .Entries}} fs[{{quote .Path}}].(*vfsgen۰DirInfo).entries = []os.FileInfo{{"{"}}{{range .Entries}}
|
||||
fs[{{quote .}}].(os.FileInfo),{{end}}
|
||||
}
|
||||
{{end}}{{end}}
|
||||
return fs
|
||||
}()
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "Trailer"}}
|
||||
type vfsgen۰FS map[string]interface{}
|
||||
|
||||
func (fs vfsgen۰FS) Open(path string) (http.File, error) {
|
||||
path = pathpkg.Clean("/" + path)
|
||||
f, ok := fs[path]
|
||||
if !ok {
|
||||
return nil, &os.PathError{Op: "open", Path: path, Err: os.ErrNotExist}
|
||||
}
|
||||
|
||||
switch f := f.(type) {{"{"}}{{if .HasCompressedFile}}
|
||||
case *vfsgen۰CompressedFileInfo:
|
||||
gr, err := gzip.NewReader(bytes.NewReader(f.compressedContent))
|
||||
if err != nil {
|
||||
// This should never happen because we generate the gzip bytes such that they are always valid.
|
||||
panic("unexpected error reading own gzip compressed bytes: " + err.Error())
|
||||
}
|
||||
return &vfsgen۰CompressedFile{
|
||||
vfsgen۰CompressedFileInfo: f,
|
||||
gr: gr,
|
||||
}, nil{{end}}{{if .HasFile}}
|
||||
case *vfsgen۰FileInfo:
|
||||
return &vfsgen۰File{
|
||||
vfsgen۰FileInfo: f,
|
||||
Reader: bytes.NewReader(f.content),
|
||||
}, nil{{end}}
|
||||
case *vfsgen۰DirInfo:
|
||||
return &vfsgen۰Dir{
|
||||
vfsgen۰DirInfo: f,
|
||||
}, nil
|
||||
default:
|
||||
// This should never happen because we generate only the above types.
|
||||
panic(fmt.Sprintf("unexpected type %T", f))
|
||||
}
|
||||
}
|
||||
{{if .HasCompressedFile}}
|
||||
// vfsgen۰CompressedFileInfo is a static definition of a gzip compressed file.
|
||||
type vfsgen۰CompressedFileInfo struct {
|
||||
name string
|
||||
modTime time.Time
|
||||
compressedContent []byte
|
||||
uncompressedSize int64
|
||||
}
|
||||
|
||||
func (f *vfsgen۰CompressedFileInfo) Readdir(count int) ([]os.FileInfo, error) {
|
||||
return nil, fmt.Errorf("cannot Readdir from file %s", f.name)
|
||||
}
|
||||
func (f *vfsgen۰CompressedFileInfo) Stat() (os.FileInfo, error) { return f, nil }
|
||||
|
||||
func (f *vfsgen۰CompressedFileInfo) GzipBytes() []byte {
|
||||
return f.compressedContent
|
||||
}
|
||||
|
||||
func (f *vfsgen۰CompressedFileInfo) Name() string { return f.name }
|
||||
func (f *vfsgen۰CompressedFileInfo) Size() int64 { return f.uncompressedSize }
|
||||
func (f *vfsgen۰CompressedFileInfo) Mode() os.FileMode { return 0444 }
|
||||
func (f *vfsgen۰CompressedFileInfo) ModTime() time.Time { return f.modTime }
|
||||
func (f *vfsgen۰CompressedFileInfo) IsDir() bool { return false }
|
||||
func (f *vfsgen۰CompressedFileInfo) Sys() interface{} { return nil }
|
||||
|
||||
// vfsgen۰CompressedFile is an opened compressedFile instance.
|
||||
type vfsgen۰CompressedFile struct {
|
||||
*vfsgen۰CompressedFileInfo
|
||||
gr *gzip.Reader
|
||||
grPos int64 // Actual gr uncompressed position.
|
||||
seekPos int64 // Seek uncompressed position.
|
||||
}
|
||||
|
||||
func (f *vfsgen۰CompressedFile) Read(p []byte) (n int, err error) {
|
||||
if f.grPos > f.seekPos {
|
||||
// Rewind to beginning.
|
||||
err = f.gr.Reset(bytes.NewReader(f.compressedContent))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
f.grPos = 0
|
||||
}
|
||||
if f.grPos < f.seekPos {
|
||||
// Fast-forward.
|
||||
_, err = io.CopyN(ioutil.Discard, f.gr, f.seekPos-f.grPos)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
f.grPos = f.seekPos
|
||||
}
|
||||
n, err = f.gr.Read(p)
|
||||
f.grPos += int64(n)
|
||||
f.seekPos = f.grPos
|
||||
return n, err
|
||||
}
|
||||
func (f *vfsgen۰CompressedFile) Seek(offset int64, whence int) (int64, error) {
|
||||
switch whence {
|
||||
case io.SeekStart:
|
||||
f.seekPos = 0 + offset
|
||||
case io.SeekCurrent:
|
||||
f.seekPos += offset
|
||||
case io.SeekEnd:
|
||||
f.seekPos = f.uncompressedSize + offset
|
||||
default:
|
||||
panic(fmt.Errorf("invalid whence value: %v", whence))
|
||||
}
|
||||
return f.seekPos, nil
|
||||
}
|
||||
func (f *vfsgen۰CompressedFile) Close() error {
|
||||
return f.gr.Close()
|
||||
}
|
||||
{{else}}
|
||||
// We already imported "compress/gzip" and "io/ioutil", but ended up not using them. Avoid unused import error.
|
||||
var _ = gzip.Reader{}
|
||||
var _ = ioutil.Discard
|
||||
{{end}}{{if .HasFile}}
|
||||
// vfsgen۰FileInfo is a static definition of an uncompressed file (because it's not worth gzip compressing).
|
||||
type vfsgen۰FileInfo struct {
|
||||
name string
|
||||
modTime time.Time
|
||||
content []byte
|
||||
}
|
||||
|
||||
func (f *vfsgen۰FileInfo) Readdir(count int) ([]os.FileInfo, error) {
|
||||
return nil, fmt.Errorf("cannot Readdir from file %s", f.name)
|
||||
}
|
||||
func (f *vfsgen۰FileInfo) Stat() (os.FileInfo, error) { return f, nil }
|
||||
|
||||
func (f *vfsgen۰FileInfo) NotWorthGzipCompressing() {}
|
||||
|
||||
func (f *vfsgen۰FileInfo) Name() string { return f.name }
|
||||
func (f *vfsgen۰FileInfo) Size() int64 { return int64(len(f.content)) }
|
||||
func (f *vfsgen۰FileInfo) Mode() os.FileMode { return 0444 }
|
||||
func (f *vfsgen۰FileInfo) ModTime() time.Time { return f.modTime }
|
||||
func (f *vfsgen۰FileInfo) IsDir() bool { return false }
|
||||
func (f *vfsgen۰FileInfo) Sys() interface{} { return nil }
|
||||
|
||||
// vfsgen۰File is an opened file instance.
|
||||
type vfsgen۰File struct {
|
||||
*vfsgen۰FileInfo
|
||||
*bytes.Reader
|
||||
}
|
||||
|
||||
func (f *vfsgen۰File) Close() error {
|
||||
return nil
|
||||
}
|
||||
{{else if not .HasCompressedFile}}
|
||||
// We already imported "bytes", but ended up not using it. Avoid unused import error.
|
||||
var _ = bytes.Reader{}
|
||||
{{end}}
|
||||
// vfsgen۰DirInfo is a static definition of a directory.
|
||||
type vfsgen۰DirInfo struct {
|
||||
name string
|
||||
modTime time.Time
|
||||
entries []os.FileInfo
|
||||
}
|
||||
|
||||
func (d *vfsgen۰DirInfo) Read([]byte) (int, error) {
|
||||
return 0, fmt.Errorf("cannot Read from directory %s", d.name)
|
||||
}
|
||||
func (d *vfsgen۰DirInfo) Close() error { return nil }
|
||||
func (d *vfsgen۰DirInfo) Stat() (os.FileInfo, error) { return d, nil }
|
||||
|
||||
func (d *vfsgen۰DirInfo) Name() string { return d.name }
|
||||
func (d *vfsgen۰DirInfo) Size() int64 { return 0 }
|
||||
func (d *vfsgen۰DirInfo) Mode() os.FileMode { return 0755 | os.ModeDir }
|
||||
func (d *vfsgen۰DirInfo) ModTime() time.Time { return d.modTime }
|
||||
func (d *vfsgen۰DirInfo) IsDir() bool { return true }
|
||||
func (d *vfsgen۰DirInfo) Sys() interface{} { return nil }
|
||||
|
||||
// vfsgen۰Dir is an opened dir instance.
|
||||
type vfsgen۰Dir struct {
|
||||
*vfsgen۰DirInfo
|
||||
pos int // Position within entries for Seek and Readdir.
|
||||
}
|
||||
|
||||
func (d *vfsgen۰Dir) Seek(offset int64, whence int) (int64, error) {
|
||||
if offset == 0 && whence == io.SeekStart {
|
||||
d.pos = 0
|
||||
return 0, nil
|
||||
}
|
||||
return 0, fmt.Errorf("unsupported Seek in directory %s", d.name)
|
||||
}
|
||||
|
||||
func (d *vfsgen۰Dir) Readdir(count int) ([]os.FileInfo, error) {
|
||||
if d.pos >= len(d.entries) && count > 0 {
|
||||
return nil, io.EOF
|
||||
}
|
||||
if count <= 0 || count > len(d.entries)-d.pos {
|
||||
count = len(d.entries) - d.pos
|
||||
}
|
||||
e := d.entries[d.pos : d.pos+count]
|
||||
d.pos += count
|
||||
return e, nil
|
||||
}
|
||||
{{end}}
|
||||
|
||||
|
||||
|
||||
{{define "Time"}}
|
||||
{{- if .IsZero -}}
|
||||
time.Time{}
|
||||
{{- else -}}
|
||||
time.Date({{.Year}}, {{printf "%d" .Month}}, {{.Day}}, {{.Hour}}, {{.Minute}}, {{.Second}}, {{.Nanosecond}}, time.UTC)
|
||||
{{- end -}}
|
||||
{{end}}
|
||||
`))
|
|
@ -0,0 +1,45 @@
|
|||
package vfsgen
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Options for vfsgen code generation.
|
||||
type Options struct {
|
||||
// Filename of the generated Go code output (including extension).
|
||||
// If left empty, it defaults to "{{toLower .VariableName}}_vfsdata.go".
|
||||
Filename string
|
||||
|
||||
// PackageName is the name of the package in the generated code.
|
||||
// If left empty, it defaults to "main".
|
||||
PackageName string
|
||||
|
||||
// BuildTags are the optional build tags in the generated code.
|
||||
// The build tags syntax is specified by the go tool.
|
||||
BuildTags string
|
||||
|
||||
// VariableName is the name of the http.FileSystem variable in the generated code.
|
||||
// If left empty, it defaults to "assets".
|
||||
VariableName string
|
||||
|
||||
// VariableComment is the comment of the http.FileSystem variable in the generated code.
|
||||
// If left empty, it defaults to "{{.VariableName}} statically implements the virtual filesystem provided to vfsgen.".
|
||||
VariableComment string
|
||||
}
|
||||
|
||||
// fillMissing sets default values for mandatory options that are left empty.
|
||||
func (opt *Options) fillMissing() {
|
||||
if opt.PackageName == "" {
|
||||
opt.PackageName = "main"
|
||||
}
|
||||
if opt.VariableName == "" {
|
||||
opt.VariableName = "assets"
|
||||
}
|
||||
if opt.Filename == "" {
|
||||
opt.Filename = fmt.Sprintf("%s_vfsdata.go", strings.ToLower(opt.VariableName))
|
||||
}
|
||||
if opt.VariableComment == "" {
|
||||
opt.VariableComment = fmt.Sprintf("%s statically implements the virtual filesystem provided to vfsgen.", opt.VariableName)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package vfsgen
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// stringWriter writes given bytes to underlying io.Writer as a Go interpreted string literal value,
|
||||
// not including double quotes. It tracks the total number of bytes written.
|
||||
type stringWriter struct {
|
||||
io.Writer
|
||||
N int64 // Total bytes written.
|
||||
}
|
||||
|
||||
func (sw *stringWriter) Write(p []byte) (n int, err error) {
|
||||
const hex = "0123456789abcdef"
|
||||
buf := []byte{'\\', 'x', 0, 0}
|
||||
for _, b := range p {
|
||||
buf[2], buf[3] = hex[b/16], hex[b%16]
|
||||
_, err = sw.Writer.Write(buf)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
n++
|
||||
sw.N++
|
||||
}
|
||||
return n, nil
|
||||
}
|
Loading…
Reference in New Issue