From 0267f6e673233d00a83634be4ae01059d8d712e7 Mon Sep 17 00:00:00 2001 From: Ryosuke IWANAGA Date: Mon, 8 Dec 2014 09:30:11 +0900 Subject: [PATCH] Add RestoreAsset/RestoreAssets methods --- convert.go | 7 +++++- debug.go | 2 ++ release.go | 12 +++++++++++ restore.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 restore.go diff --git a/convert.go b/convert.go index 410d1de..0d41ea0 100644 --- a/convert.go +++ b/convert.go @@ -77,7 +77,12 @@ func Translate(c *Config) error { return err } // Write hierarchical tree of assets - return writeTOCTree(bfd, toc) + if err := writeTOCTree(bfd, toc); err != nil { + return err + } + + // Write restore procedure + return writeRestore(bfd) } // Implement sort.Interface for []os.FileInfo based on Name() diff --git a/debug.go b/debug.go index 03823c6..884b4df 100644 --- a/debug.go +++ b/debug.go @@ -34,6 +34,8 @@ func writeDebugHeader(w io.Writer) error { "io/ioutil" "strings" "os" + "path" + "path/filepath" ) // bindata_read reads the given file from disk. It returns an error on failure. diff --git a/release.go b/release.go index cb8db86..dbac762 100644 --- a/release.go +++ b/release.go @@ -108,6 +108,9 @@ func header_compressed_nomemcopy(w io.Writer) error { "unsafe" "os" "time" + "io/ioutil" + "path" + "path/filepath" ) func bindata_read(data, name string) ([]byte, error) { @@ -148,6 +151,9 @@ func header_compressed_memcopy(w io.Writer) error { "strings" "os" "time" + "io/ioutil" + "path" + "path/filepath" ) func bindata_read(data []byte, name string) ([]byte, error) { @@ -179,6 +185,9 @@ func header_uncompressed_nomemcopy(w io.Writer) error { "unsafe" "os" "time" + "io/ioutil" + "path" + "path/filepath" ) func bindata_read(data, name string) ([]byte, error) { @@ -202,6 +211,9 @@ func header_uncompressed_memcopy(w io.Writer) error { "strings" "os" "time" + "io/ioutil" + "path" + "path/filepath" ) `) return err diff --git a/restore.go b/restore.go new file mode 100644 index 0000000..8fe8066 --- /dev/null +++ b/restore.go @@ -0,0 +1,62 @@ +// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication +// license. Its contents can be found at: +// http://creativecommons.org/publicdomain/zero/1.0/ + +package bindata + +import ( + "fmt" + "io" +) + +func writeRestore(w io.Writer) error { + _, err := fmt.Fprintf(w, ` +// Restore an asset under the given directory +func RestoreAsset(dir, name string) error { + data, err := Asset(name) + if err != nil { + return err + } + info, err := AssetInfo(name) + if err != nil { + return err + } + err = os.MkdirAll(_filePath(dir, path.Dir(name)), os.FileMode(0755)) + if err != nil { + return err + } + err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + if err != nil { + return err + } + err = os.Chtimes(_filePath(dir, name), info.ModTime(), info.ModTime()) + if err != nil { + return err + } + return nil +} + +// Restore assets under the given directory recursively +func RestoreAssets(dir, name string) error { + children, err := AssetDir(name) + if err != nil { // File + return RestoreAsset(dir, name) + } else { // Dir + for _, child := range children { + err = RestoreAssets(dir, path.Join(name, child)) + if err != nil { + return err + } + } + } + return nil +} + +func _filePath(dir, name string) string { + cannonicalName := strings.Replace(name, "\\", "/", -1) + return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...) +} + +`) + return err +}