Add RestoreAsset/RestoreAssets methods

pull/4/head
Ryosuke IWANAGA 2014-12-08 09:30:11 +09:00
parent 9091c617ee
commit 0267f6e673
4 changed files with 82 additions and 1 deletions

View File

@ -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()

View File

@ -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.

View File

@ -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

62
restore.go Normal file
View File

@ -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
}