From b5d9eff767e285dc16b2d5fe5a27226531ebaefa Mon Sep 17 00:00:00 2001 From: Tobias Schottdorf Date: Tue, 10 Mar 2015 15:41:54 +0100 Subject: [PATCH] add `mode` and `modTime` flags In some situations, it is desired to have reproducible runs of `go-bindata` from a fixed git repository commit. Since mtimes and file modes are not generally versioned, the newly introduced flags aid in that quest by allowing to completely replace those two pieces of data by something fixed. Example: //go:generate go-bindata -pkg resource -mode 0644 -modtime 1388530800 \ -o ./embedded.go ./ui/... --- config.go | 5 +++++ go-bindata/main.go | 2 ++ release.go | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index d43ef12..8c58b51 100644 --- a/config.go +++ b/config.go @@ -127,6 +127,11 @@ type Config struct { // repository. Dev bool + // When nonzero, use this as mode for all files. + Mode uint + // When nonzero, use this as unix timestamp for all files. + ModTime int64 + // Ignores any filenames matching the regex pattern specified, e.g. // path/to/file.ext will ignore only that file, or \\.gitignore // will match any .gitignore file. diff --git a/go-bindata/main.go b/go-bindata/main.go index f2e00e9..55d9dc5 100644 --- a/go-bindata/main.go +++ b/go-bindata/main.go @@ -47,6 +47,8 @@ func parseArgs() *bindata.Config { flag.StringVar(&c.Package, "pkg", c.Package, "Package name to use in the generated code.") flag.BoolVar(&c.NoMemCopy, "nomemcopy", c.NoMemCopy, "Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.") flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.") + flag.UintVar(&c.Mode, "mode", c.Mode, "Optional file mode override for all files.") + flag.Int64Var(&c.ModTime, "modtime", c.ModTime, "Optional modification unix timestamp override for all files.") flag.StringVar(&c.Output, "o", c.Output, "Optional name of the output file to be generated.") flag.BoolVar(&version, "version", false, "Displays version information.") diff --git a/release.go b/release.go index dbac762..b58d579 100644 --- a/release.go +++ b/release.go @@ -81,7 +81,7 @@ func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error { if err != nil { return err } - return asset_release_common(w, asset) + return asset_release_common(w, c, asset) } // sanitize prepares a valid UTF-8 string as a raw string constant. @@ -359,12 +359,21 @@ func %s_bytes() ([]byte, error) { return err } -func asset_release_common(w io.Writer, asset *Asset) error { +func asset_release_common(w io.Writer, c *Config, asset *Asset) error { fi, err := os.Stat(asset.Path) if err != nil { return err } + mode := uint(fi.Mode()) + modTime := fi.ModTime().Unix() + + if c.Mode > 0 { + mode = uint(os.ModePerm) & c.Mode + } + if c.ModTime > 0 { + modTime = c.ModTime + } _, err = fmt.Fprintf(w, `func %s() (*asset, error) { bytes, err := %s_bytes() if err != nil { @@ -376,6 +385,6 @@ func asset_release_common(w io.Writer, asset *Asset) error { return a, nil } -`, asset.Func, asset.Func, asset.Name, fi.Size(), uint32(fi.Mode()), fi.ModTime().Unix()) +`, asset.Func, asset.Func, asset.Name, fi.Size(), mode, modTime) return err }