-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.go
96 lines (88 loc) · 1.95 KB
/
path.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
92
93
94
95
96
package fleet
import (
"errors"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/KarpelesLab/goupd"
)
func (a *Agent) initPath() {
if goupd.PROJECT_NAME != "unconfigured" {
// chdir to cache
c, err := os.UserCacheDir()
if err != nil {
panic(err)
}
c = filepath.Join(c, goupd.PROJECT_NAME)
if err := EnsureDir(c); err != nil {
log.Printf("[fleet] Failed to access cache directory: %s", err)
return
}
log.Printf("[fleet] set cache dir: %s", c)
os.Chdir(c)
}
}
func EnsureDir(c string) error {
inf, err := os.Stat(c)
if err != nil && os.IsNotExist(err) {
err = os.MkdirAll(c, 0755)
if err != nil {
return err
}
} else if err != nil {
return err
} else if err == nil && !inf.IsDir() {
return errors.New("error: file exists at directory location")
}
return nil
}
func findFile(filename string) (string, error) {
// locate file
if exe, err := os.Executable(); err == nil {
p := filepath.Join(filepath.Dir(exe), filename)
if _, err := os.Stat(p); err == nil {
return p, nil
}
}
if cwd, err := os.Getwd(); err == nil {
p := filepath.Join(cwd, filename)
if _, err = os.Stat(p); err == nil {
return p, nil
}
}
p := filepath.Join("/etc/fleet", filename)
if _, err := os.Stat(p); err == nil {
return p, nil
}
return "", fs.ErrNotExist
}
func (a *Agent) getFile(filename string, cb func([]byte) error) error {
if a.GetFile != nil {
v, err := a.GetFile(a, filename)
if err != nil {
return err
}
return cb(v)
}
fn, err := findFile(filename)
if err != nil {
log.Printf("[fleet] Failed to locate %s: %s", filename, err)
return err
}
log.Printf("[fleet] located file %s", fn)
data, err := ioutil.ReadFile(fn)
if err != nil {
log.Printf("[fleet] Failed to read %s: %s", filename, err)
return err
}
err = cb(data)
if err != nil {
log.Printf("[fleet] Failed to perform %s: %s", filename, err)
return err
}
// only remove after success of callback
os.Remove(fn)
return nil
}