-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.go
More file actions
56 lines (45 loc) · 1.28 KB
/
Copy pathcache.go
File metadata and controls
56 lines (45 loc) · 1.28 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/dustin/go-humanize"
)
func clearCache(dir string) error {
cacheSize := dirSize(dir)
cacheSizeFmt := humanize.Bytes(uint64((cacheSize)))
if cacheSize == 0 {
fmt.Println("Cache directory is empty, nothing to clear")
return nil
}
if !confirm(fmt.Sprintf("Cache size in %s: %s.\nClear cache?", filepath.Base(dir), red(cacheSizeFmt))) {
return nil
}
if err := os.RemoveAll(dir); err != nil {
return fmt.Errorf("failed to clear cache directory: %w", err)
}
fmt.Printf("=> Cleared %s of cache\n", red(cacheSizeFmt))
return nil
}
func clearPkgCache(name, tag string) error {
reg, err := loadRegistry()
if err != nil {
return err
}
pkg, ok := reg.Packages[name]
if !ok {
return fmt.Errorf("package %s is not installed", name)
}
if pkg.SourceType == "local" {
return fmt.Errorf("cant clear cache of a local package")
}
if tag != "" && (strings.ContainsAny(tag, `/\`) || strings.Contains(tag, "..")) {
return fmt.Errorf("invalid tag: %s contains path separators or traversal components", tag)
}
cachePath := pkg.ResolveCachePath()
if cachePath == "" {
return fmt.Errorf("cannot resolve cache path for package source type: %s", pkg.SourceType)
}
return clearCache(filepath.Join(cachePath, tag))
}