-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin.go
71 lines (60 loc) · 1.56 KB
/
plugin.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
package main
import (
"os"
"path/filepath"
"time"
"github.com/drone/drone-cache-lib/archive/tar"
"github.com/drone/drone-cache-lib/cache"
"github.com/drone/drone-cache-lib/storage"
"github.com/sirupsen/logrus"
)
type plugin struct {
mount []string
path string
file string
fallback string
rebuild bool
restore bool
flush bool
ttl time.Duration
storage storage.Storage
}
// Exec runs the plugin
func (p *plugin) Exec() error {
encoder := tar.New()
cacher := cache.New(p.storage, encoder)
if p.rebuild {
dir, _ := filepath.Split(p.file)
_ = os.MkdirAll(dir, 0700)
logrus.Infof("Rebuilding cache at %s", p.file)
if err := cacher.Rebuild(p.mount, p.file); err == nil {
logrus.Infof("Cache rebuilt")
} else {
logrus.Warnf("Error rebuilding cache. %s", err)
}
}
if p.restore {
logrus.Infof("Restoring cache from %s", p.file)
if err := cacher.Restore(p.file, p.fallback); err == nil {
logrus.Info("Cache restored")
} else {
logrus.Warningf("Error restoring cache %v", err)
_ = p.storage.Delete(p.file)
_ = p.storage.Delete(p.fallback)
}
}
if p.flush && p.ttl > 0 {
logrus.Infof("Purging cached items older then %v", p.ttl)
flusher := cache.NewFlusher(p.storage, testExpired(p.ttl))
if ferr := flusher.Flush(p.path); ferr != nil {
logrus.Warnf("Error purging cache. %s", ferr)
}
}
return nil
}
// Check if older then x days (default 30 days)
func testExpired(ttl time.Duration) cache.DirtyFunc {
return func(file storage.FileEntry) bool {
return file.LastModified.Before(time.Now().Add(-ttl))
}
}