move around code

change package to flag-gen to avoid conflict with flag-generator binary
test code generation
main
Ajitem Sahasrabuddhe 5 years ago
parent 065fe9e9af
commit 2a084945a4
No known key found for this signature in database
GPG Key ID: 5B0EE10DAA76876C

@ -0,0 +1,53 @@
// +build ignore
package main
import (
"github.com/shurcooL/httpfs/union"
"github.com/shurcooL/vfsgen"
"log"
"net/http"
"os"
"time"
)
// zeroModTimeFileSystem is an http.FileSystem wrapper.
// It exposes a filesystem exactly like Source, except
// all file modification times are changed to zero.
// See https://github.com/shurcooL/vfsgen/pull/40#issuecomment-355416103
type zeroModTimeFileSystem struct {
Source http.FileSystem
}
func (fs zeroModTimeFileSystem) Open(name string) (http.File, error) {
f, err := fs.Source.Open(name)
return file{f}, err
}
type file struct {
http.File
}
func (f file) Stat() (os.FileInfo, error) {
fi, err := f.File.Stat()
return fileInfo{fi}, err
}
type fileInfo struct {
os.FileInfo
}
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
func main() {
err := vfsgen.Generate(zeroModTimeFileSystem{
Source: union.New(map[string]http.FileSystem{
"/templates": http.Dir("templates"),
"/source": http.Dir("source"),
}),
}, vfsgen.Options{})
if err != nil {
log.Fatal(err)
}
}

@ -14,8 +14,8 @@ import (
"time"
)
// fs statically implements the virtual filesystem provided to vfsgen.
var fs = func() http.FileSystem {
// assets statically implements the virtual filesystem provided to vfsgen.
var assets = func() http.FileSystem {
fs := vfsgen۰FS{
"/": &vfsgen۰DirInfo{
name: "/",

@ -1,17 +1,15 @@
//go:generate go run assets_generate.go
package main
import (
"encoding/json"
"fmt"
"github.com/shurcooL/httpfs/union"
"github.com/shurcooL/vfsgen"
"github.com/urfave/cli"
"io/ioutil"
"log"
"net/http"
"os"
"text/template"
"time"
)
type CliFlagInfo struct {
@ -31,34 +29,6 @@ type FlagType struct {
ParserCast string `json:"parser_cast"`
}
// zeroModTimeFileSystem is an http.FileSystem wrapper.
// It exposes a filesystem exactly like Source, except
// all file modification times are changed to zero.
// See https://github.com/shurcooL/vfsgen/pull/40#issuecomment-355416103
type zeroModTimeFileSystem struct {
Source http.FileSystem
}
func (fs zeroModTimeFileSystem) Open(name string) (http.File, error) {
f, err := fs.Source.Open(name)
return file{f}, err
}
type file struct {
http.File
}
func (f file) Stat() (os.FileInfo, error) {
fi, err := f.File.Stat()
return fileInfo{fi}, err
}
type fileInfo struct {
os.FileInfo
}
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
func main() {
app := cli.NewApp()
@ -68,36 +38,17 @@ func main() {
app.Action = ActionFunc
err := GenerateAssets()
if err != nil {
log.Fatal(err)
}
err = app.Run(os.Args)
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func GenerateAssets() error {
fs := zeroModTimeFileSystem{
Source: union.New(map[string]http.FileSystem{
"/templates": http.Dir("templates"),
"/source": http.Dir("source"),
}),
}
return vfsgen.Generate(fs, vfsgen.Options{
PackageName: "main",
VariableName: "fs",
})
}
func ActionFunc(_ *cli.Context) error {
var info CliFlagInfo
var tpl *template.Template
inFile, err := fs.Open("/source/flag-types.json")
inFile, err := assets.Open("/source/flag-types.json")
if err != nil {
log.Fatal(err)
}
@ -149,7 +100,7 @@ func ActionFunc(_ *cli.Context) error {
}
func ReadTemplate(packageName string) ([]byte, error) {
templateFile, err := fs.Open(fmt.Sprintf("/templates/%s_flags_generated.gotpl", packageName))
templateFile, err := assets.Open(fmt.Sprintf("/templates/%s_flags_generated.gotpl", packageName))
if err != nil {
return nil, err
}
Loading…
Cancel
Save