2019-11-01 23:08:10 +03:00
|
|
|
package pkgtest
|
2019-10-26 00:11:34 +03:00
|
|
|
|
|
|
|
import (
|
2019-11-01 00:20:15 +03:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io/ioutil"
|
2019-10-26 00:11:34 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-10-31 18:06:20 +03:00
|
|
|
"runtime"
|
2019-10-26 00:11:34 +03:00
|
|
|
|
|
|
|
"github.com/markbates/pkger/here"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Ref struct {
|
|
|
|
here.Info
|
2019-11-10 16:11:07 +03:00
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
func NewRef() (*Ref, error) {
|
|
|
|
her, err := here.Package("github.com/markbates/pkger")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-01 00:20:15 +03:00
|
|
|
root := filepath.Join(
|
2019-10-30 18:31:39 +03:00
|
|
|
her.Module.Dir,
|
2019-10-26 00:11:34 +03:00
|
|
|
"pkging",
|
2019-11-01 23:08:10 +03:00
|
|
|
"pkgtest",
|
2019-10-26 00:11:34 +03:00
|
|
|
"testdata",
|
2019-11-10 16:11:07 +03:00
|
|
|
"ref",
|
2019-11-11 01:22:01 +03:00
|
|
|
)
|
2019-11-10 16:11:07 +03:00
|
|
|
|
|
|
|
return newRef(root)
|
|
|
|
}
|
2019-10-26 00:11:34 +03:00
|
|
|
|
2019-11-10 16:11:07 +03:00
|
|
|
func newRef(root string) (*Ref, error) {
|
2019-11-01 00:20:15 +03:00
|
|
|
if _, err := os.Stat(root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]byte, 10)
|
2019-11-10 16:11:07 +03:00
|
|
|
_, err := rand.Read(b)
|
2019-11-01 00:20:15 +03:00
|
|
|
if err != nil {
|
2019-10-26 00:11:34 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-01 00:20:15 +03:00
|
|
|
dir := filepath.Dir(root)
|
|
|
|
dir = filepath.Join(dir, hex.EncodeToString(b))
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
ref := &Ref{
|
|
|
|
Info: here.Info{
|
|
|
|
ImportPath: "app",
|
|
|
|
Dir: dir,
|
2019-10-31 18:06:20 +03:00
|
|
|
Name: "app",
|
2019-10-30 18:31:39 +03:00
|
|
|
Module: here.Module{
|
2019-10-31 18:06:20 +03:00
|
|
|
Main: true,
|
|
|
|
Path: "app",
|
|
|
|
Dir: dir,
|
|
|
|
GoMod: filepath.Join(dir, "go.mod"),
|
|
|
|
GoVersion: runtime.Version(),
|
2019-10-30 18:31:39 +03:00
|
|
|
},
|
2019-10-26 00:11:34 +03:00
|
|
|
},
|
2019-11-10 16:11:07 +03:00
|
|
|
|
|
|
|
root: root,
|
2019-10-26 00:11:34 +03:00
|
|
|
}
|
|
|
|
|
2019-11-01 00:20:15 +03:00
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-21 05:58:48 +03:00
|
|
|
for _, n := range []string{"go.mod", "go.sum"} {
|
|
|
|
b, err = ioutil.ReadFile(filepath.Join(root, n))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(filepath.Join(dir, n))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := f.Write(b); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-01 00:20:15 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 00:11:34 +03:00
|
|
|
return ref, nil
|
|
|
|
}
|