pkger/pkging/pkgtest/load_ref.go

154 lines
2.5 KiB
Go
Raw Normal View History

2019-11-01 23:08:10 +03:00
package pkgtest
2019-10-30 23:39:05 +03:00
import (
2019-11-01 00:20:15 +03:00
"fmt"
2019-10-30 23:39:05 +03:00
"io"
"os"
"path/filepath"
"strings"
"github.com/markbates/pkger/here"
2019-10-30 23:39:05 +03:00
"github.com/markbates/pkger/pkging"
)
2019-11-01 22:53:54 +03:00
func LoadFile(name string, ref *Ref, pkg pkging.Pkger) (os.FileInfo, error) {
root := filepath.Join(ref.root, name)
2019-11-01 00:20:15 +03:00
info, err := os.Stat(root)
if err != nil {
return nil, err
}
if info.IsDir() {
return nil, fmt.Errorf("%s is a directory", name)
}
_, err = pkg.Current()
2019-11-01 00:20:15 +03:00
if err != nil {
return nil, err
}
af, err := os.Open(root)
if err != nil {
return nil, err
}
defer af.Close()
bf, err := pkg.Create(name)
if err != nil {
return nil, err
}
defer bf.Close()
2019-11-01 22:53:54 +03:00
xp := strings.TrimPrefix(root, filepath.Dir(root))
xp = filepath.Join(ref.Dir, xp)
cf, err := os.Create(xp)
if err != nil {
return nil, err
}
defer cf.Close()
mw := io.MultiWriter(bf, cf)
_, err = io.Copy(mw, af)
if err != nil {
2019-11-01 00:20:15 +03:00
return nil, err
}
return info, nil
}
func LoadFiles(name string, ref *Ref, pkg pkging.Pkger) ([]os.FileInfo, error) {
var infos []os.FileInfo
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
}
root := filepath.Join(ref.root, name)
2019-11-01 00:20:15 +03:00
info, err := os.Stat(root)
if err != nil {
return nil, err
}
if !info.IsDir() {
return nil, fmt.Errorf("%s is not a directory", name)
}
her, err = pkg.Current()
if err != nil {
return nil, err
}
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
2019-10-30 23:39:05 +03:00
if err != nil {
return err
}
2019-11-01 00:20:15 +03:00
if strings.HasPrefix(filepath.Base(path), ".") {
return nil
}
infos = append(infos, info)
xp := strings.TrimPrefix(path, root)
xp = filepath.Join(name, xp)
pt, err := pkg.Parse(xp)
2019-10-30 23:39:05 +03:00
if err != nil {
return err
}
2019-11-01 00:20:15 +03:00
if info.IsDir() {
if err := pkg.MkdirAll(pt.Name, info.Mode()); err != nil {
return err
}
if err := os.MkdirAll(filepath.Join(her.Dir, xp), info.Mode()); err != nil {
return err
}
return nil
2019-10-30 23:39:05 +03:00
}
2019-11-01 00:20:15 +03:00
af, err := os.Open(path)
2019-10-30 23:39:05 +03:00
if err != nil {
return err
}
2019-11-01 00:20:15 +03:00
defer af.Close()
2019-10-30 23:39:05 +03:00
2019-11-01 00:20:15 +03:00
bf, err := pkg.Create(pt.Name)
if err != nil {
2019-10-30 23:39:05 +03:00
return err
}
2019-11-01 00:20:15 +03:00
defer bf.Close()
2019-10-30 23:39:05 +03:00
2019-11-01 00:20:15 +03:00
xp = filepath.Join(her.Dir, xp)
cf, err := os.Create(xp)
2019-10-30 23:39:05 +03:00
if err != nil {
return err
}
2019-11-01 00:20:15 +03:00
defer cf.Close()
2019-10-30 23:39:05 +03:00
2019-11-01 00:20:15 +03:00
mw := io.MultiWriter(bf, cf)
_, err = io.Copy(mw, af)
if err != nil {
2019-10-30 23:39:05 +03:00
return err
}
2019-11-01 00:20:15 +03:00
2019-10-30 23:39:05 +03:00
return nil
})
2019-11-01 00:20:15 +03:00
if err != nil {
return nil, err
}
if len(infos) == 0 {
return nil, fmt.Errorf("did not load any infos for %s", name)
}
return infos, nil
2019-10-30 23:39:05 +03:00
}