mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
Update github.com/mjibson/esc (#515)
* Update github.com/mjibson/esc * Update github.com/mjibson/esc * Fix generate.go to new signature for esc.Run * Internal: Upgrade to lastest "esc"
This commit is contained in:
284
vendor/github.com/mjibson/esc/embed/embed.go
generated
vendored
284
vendor/github.com/mjibson/esc/embed/embed.go
generated
vendored
@ -6,6 +6,7 @@ import (
|
||||
"compress/gzip"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
@ -15,6 +16,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/tools/imports"
|
||||
)
|
||||
|
||||
// Config contains all information needed to run esc.
|
||||
@ -36,6 +40,8 @@ type Config struct {
|
||||
Private bool
|
||||
// NoCompression, if true, stores the files without compression.
|
||||
NoCompression bool
|
||||
// Invocation, if set, is added to the invocation string in the generated template.
|
||||
Invocation string
|
||||
|
||||
// Files is the list of files or directories to embed.
|
||||
Files []string
|
||||
@ -43,20 +49,36 @@ type Config struct {
|
||||
|
||||
var modTime *int64
|
||||
|
||||
type headerTemplateParams struct {
|
||||
var tmpl = template.Must(template.New("").Parse(fileTemplate))
|
||||
|
||||
type templateParams struct {
|
||||
Invocation string
|
||||
PackageName string
|
||||
FunctionPrefix string
|
||||
Files []*_escFile
|
||||
Dirs []*_escDir
|
||||
}
|
||||
|
||||
type _escFile struct {
|
||||
data []byte
|
||||
local string
|
||||
Name string
|
||||
BaseName string
|
||||
Data []byte
|
||||
Local string
|
||||
ModTime int64
|
||||
Compressed string
|
||||
|
||||
fileinfo os.FileInfo
|
||||
}
|
||||
|
||||
type _escDir struct {
|
||||
Name string
|
||||
BaseName string
|
||||
Local string
|
||||
ChildFileNames []string
|
||||
}
|
||||
|
||||
// Run executes a Config.
|
||||
func Run(conf *Config) error {
|
||||
func Run(conf *Config, out io.Writer) error {
|
||||
var err error
|
||||
if conf.ModTime != "" {
|
||||
i, err := strconv.ParseInt(conf.ModTime, 10, 64)
|
||||
@ -65,8 +87,9 @@ func Run(conf *Config) error {
|
||||
}
|
||||
modTime = &i
|
||||
}
|
||||
var fnames, dirnames []string
|
||||
content := make(map[string]_escFile)
|
||||
|
||||
alreadyPrepared := make(map[string]bool, 10)
|
||||
escFiles := make([]*_escFile, 0, 10)
|
||||
prefix := filepath.ToSlash(conf.Prefix)
|
||||
var ignoreRegexp *regexp.Regexp
|
||||
if conf.Ignore != "" {
|
||||
@ -82,6 +105,11 @@ func Run(conf *Config) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
gzipLevel := gzip.BestCompression
|
||||
if conf.NoCompression {
|
||||
gzipLevel = gzip.NoCompression
|
||||
}
|
||||
directories := make([]*_escDir, 0, 10)
|
||||
for _, base := range conf.Files {
|
||||
files := []string{base}
|
||||
for len(files) > 0 {
|
||||
@ -98,146 +126,126 @@ func Run(conf *Config) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fpath := filepath.ToSlash(fname)
|
||||
n := canonicFileName(fname, prefix)
|
||||
if fi.IsDir() {
|
||||
fis, err := f.Readdir(0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, fi := range fis {
|
||||
files = append(files, filepath.Join(fname, fi.Name()))
|
||||
dir := &_escDir{
|
||||
Name: n,
|
||||
BaseName: path.Base(n),
|
||||
Local: fpath,
|
||||
ChildFileNames: make([]string, 0, len(fis)),
|
||||
}
|
||||
for _, fi := range fis {
|
||||
childFName := filepath.Join(fname, fi.Name())
|
||||
files = append(files, childFName)
|
||||
if ignoreRegexp != nil && ignoreRegexp.MatchString(childFName) {
|
||||
continue
|
||||
}
|
||||
if includeRegexp == nil || includeRegexp.MatchString(childFName) {
|
||||
dir.ChildFileNames = append(dir.ChildFileNames, canonicFileName(filepath.Join(fname, fi.Name()), prefix))
|
||||
}
|
||||
}
|
||||
sort.Strings(dir.ChildFileNames)
|
||||
directories = append(directories, dir)
|
||||
} else if includeRegexp == nil || includeRegexp.MatchString(fname) {
|
||||
b, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "readAll return err")
|
||||
}
|
||||
if alreadyPrepared[n] {
|
||||
return fmt.Errorf("%s, %s: duplicate Name after prefix removal", n, fpath)
|
||||
}
|
||||
escFile := &_escFile{
|
||||
Name: n,
|
||||
BaseName: path.Base(n),
|
||||
Data: b,
|
||||
Local: fpath,
|
||||
fileinfo: fi,
|
||||
ModTime: fi.ModTime().Unix(),
|
||||
}
|
||||
if modTime != nil {
|
||||
escFile.ModTime = *modTime
|
||||
}
|
||||
if err := escFile.fillCompressed(gzipLevel); err != nil {
|
||||
return err
|
||||
}
|
||||
fpath := filepath.ToSlash(fname)
|
||||
n := strings.TrimPrefix(fpath, prefix)
|
||||
n = path.Join("/", n)
|
||||
if _, ok := content[n]; ok {
|
||||
return fmt.Errorf("%s, %s: duplicate name after prefix removal", n, fpath)
|
||||
}
|
||||
content[n] = _escFile{data: b, local: fpath, fileinfo: fi}
|
||||
fnames = append(fnames, n)
|
||||
escFiles = append(escFiles, escFile)
|
||||
alreadyPrepared[n] = true
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
}
|
||||
sort.Strings(fnames)
|
||||
w := new(bytes.Buffer)
|
||||
headerText, err := header(conf.Package, !(conf.Private))
|
||||
if nil != err {
|
||||
return fmt.Errorf("failed to expand autogenerated code: %s", err)
|
||||
|
||||
sort.Slice(escFiles, func(i, j int) bool { return strings.Compare(escFiles[i].Name, escFiles[j].Name) == -1 })
|
||||
sort.Slice(directories, func(i, j int) bool { return strings.Compare(directories[i].Name, directories[j].Name) == -1 })
|
||||
|
||||
functionPrefix := ""
|
||||
if conf.Private {
|
||||
functionPrefix = "_esc"
|
||||
}
|
||||
if _, err := w.Write(headerText); err != nil {
|
||||
return fmt.Errorf("failed to write output: %s", err)
|
||||
}
|
||||
dirs := map[string]bool{"/": true}
|
||||
gzipLevel := gzip.BestCompression
|
||||
if conf.NoCompression {
|
||||
gzipLevel = gzip.NoCompression
|
||||
}
|
||||
for _, fname := range fnames {
|
||||
f := content[fname]
|
||||
for b := path.Dir(fname); b != "/"; b = path.Dir(b) {
|
||||
dirs[b] = true
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
gw, err := gzip.NewWriterLevel(&buf, gzipLevel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := gw.Write(f.data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
t := f.fileinfo.ModTime().Unix()
|
||||
if modTime != nil {
|
||||
t = *modTime
|
||||
}
|
||||
fmt.Fprintf(w, `
|
||||
%q: {
|
||||
local: %q,
|
||||
size: %v,
|
||||
modtime: %v,
|
||||
compressed: %s,
|
||||
},%s`, fname, f.local, len(f.data), t, segment(&buf), "\n")
|
||||
}
|
||||
for d := range dirs {
|
||||
dirnames = append(dirnames, d)
|
||||
}
|
||||
sort.Strings(dirnames)
|
||||
for _, dir := range dirnames {
|
||||
local := path.Join(prefix, dir)
|
||||
if len(local) == 0 {
|
||||
local = "."
|
||||
}
|
||||
if local[0] == '/' {
|
||||
// Read dirs relative to the go proc's cwd vs system's
|
||||
// fs root.
|
||||
local = local[1:]
|
||||
}
|
||||
fmt.Fprintf(w, `
|
||||
%q: {
|
||||
isDir: true,
|
||||
local: %q,
|
||||
},%s`, dir, local, "\n")
|
||||
}
|
||||
w.WriteString(footer)
|
||||
out := os.Stdout
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
tmpl.Execute(buf, templateParams{
|
||||
Invocation: conf.Invocation,
|
||||
PackageName: conf.Package,
|
||||
FunctionPrefix: functionPrefix,
|
||||
Files: escFiles,
|
||||
Dirs: directories,
|
||||
})
|
||||
|
||||
fakeOutFileName := "static.go"
|
||||
if conf.OutputFile != "" {
|
||||
if out, err = os.Create(conf.OutputFile); err != nil {
|
||||
return err
|
||||
}
|
||||
fakeOutFileName = conf.OutputFile
|
||||
}
|
||||
if _, err := w.WriteTo(out); err != nil {
|
||||
return err
|
||||
}
|
||||
if conf.OutputFile != "" {
|
||||
return out.Close()
|
||||
|
||||
data, err := imports.Process(fakeOutFileName, buf.Bytes(), nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "imports.Process return error")
|
||||
}
|
||||
|
||||
fmt.Fprint(out, string(data))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func segment(s *bytes.Buffer) string {
|
||||
func canonicFileName(fname, prefix string) string {
|
||||
fpath := filepath.ToSlash(fname)
|
||||
return path.Join("/", strings.TrimPrefix(fpath, prefix))
|
||||
}
|
||||
|
||||
func (f *_escFile) fillCompressed(gzipLevel int) error {
|
||||
var buf bytes.Buffer
|
||||
gw, err := gzip.NewWriterLevel(&buf, gzipLevel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := gw.Write(f.Data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
b64 := base64.NewEncoder(base64.StdEncoding, &b)
|
||||
b64.Write(s.Bytes())
|
||||
b64.Write(buf.Bytes())
|
||||
b64.Close()
|
||||
res := "`\n"
|
||||
res := "\n"
|
||||
chunk := make([]byte, 80)
|
||||
for n, _ := b.Read(chunk); n > 0; n, _ = b.Read(chunk) {
|
||||
res += string(chunk[0:n]) + "\n"
|
||||
}
|
||||
return res + "`"
|
||||
}
|
||||
|
||||
func header(packageName string, enableExports bool) ([]byte, error) {
|
||||
functionPrefix := ""
|
||||
if !enableExports {
|
||||
functionPrefix = "_esc"
|
||||
}
|
||||
headerParams := headerTemplateParams{
|
||||
Invocation: strings.Join(os.Args[1:], " "),
|
||||
PackageName: packageName,
|
||||
FunctionPrefix: functionPrefix,
|
||||
}
|
||||
tmpl, err := template.New("").Parse(headerTemplate)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
var b bytes.Buffer
|
||||
err = tmpl.Execute(&b, headerParams)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
f.Compressed = res
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
const (
|
||||
headerTemplate = `// Code generated by "esc {{.Invocation}}"; DO NOT EDIT.
|
||||
fileTemplate = `// Code generated by "esc{{with .Invocation}} {{.}}{{end}}"; DO NOT EDIT.
|
||||
|
||||
package {{.PackageName}}
|
||||
|
||||
@ -339,9 +347,27 @@ func (f *_escFile) Close() error {
|
||||
}
|
||||
|
||||
func (f *_escFile) Readdir(count int) ([]os.FileInfo, error) {
|
||||
return nil, nil
|
||||
if !f.isDir {
|
||||
return nil, fmt.Errorf(" escFile.Readdir: '%s' is not directory", f.name)
|
||||
}
|
||||
|
||||
fis, ok := _escDirs[f.local]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf(" escFile.Readdir: '%s' is directory, but we have no info about content of this dir, local=%s", f.name, f.local)
|
||||
}
|
||||
limit := count
|
||||
if count <= 0 || limit > len(fis) {
|
||||
limit = len(fis)
|
||||
}
|
||||
|
||||
if len(fis) == 0 && count > 0 {
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
return fis[0:limit], nil
|
||||
}
|
||||
|
||||
|
||||
func (f *_escFile) Stat() (os.FileInfo, error) {
|
||||
return f, nil
|
||||
}
|
||||
@ -428,7 +454,33 @@ func {{.FunctionPrefix}}FSMustString(useLocal bool, name string) string {
|
||||
}
|
||||
|
||||
var _escData = map[string]*_escFile{
|
||||
`
|
||||
footer = `}
|
||||
{{ range .Files }}
|
||||
"{{ .Name }}": {
|
||||
name: "{{ .BaseName }}",
|
||||
local: "{{ .Local }}",
|
||||
size: {{ .Data | len }},
|
||||
modtime: {{ .ModTime }},
|
||||
compressed: ` + "`" + `{{ .Compressed }}` + "`" + `,
|
||||
},
|
||||
{{ end -}}
|
||||
{{ range .Dirs }}
|
||||
"{{ .Name }}": {
|
||||
name: "{{ .BaseName }}",
|
||||
local: ` + "`" + `{{ .Local }}` + "`" + `,
|
||||
isDir: true,
|
||||
},
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
var _escDirs = map[string][]os.FileInfo{
|
||||
{{ range .Dirs }}
|
||||
"{{ .Local }}": {
|
||||
{{ range .ChildFileNames -}}
|
||||
_escData["{{.}}"],
|
||||
{{ end }}
|
||||
},
|
||||
{{ end }}
|
||||
}
|
||||
|
||||
`
|
||||
)
|
||||
|
1202
vendor/vendor.json
vendored
Normal file
1202
vendor/vendor.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user