This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ref.go
91 lines (75 loc) · 1.4 KB
/
ref.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package pkgtest
import (
"crypto/rand"
"encoding/hex"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/markbates/pkger/here"
)
type Ref struct {
here.Info
root string
}
func NewRef() (*Ref, error) {
her, err := here.Package("github.com/markbates/pkger")
if err != nil {
return nil, err
}
root := filepath.Join(
her.Module.Dir,
"pkging",
"pkgtest",
"testdata",
"ref",
)
return newRef(root)
}
func newRef(root string) (*Ref, error) {
if _, err := os.Stat(root); err != nil {
return nil, err
}
b := make([]byte, 10)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
dir := filepath.Dir(root)
dir = filepath.Join(dir, hex.EncodeToString(b))
ref := &Ref{
Info: here.Info{
ImportPath: "app",
Dir: dir,
Name: "app",
Module: here.Module{
Main: true,
Path: "app",
Dir: dir,
GoMod: filepath.Join(dir, "go.mod"),
GoVersion: runtime.Version(),
},
},
root: root,
}
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
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
}
}
return ref, nil
}