Skip to content

Commit

Permalink
add fallback
Browse files Browse the repository at this point in the history
Signed-off-by: cwen0 <[email protected]>
  • Loading branch information
cwen0 committed Jun 18, 2020
1 parent 6a9ea43 commit c353227
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
.idea
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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)
[![Build Status](https://travis-ci.org/cwen0/vfsgen.svg?branch=master)](https://travis-ci.org/cwen0/vfsgen) [![GoDoc](https://godoc.org/github.com/cwen0/vfsgen?status.svg)](https://godoc.org/github.com/cwen0/vfsgen)

Package vfsgen takes an http.FileSystem (likely at `go generate` time) and
generates Go code that statically implements the provided http.FileSystem.
Expand All @@ -20,7 +20,7 @@ Installation
------------

```bash
go get -u github.com/shurcooL/vfsgen
go get -u github.com/cwen0/vfsgen
```

Usage
Expand Down Expand Up @@ -104,7 +104,7 @@ import (
"log"

"example.com/project/data"
"github.com/shurcooL/vfsgen"
"github.com/cwen0/vfsgen"
)

func main() {
Expand All @@ -128,7 +128,7 @@ Note that "dev" build tag is used to access the source filesystem, and the outpu
Make sure it's installed and available in your PATH.

```bash
go get -u github.com/shurcooL/vfsgen/cmd/vfsgendev
go get -u github.com/cwen0/vfsgen/cmd/vfsgendev
```

Then the "//go:generate go run -tags=dev assets_generate.go" directive can be replaced with:
Expand All @@ -141,7 +141,7 @@ vfsgendev accesses the source variable using "dev" build tag, and generates an o

### 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:
All compressed files implement [`httpgzip.GzipByter` interface](https://godoc.org/github.com/cwen0/httpgzip#GzipByter) for efficient direct access to the internal compressed bytes:

```Go
// GzipByter is implemented by compressed files for
Expand All @@ -152,7 +152,7 @@ type GzipByter interface {
}
```

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):
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/cwen0/httpgzip#NotWorthGzipCompressing):

```Go
// NotWorthGzipCompressing is implemented by files that were determined
Expand Down
2 changes: 1 addition & 1 deletion cmd/vfsgendev/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var generateTemplate = template.Must(template.New("").Funcs(template.FuncMap{
import (
"log"
"github.com/shurcooL/vfsgen"
"github.com/cwen0/vfsgen"
sourcepkg {{.ImportPath | quote}}
)
Expand Down
10 changes: 9 additions & 1 deletion generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func Generate(input http.FileSystem, opt Options) error {
return err
}

toc.Fallback = opt.Fallback
err = t.ExecuteTemplate(buf, "Trailer", toc)
if err != nil {
return err
Expand All @@ -58,6 +59,9 @@ type toc struct {

HasCompressedFile bool // There's at least one compressedFile.
HasFile bool // There's at least one uncompressed file.

// Fallback file that is served if no other is found
Fallback string
}

// fileInfo is a definition of a file.
Expand Down Expand Up @@ -288,7 +292,11 @@ 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}
{{ if .Fallback -}}
fallbackPath := pathpkg.Clean("/" + "{{.Fallback}}")
f = fs[fallbackPath]
{{- else -}}
return nil, &os.PathError{Op: "open", Path: path, Err: os.ErrNotExist}{{end}}
}
switch f := f.(type) {{"{"}}{{if .HasCompressedFile}}
Expand Down
2 changes: 1 addition & 1 deletion generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"strings"
"testing"

"github.com/cwen0/vfsgen"
"github.com/shurcooL/httpfs/union"
"github.com/shurcooL/vfsgen"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
Expand Down
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/cwen0/vfsgen

go 1.13

require (
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0
golang.org/x/tools v0.0.0-20200618031402-d15173dcc7e4
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0 h1:mj/nMDAwTBiaCqMEs4cYCqF7pO6Np7vhy1D1wcQGz+E=
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0/go.mod h1:919LwcH0M7/W4fcZ0/jy0qGght1GIhqyS/EgWGH2j5Q=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200618031402-d15173dcc7e4 h1:xotC2PrtjSST30ilZLyawOqMNuVyq9ev3IlvoQwaZZ8=
golang.org/x/tools v0.0.0-20200618031402-d15173dcc7e4/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type Options struct {
// 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

// Fallback file that is served if no other is found
Fallback string
}

// fillMissing sets default values for mandatory options that are left empty.
Expand Down
2 changes: 1 addition & 1 deletion test/test_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"net/http"

"github.com/shurcooL/vfsgen"
"github.com/cwen0/vfsgen"
"golang.org/x/tools/godoc/vfs/httpfs"
"golang.org/x/tools/godoc/vfs/mapfs"
)
Expand Down

0 comments on commit c353227

Please sign in to comment.