change structure to embed source json and template files
restructure code to have defaults in place of choices
This commit is contained in:
parent
d1ded77768
commit
065fe9e9af
1
flag-generator/.gitignore
vendored
Normal file
1
flag-generator/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
vendor
|
214
flag-generator/fs_vfsdata.go
Normal file
214
flag-generator/fs_vfsdata.go
Normal file
@ -0,0 +1,214 @@
|
||||
// Code generated by vfsgen; DO NOT EDIT.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"time"
|
||||
)
|
||||
|
||||
// fs statically implements the virtual filesystem provided to vfsgen.
|
||||
var fs = func() http.FileSystem {
|
||||
fs := vfsgen۰FS{
|
||||
"/": &vfsgen۰DirInfo{
|
||||
name: "/",
|
||||
modTime: time.Time{},
|
||||
},
|
||||
"/source": &vfsgen۰DirInfo{
|
||||
name: "source",
|
||||
modTime: time.Time{},
|
||||
},
|
||||
"/source/flag-types.json": &vfsgen۰CompressedFileInfo{
|
||||
name: "flag-types.json",
|
||||
modTime: time.Time{},
|
||||
uncompressedSize: 2559,
|
||||
|
||||
compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xc4\x94\xc1\x6e\x9c\x30\x10\x86\xef\x3c\xc5\xc8\xbd\x40\xb5\x82\x1c\x56\x7b\xd8\x63\x55\xb5\xda\x5b\xa5\x34\xbd\x24\x51\xe4\x80\x21\x56\x1d\x1b\xd9\x43\xd4\x28\xca\xbb\x57\xf6\x2e\xbb\x60\x0c\x62\x5b\x2d\xb9\xfe\x23\x98\xef\x9b\xb1\xe6\x36\x02\x78\x8b\x00\x00\x88\xa4\xcf\x8c\x6c\x81\x7c\x51\x4a\x90\xd5\x3e\xc3\xd7\xda\x65\x8f\x9d\xec\x85\x8a\xc6\x86\x25\x15\x86\x1d\xb2\x82\x19\x24\x5b\x40\xdd\xb4\x49\xae\x24\xb2\x3f\xf8\x50\xb0\x92\x36\xc2\x16\x89\xfb\xa0\xfd\x4b\x4d\xb5\x61\xda\xc6\x06\x75\xae\xe4\x4b\xfa\xc3\x26\xb6\x79\x5c\xa6\xbf\x6c\x8f\xf4\x1a\x35\x97\x55\x9c\x24\x24\x02\x78\x5f\x85\x51\x7f\xfe\x3f\x6b\xa1\x72\xa4\x5c\xd8\x8f\x01\x9f\x28\x02\x37\xae\x0c\x8f\xaf\xd0\xf2\x2f\xa9\xf5\xb5\xd1\x14\xb9\x92\xbe\x19\xf2\x67\x96\xfa\xc5\x56\xb1\xeb\x33\x69\x18\x1b\xc6\xe0\x09\xb1\x36\xdb\x2c\xab\x94\xa0\xb2\x4a\x95\xae\xb2\xfa\x77\x95\xd9\x0e\xd9\x27\x87\xdc\xf6\x49\x26\xd4\xaf\x86\xda\x8e\xb1\xf7\x83\xf9\xde\xdf\x84\xa2\xb8\x59\xfb\xda\x65\x3f\x9e\x25\x3c\x0f\xb6\xb7\x23\xd7\x7d\x00\xbb\x82\xcd\x7a\x14\xf8\x3b\x93\x4c\xf3\xdc\x07\xf6\xe2\x71\xe0\xee\xb3\x0c\x10\x4b\x2e\xfc\xe1\xb7\x2d\xb8\x44\xa6\x4b\x9a\xb3\xb7\xf7\x31\xb8\x9d\x0c\xcc\x92\xcb\x05\x26\xb9\x93\xa1\x39\x5e\x4d\x8e\x72\x27\x31\xc0\xfa\xa1\xa4\xbd\x4f\x1f\x72\xea\xfa\x59\xaa\xd8\x45\xc5\x94\xcb\xb5\xe0\x39\xf3\x85\x3e\xfb\x85\x4b\xbc\x8c\xdb\xfb\xce\xdc\x4e\xda\x47\xcd\xf8\x48\x91\x24\xfb\xc8\x2a\x33\xad\x95\x8e\x25\x17\x53\x52\x9b\xf5\xa8\x96\x57\xba\x98\xd8\xe9\xf1\x8e\xa9\x1d\x48\xce\x93\xdb\xef\xde\x17\x33\xbd\xf4\x5f\xdf\xe0\x1d\xb9\x23\x43\xe8\xe1\xab\x9b\x4b\x19\xde\x41\xa0\x76\x99\x25\xf4\x87\x12\xdc\x42\x87\xe5\xbc\x35\xdc\xf0\xd0\xcd\x6a\x16\x39\x5a\xb6\xf7\xd9\x57\xeb\x86\x0f\xcf\x56\xb3\xc0\xdd\x9a\x86\x0d\x1f\xae\xc6\xbf\x5c\xd1\x7d\xf4\x37\x00\x00\xff\xff\x66\x52\x85\x44\xff\x09\x00\x00"),
|
||||
},
|
||||
"/templates": &vfsgen۰DirInfo{
|
||||
name: "templates",
|
||||
modTime: time.Time{},
|
||||
},
|
||||
"/templates/altsrc_flags_generated.gotpl": &vfsgen۰CompressedFileInfo{
|
||||
name: "altsrc_flags_generated.gotpl",
|
||||
modTime: time.Time{},
|
||||
uncompressedSize: 1064,
|
||||
|
||||
compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xb4\x92\x41\x6b\xdc\x30\x10\x85\xcf\xd1\xaf\x78\x2c\xa1\xac\xc3\xd6\xbe\x6f\xc9\xa1\x34\x29\xf4\xb2\x29\x24\xd0\xb3\xd6\x1e\xd9\xa2\x5a\xdb\x48\xe3\x5d\x16\xe1\xff\x5e\x46\x0e\xe9\x96\x6a\x4b\x2f\xb9\xc9\x1a\xbd\xf7\xe6\x9b\x71\x55\xe1\xcb\xd0\x10\x5a\xea\xc9\x6b\xa6\x06\xfb\x33\x4c\xfb\x09\x0f\x4f\xd8\x3d\xbd\xe0\xf1\xe1\xdb\x4b\xa9\xd4\xa8\xeb\x9f\xba\x25\xc4\x88\xf2\xfb\x72\xde\xe9\x03\x61\x9e\x95\xb2\x87\x71\xf0\x8c\xb5\xba\x59\x19\xa7\xdb\x95\x02\x80\x55\x6b\xb9\x9b\xf6\x65\x3d\x1c\xaa\xc9\x1b\x7d\xa4\xaa\x76\x76\xa5\x0a\xa5\x62\x84\xd7\x7d\x4b\xb8\xb5\x1b\xdc\x8a\x04\xdb\x7b\x94\x5f\x9d\x6e\x83\x18\x56\x95\xc4\xa4\x42\xf9\x1a\x22\x35\xd8\x00\xee\x08\x49\xc0\xe7\x91\xc0\x9d\x66\x9c\xbc\x1e\x03\x6a\x67\xcb\xac\x88\x07\x68\xe7\x86\x93\xb8\x9a\xc1\x63\xe0\x8e\x3c\x8e\xda\x4d\x14\xa4\xb8\x27\x84\x91\x6a\x6b\x2c\x35\x2a\xb9\x66\x6d\x02\xfb\xa9\x66\xc4\xc4\x76\x2d\x2c\x15\x03\x31\xee\x52\x45\xae\x9e\x89\xd5\xac\x24\x7c\x47\xa7\xac\x73\xed\x49\x33\x05\x68\xf4\x74\xca\x86\x2b\x33\xf5\xf5\x35\xfd\xda\xb8\xab\xfd\x14\xb8\xcb\x46\x2e\x14\x9e\x78\xf2\x3d\x3e\xe4\x9e\xc4\x6c\x23\x5b\x18\xb7\x11\xc0\x2d\x7a\xeb\x30\xbf\x92\x7d\x1e\x47\x77\x46\xd0\x47\xfa\xbd\xa0\x67\xe2\x34\x6e\xa7\x99\x3c\xa6\x20\xff\x4e\xad\x9d\x0b\x1b\x79\xd2\x2f\x67\x11\x8b\x40\x56\x38\x52\x93\xcd\x2c\x93\xfb\x32\x82\xb5\xc9\x03\x15\x4b\x0b\xeb\xbf\x46\x5f\x20\xaa\x1b\x53\xca\xfd\xbd\xf4\x2d\x1f\xd7\x43\x44\x5f\x5c\x32\xfd\xb0\xdc\x3d\x7a\x3f\xf8\xf7\x85\x7b\x8b\xf9\x2f\xca\xb7\xd7\x39\x5c\x4a\xdd\x2e\xfb\xbd\xe4\xbe\xd8\xf7\xbf\x26\xf0\x87\xb7\x8c\x22\xc6\x8f\xa0\xbe\xc1\x3c\xff\x0a\x00\x00\xff\xff\x3c\x32\xe5\x42\x28\x04\x00\x00"),
|
||||
},
|
||||
"/templates/cli_flags_generated.gotpl": &vfsgen۰CompressedFileInfo{
|
||||
name: "cli_flags_generated.gotpl",
|
||||
modTime: time.Time{},
|
||||
uncompressedSize: 2234,
|
||||
|
||||
compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xbc\x55\x41\x4f\xeb\x38\x10\x3e\x37\xbf\x62\xb6\xe2\x90\xa0\x6e\x7b\x5f\xc4\x89\xc2\x2e\xd2\x0a\xd0\x83\xc7\xdd\x4d\xc6\xa9\x85\xb1\x8b\xed\xc0\x43\x55\xfe\xfb\xd3\x8c\x9d\xb4\x4d\x0b\xf4\xf2\xe8\xc9\x9e\xf9\xf2\xcd\x37\xdf\x8c\xea\xd9\x0c\x2e\x6c\x85\x50\xa3\x41\x27\x02\x56\xb0\x78\x07\x59\x9f\xc1\xfc\x16\x6e\x6e\x1f\xe0\x72\x7e\xfd\x30\xcd\xb2\x95\x28\x9f\x44\x8d\xb0\x5e\xc3\xf4\x2e\x9e\x6f\xc4\x33\x42\xdb\x66\x99\x7a\x5e\x59\x17\x20\xcf\x46\x63\xa9\x45\x3d\xce\x46\x63\x1f\x5c\x69\xcd\x2b\x1d\x83\x7a\xc6\x71\x56\x64\xeb\x35\x38\x61\x6a\x84\x13\x35\x81\x13\x02\xc2\x3f\xe7\x30\xbd\xd2\xa2\xf6\x44\x33\x9b\x11\x39\x27\xa6\x89\x9a\x72\xa0\x3c\x08\x60\xf8\x9b\x0a\x4b\x08\xef\x2b\xdc\x00\x1f\xe8\xd6\xb6\xfd\x7d\x6e\xcb\x20\x94\x26\xbe\x5d\xe0\x36\xa3\x0f\xae\x29\x03\xac\x33\x00\x00\x4e\xa4\x9f\x0f\x4e\x99\x9a\xc3\x3f\x3d\x75\xbb\x17\xbe\x34\xaf\x8f\xc2\xed\x85\xaf\x94\xc6\x3b\x11\x96\x83\xf0\x0f\x7c\x69\x94\xc3\x8a\xce\x0b\x6b\x35\x07\xff\x53\x55\x85\x26\x52\xf4\xc1\xf5\xfa\x6f\x50\x12\xf0\x25\xc9\x7d\x14\xba\x41\x08\xae\x61\x87\x47\x7c\x1d\x8d\x86\x6d\x67\x23\xfa\x0e\x4d\xd5\x9f\xb7\x39\xe6\xe8\x83\x32\x22\x28\x6b\x36\x4c\x5b\xc1\xd1\xe9\xa7\x7c\x6d\x46\x23\xb9\xe7\x6e\xc0\x61\x68\x9c\xa1\x49\x38\x14\x95\x58\x68\x04\x87\x2b\x87\x1e\x4d\x88\x15\xac\x84\xb0\x54\x1e\x5e\x49\x2a\x7d\x99\x4b\xeb\xa0\x61\x1b\x2b\x94\xa2\xd1\xc1\x17\x99\x6c\x4c\x09\xb9\x3c\x38\x97\x22\x15\xcb\x8b\xe4\x61\x9a\x50\xac\x0d\x04\x89\x00\x74\xb9\x2c\x92\xbe\x7f\x31\x30\x45\x27\x30\x2c\x11\x0c\x05\x58\x0f\xf2\xe2\x7c\x51\x35\x51\x7c\x50\x56\x32\x38\x55\xbb\xf6\xfd\x48\xbb\x82\x6f\x4b\x0c\x4b\x74\x60\x1d\x18\x1b\xfa\x9a\xb4\xb6\x2e\x61\xbf\xa8\xbf\x21\xcd\x0b\xde\x88\xa1\x80\x2e\x9d\x44\x0c\x59\x40\x5b\xfb\xe4\xa1\x59\x71\x71\xf6\x9f\xba\x17\xa0\x6d\x29\xf4\xc1\xa2\x93\x4e\xfe\x0e\xdf\x85\x35\x01\x7f\x85\x79\x9c\x16\x31\x2b\xc9\x4d\x49\xdb\x98\xae\x8b\x12\x4e\x13\xae\xd8\xa3\xce\xd9\xf9\xe8\x22\x67\xe9\x7b\x84\x8e\x98\xb7\x6c\x3c\x26\xe2\x61\xcd\xb4\x80\x14\x47\xed\x71\x07\xb2\xc9\x75\xbb\xb9\x6b\x10\xb5\xdf\xac\x0e\x6a\x99\x40\x39\xa5\xe0\x3d\x86\x7e\x61\xb4\x5d\x08\x7d\xb4\x87\x35\xc3\xff\xa4\x89\x87\x05\x7d\xb3\x95\x4a\x82\xf4\xf4\x7f\x1c\xcd\x8c\x9a\xae\xa2\x73\x9d\x93\xc5\x19\x61\xfe\x3a\x07\xa3\xba\x15\x3d\x76\x0a\xd2\x17\x8c\x6f\xb7\xe7\xf6\xb1\x65\x34\x2a\x36\xea\x13\xd2\xe4\xcd\x04\x3c\x06\x38\xe5\x74\xd2\xfb\x0d\x76\x49\xb2\xca\x63\x98\xfe\xcf\x02\x59\x50\xd1\x1b\xb9\xef\xd1\x46\xd0\x9d\x70\x1e\x5d\xd4\xb2\xa2\x73\x35\x01\x74\x8e\xf8\xfa\xb2\x09\xc3\xcf\x5a\xd2\x34\x80\xca\xf8\x40\xf0\xdd\xba\xdc\x28\x5d\x10\x36\xfe\x71\x77\x45\xe9\x2d\x70\x6e\x5f\xcc\x71\x23\xe8\x90\xed\x87\x4d\x5c\x08\x1f\x62\x23\x43\xb6\xad\xfc\x76\x13\x09\x16\x7b\xd9\xd5\x7b\xfc\x62\x6c\x66\xf1\x3b\x00\x00\xff\xff\x59\x27\xb0\x6d\xba\x08\x00\x00"),
|
||||
},
|
||||
}
|
||||
fs["/"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
|
||||
fs["/source"].(os.FileInfo),
|
||||
fs["/templates"].(os.FileInfo),
|
||||
}
|
||||
fs["/source"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
|
||||
fs["/source/flag-types.json"].(os.FileInfo),
|
||||
}
|
||||
fs["/templates"].(*vfsgen۰DirInfo).entries = []os.FileInfo{
|
||||
fs["/templates/altsrc_flags_generated.gotpl"].(os.FileInfo),
|
||||
fs["/templates/cli_flags_generated.gotpl"].(os.FileInfo),
|
||||
}
|
||||
|
||||
return fs
|
||||
}()
|
||||
|
||||
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) {
|
||||
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
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
9
flag-generator/go.mod
Normal file
9
flag-generator/go.mod
Normal file
@ -0,0 +1,9 @@
|
||||
module github.com/urfave/cli/flag-generator
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
|
||||
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
|
||||
github.com/urfave/cli v1.21.0
|
||||
)
|
9
flag-generator/go.sum
Normal file
9
flag-generator/go.sum
Normal file
@ -0,0 +1,9 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
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/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
|
||||
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
|
||||
github.com/urfave/cli v1.21.0 h1:wYSSj06510qPIzGSua9ZqsncMmWE3Zr55KBERygyrxE=
|
||||
github.com/urfave/cli v1.21.0/go.mod h1:lxDj6qX9Q6lWQxIrbrT0nwecwUtRnhVZAJjJZrVUZZQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
@ -2,10 +2,16 @@ 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 {
|
||||
@ -25,178 +31,133 @@ type FlagType struct {
|
||||
ParserCast string `json:"parser_cast"`
|
||||
}
|
||||
|
||||
var flagTemplate = `// Code generated by fg; DO NOT EDIT.
|
||||
|
||||
package {{ .PackageName }}
|
||||
{{ if eq .PackageName "cli" }}
|
||||
import (
|
||||
"flag"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
{{ range $i, $flag := .Flags }}
|
||||
// {{ $flag.Name }}Flag is a flag with type {{ $flag.Type }}{{ $flag.Doctail }}
|
||||
type {{ $flag.Name }}Flag struct {
|
||||
Name string
|
||||
Usage string
|
||||
EnvVar string
|
||||
FilePath string
|
||||
Required bool
|
||||
Hidden bool
|
||||
{{- if eq $flag.Value true }}
|
||||
Value {{ $flag.Type }}
|
||||
{{- end }}
|
||||
{{- if eq $flag.Destination true }}
|
||||
Destination *{{ $flag.Type }}
|
||||
{{- end }}
|
||||
// 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
|
||||
}
|
||||
|
||||
// String returns a readable representation of this value
|
||||
// (for usage defaults)
|
||||
func (f {{ $flag.Name }}Flag) String() string {
|
||||
return FlagStringer(f)
|
||||
func (fs zeroModTimeFileSystem) Open(name string) (http.File, error) {
|
||||
f, err := fs.Source.Open(name)
|
||||
return file{f}, err
|
||||
}
|
||||
|
||||
// GetName returns the name of the flag
|
||||
func (f {{ $flag.Name }}Flag) GetName() string {
|
||||
return f.Name
|
||||
type file struct {
|
||||
http.File
|
||||
}
|
||||
|
||||
// IsRequired returns whether or not the flag is required
|
||||
func (f {{ $flag.Name }}Flag) IsRequired() bool {
|
||||
return f.Required
|
||||
func (f file) Stat() (os.FileInfo, error) {
|
||||
fi, err := f.File.Stat()
|
||||
return fileInfo{fi}, err
|
||||
}
|
||||
|
||||
// {{ $flag.Name }} looks up the value of a local {{ $flag.Name }}Flag, returns
|
||||
// {{ $flag.ContextDefault }} if not found
|
||||
func (c *Context) {{ $flag.Name }}(name string) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
return lookup{{ $flag.Name }}(name, c.flagSet)
|
||||
type fileInfo struct {
|
||||
os.FileInfo
|
||||
}
|
||||
|
||||
// Global{{ $flag.Name }} looks up the value of a global {{ $flag.Name }}Flag, returns
|
||||
// {{ $flag.ContextDefault }} if not found
|
||||
func (c *Context) Global{{ $flag.Name }}(name string) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||
return lookup{{ $flag.Name }}(name, fs)
|
||||
}
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
|
||||
func lookup{{ $flag.Name }}(name string, set *flag.FlagSet) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
f := set.Lookup(name)
|
||||
if f != nil {
|
||||
{{ if ne .Parser "" }}parsed, err := {{ $flag.Parser }}{{ else }}parsed, err := f.Value, error(nil){{ end }}
|
||||
if err != nil {
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
{{ if ne .ParserCast "" }}return {{ $flag.ParserCast }}{{ else }}return parsed{{ end }}
|
||||
}
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
{{- end }}
|
||||
{{ else }}
|
||||
import (
|
||||
"flag"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
{{ range $i, $flag := .Flags }}
|
||||
// {{ $flag.Name }}Flag is the flag type that wraps cli.{{ $flag.Name }}Flag to allow
|
||||
// for other values to be specified
|
||||
type {{ $flag.Name }}Flag struct {
|
||||
cli.{{ $flag.Name }}Flag
|
||||
set *flag.FlagSet
|
||||
}
|
||||
|
||||
// New{{ $flag.Name }}Flag creates a new {{ $flag.Name }}Flag
|
||||
func New{{ $flag.Name }}Flag(fl cli.{{ $flag.Name }}Flag) *{{ $flag.Name }}Flag {
|
||||
return &{{ $flag.Name }}Flag{ {{ $flag.Name }}Flag: fl, set: nil }
|
||||
}
|
||||
|
||||
// Apply saves the flagSet for later usage calls, then calls
|
||||
// the wrapped {{ $flag.Name }}Flag.Apply
|
||||
func (f *{{ $flag.Name }}Flag) Apply(set *flag.FlagSet) {
|
||||
f.set = set
|
||||
f.{{ $flag.Name }}Flag.Apply(set)
|
||||
}
|
||||
|
||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
||||
// the wrapped {{ $flag.Name }}Flag.ApplyWithError
|
||||
func (f *{{ $flag.Name }}Flag) ApplyWithError(set *flag.FlagSet) error {
|
||||
f.set = set
|
||||
return f.{{ $flag.Name }}Flag.ApplyWithError(set)
|
||||
}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
`
|
||||
func (fi fileInfo) ModTime() time.Time { return time.Time{} }
|
||||
|
||||
func main() {
|
||||
var packageName, inputPath, outputPath string
|
||||
|
||||
app := cli.NewApp()
|
||||
|
||||
app.Name = "fg"
|
||||
app.Usage = "Generate flag type code!"
|
||||
app.Version = "v0.1.0"
|
||||
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "package, p",
|
||||
Value: "cli",
|
||||
Usage: "`PACKAGE` for which the flag types will be generated",
|
||||
Destination: &packageName,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "input, i",
|
||||
Usage: "path to the `INPUT JSON FILE` which defines each type to be generated",
|
||||
Destination: &inputPath,
|
||||
},
|
||||
cli.StringFlag{
|
||||
Name: "output, o",
|
||||
Usage: "path to the `OUTPUT GO FILE` which will contain the flag types",
|
||||
Destination: &outputPath,
|
||||
},
|
||||
app.Action = ActionFunc
|
||||
|
||||
err := GenerateAssets()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
app.Action = func(c *cli.Context) error {
|
||||
var info CliFlagInfo
|
||||
var tpl *template.Template
|
||||
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")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(inFile)
|
||||
err = decoder.Decode(&info.Flags)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = inFile.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, packageName := range []string{"cli", "altsrc"} {
|
||||
info.PackageName = packageName
|
||||
|
||||
inFile, err := os.Open(inputPath)
|
||||
bytes, err := ReadTemplate(packageName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer inFile.Close()
|
||||
tpl = template.Must(template.New("").Parse(string(bytes)))
|
||||
|
||||
decoder := json.NewDecoder(inFile)
|
||||
var outFile *os.File
|
||||
|
||||
err = decoder.Decode(&info.Flags)
|
||||
if packageName == "cli" {
|
||||
outFile, err = os.Create("flag_generated.go")
|
||||
} else {
|
||||
outFile, err = os.Create("altsrc/flag_generated.go")
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
tpl = template.Must(template.New("").Parse(flagTemplate))
|
||||
|
||||
outFile, err := os.Create(outputPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer outFile.Close()
|
||||
|
||||
err = tpl.Execute(outFile, info)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
err = outFile.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ReadTemplate(packageName string) ([]byte, error) {
|
||||
templateFile, err := fs.Open(fmt.Sprintf("/templates/%s_flags_generated.gotpl", packageName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templateFileBytes, err := ioutil.ReadAll(templateFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return templateFileBytes, nil
|
||||
}
|
||||
|
36
flag-generator/templates/altsrc_flags_generated.gotpl
Normal file
36
flag-generator/templates/altsrc_flags_generated.gotpl
Normal file
@ -0,0 +1,36 @@
|
||||
// Code generated by fg; DO NOT EDIT.
|
||||
|
||||
package {{ .PackageName }}
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
{{ range $i, $flag := .Flags }}
|
||||
// {{ $flag.Name }}Flag is the flag type that wraps cli.{{ $flag.Name }}Flag to allow
|
||||
// for other values to be specified
|
||||
type {{ $flag.Name }}Flag struct {
|
||||
cli.{{ $flag.Name }}Flag
|
||||
set *flag.FlagSet
|
||||
}
|
||||
|
||||
// New{{ $flag.Name }}Flag creates a new {{ $flag.Name }}Flag
|
||||
func New{{ $flag.Name }}Flag(fl cli.{{ $flag.Name }}Flag) *{{ $flag.Name }}Flag {
|
||||
return &{{ $flag.Name }}Flag{ {{ $flag.Name }}Flag: fl, set: nil }
|
||||
}
|
||||
|
||||
// Apply saves the flagSet for later usage calls, then calls
|
||||
// the wrapped {{ $flag.Name }}Flag.Apply
|
||||
func (f *{{ $flag.Name }}Flag) Apply(set *flag.FlagSet) {
|
||||
f.set = set
|
||||
f.{{ $flag.Name }}Flag.Apply(set)
|
||||
}
|
||||
|
||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
||||
// the wrapped {{ $flag.Name }}Flag.ApplyWithError
|
||||
func (f *{{ $flag.Name }}Flag) ApplyWithError(set *flag.FlagSet) error {
|
||||
f.set = set
|
||||
return f.{{ $flag.Name }}Flag.ApplyWithError(set)
|
||||
}
|
||||
{{- end }}
|
69
flag-generator/templates/cli_flags_generated.gotpl
Normal file
69
flag-generator/templates/cli_flags_generated.gotpl
Normal file
@ -0,0 +1,69 @@
|
||||
// Code generated by fg; DO NOT EDIT.
|
||||
|
||||
package {{ .PackageName }}
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
{{ range $i, $flag := .Flags }}
|
||||
// {{ $flag.Name }}Flag is a flag with type {{ $flag.Type }}{{ $flag.Doctail }}
|
||||
type {{ $flag.Name }}Flag struct {
|
||||
Name string
|
||||
Usage string
|
||||
EnvVar string
|
||||
FilePath string
|
||||
Required bool
|
||||
Hidden bool
|
||||
{{- if eq $flag.Value true }}
|
||||
Value {{ $flag.Type }}
|
||||
{{- end }}
|
||||
{{- if eq $flag.Destination true }}
|
||||
Destination *{{ $flag.Type }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
// String returns a readable representation of this value
|
||||
// (for usage defaults)
|
||||
func (f {{ $flag.Name }}Flag) String() string {
|
||||
return FlagStringer(f)
|
||||
}
|
||||
|
||||
// GetName returns the name of the flag
|
||||
func (f {{ $flag.Name }}Flag) GetName() string {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
// IsRequired returns whether or not the flag is required
|
||||
func (f {{ $flag.Name }}Flag) IsRequired() bool {
|
||||
return f.Required
|
||||
}
|
||||
|
||||
// {{ $flag.Name }} looks up the value of a local {{ $flag.Name }}Flag, returns
|
||||
// {{ $flag.ContextDefault }} if not found
|
||||
func (c *Context) {{ $flag.Name }}(name string) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
return lookup{{ $flag.Name }}(name, c.flagSet)
|
||||
}
|
||||
|
||||
// Global{{ $flag.Name }} looks up the value of a global {{ $flag.Name }}Flag, returns
|
||||
// {{ $flag.ContextDefault }} if not found
|
||||
func (c *Context) Global{{ $flag.Name }}(name string) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
||||
return lookup{{ $flag.Name }}(name, fs)
|
||||
}
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
|
||||
func lookup{{ $flag.Name }}(name string, set *flag.FlagSet) {{ if ne .ContextType "" }} {{ $flag.ContextType }} {{ else }} {{ $flag.Type }} {{- end }} {
|
||||
f := set.Lookup(name)
|
||||
if f != nil {
|
||||
{{ if ne .Parser "" }}parsed, err := {{ $flag.Parser }}{{ else }}parsed, err := f.Value, error(nil){{ end }}
|
||||
if err != nil {
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
{{ if ne .ParserCast "" }}return {{ $flag.ParserCast }}{{ else }}return parsed{{ end }}
|
||||
}
|
||||
return {{ $flag.ContextDefault }}
|
||||
}
|
||||
{{- end }}
|
Loading…
Reference in New Issue
Block a user