-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
38 lines (32 loc) · 797 Bytes
/
cache.go
File metadata and controls
38 lines (32 loc) · 797 Bytes
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
package main
import (
"os"
"path/filepath"
)
type CacheKey string
func LookupCache(key CacheKey) (*Resolved, bool, error) {
bin := cacheBinPath(key)
if _, err := os.Stat(bin); err == nil {
return &Resolved{Key: key, Binary: bin, WorkDir: cacheWorkDir(key)}, true, nil
} else if os.IsNotExist(err) {
return nil, false, nil
} else {
return nil, false, err
}
}
func cacheRoot() string {
if v := os.Getenv("XDG_CACHE_HOME"); v != "" {
return v
}
home, err := os.UserHomeDir()
if err != nil {
return "."
}
return filepath.Join(home, ".cache")
}
func cacheWorkDir(key CacheKey) string {
return filepath.Join(cacheRoot(), "goscript", "work", string(key))
}
func cacheBinPath(key CacheKey) string {
return filepath.Join(cacheRoot(), "goscript", "bin", string(key), "app")
}