bindata/restore.go

64 lines
1.7 KiB
Go
Raw Normal View History

2014-12-08 00:30:11 +00:00
// 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, `
2015-05-11 19:44:41 +00:00
// RestoreAsset restores an asset under the given directory
2014-12-08 00:30:11 +00:00
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, filepath.Dir(name)), os.FileMode(0755))
2014-12-08 00:30:11 +00:00
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
}
2015-05-11 19:44:41 +00:00
// RestoreAssets restores an asset under the given directory recursively
2014-12-08 00:30:11 +00:00
func RestoreAssets(dir, name string) error {
children, err := AssetDir(name)
2015-05-11 19:47:48 +00:00
// File
if err != nil {
2014-12-08 00:30:11 +00:00
return RestoreAsset(dir, name)
2015-05-11 19:47:48 +00:00
}
// Dir
for _, child := range children {
err = RestoreAssets(dir, filepath.Join(name, child))
2015-05-11 19:47:48 +00:00
if err != nil {
return err
2014-12-08 00:30:11 +00:00
}
}
return nil
}
func _filePath(dir, name string) string {
cannonicalName := strings.Replace(name, "\\", "/", -1)
return filepath.Join(append([]string{dir}, strings.Split(cannonicalName, "/")...)...)
}
`)
return err
}