-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
cache.go
58 lines (51 loc) · 1.16 KB
/
cache.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
package runn
import (
"fmt"
"os"
"path/filepath"
)
var globalCacheDir string
// SetCacheDir set cache directory for remote runbooks.
func SetCacheDir(dir string) error {
if dir == "" {
globalCacheDir = dir
return nil
}
if globalCacheDir != "" && dir != globalCacheDir {
return fmt.Errorf("duplicate cache dir: %s %s", dir, globalCacheDir)
}
if _, err := os.Stat(dir); err == nil {
return fmt.Errorf("%s already exists", dir)
}
globalCacheDir = filepath.Clean(dir)
return nil
}
// RemoveCacheDir remove cache directory for remote runbooks.
func RemoveCacheDir() error {
if globalCacheDir == "" {
return nil
}
return os.RemoveAll(globalCacheDir)
}
func cacheDirOrCreate() (string, error) {
if globalCacheDir != "" {
if _, err := os.Stat(globalCacheDir); err != nil {
if err := os.MkdirAll(globalCacheDir, os.ModePerm); err != nil {
return "", err
}
}
return globalCacheDir, nil
}
dir, err := os.MkdirTemp("", "runn")
if err != nil {
return "", err
}
globalCacheDir = dir
return dir, nil
}
func cacheDir() (string, error) {
if globalCacheDir != "" {
return globalCacheDir, nil
}
return "", fmt.Errorf("cache directory is not set")
}