-
Notifications
You must be signed in to change notification settings - Fork 12
/
stub.go
51 lines (45 loc) · 980 Bytes
/
stub.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
package aqua
import (
"errors"
"github.com/kardianos/osext"
"io/ioutil"
"os"
"strings"
)
func getContent(path string) (string, error) {
var absPath string
var exists bool
// if absolute path, try to find the file directly
if strings.HasPrefix(path, "/") {
absPath = path
_, err := os.Stat(absPath)
exists = err == nil
}
// try to locate it in working directory
if !exists {
wdir, ferr := os.Getwd()
if ferr == nil {
absPath = removeMultSlashes(wdir + "/" + path)
_, err := os.Stat(absPath)
exists = err == nil
}
}
// try to locate it in executable directory
if !exists {
edir, ferr := osext.ExecutableFolder()
if ferr == nil {
absPath = removeMultSlashes(edir + "/" + path)
_, err := os.Stat(absPath)
exists = err == nil
}
}
if !exists {
return "", errors.New("File not found in working/exe path: " + path)
}
b, err := ioutil.ReadFile(absPath)
if err != nil {
return "", err
} else {
return string(b), nil
}
}