/
utils.go
47 lines (43 loc) · 1.04 KB
/
utils.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
package gethwrappers
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
func Exit(msg string, err error) {
if err != nil {
fmt.Println(msg+":", err)
} else {
fmt.Println(msg)
}
os.Exit(1)
}
// GetProjectRoot returns the root of the chainlink project
func GetProjectRoot() (rootPath string) {
root, err := os.Getwd()
if err != nil {
Exit("could not get current working directory while seeking project root",
err)
}
for root != "/" { // Walk up path to find dir containing go.mod
if _, err := os.Stat(filepath.Join(root, "go.mod")); os.IsNotExist(err) {
root = filepath.Dir(root)
} else {
return root
}
}
Exit("could not find project root", nil)
panic("can't get here")
}
func TempDir(dirPrefix string) (string, func()) {
tmpDir, err := ioutil.TempDir("", dirPrefix+"-contractWrapper")
if err != nil {
Exit("failed to create temporary working directory", err)
}
return tmpDir, func() {
if err := os.RemoveAll(tmpDir); err != nil {
fmt.Println("failure while cleaning up temporary working directory:", err)
}
}
}