-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcache.go
85 lines (72 loc) · 1.9 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
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
package commands
import (
"fmt"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/golangci/golangci-lint/internal/cache"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
)
func (e *Executor) initCache() {
cacheCmd := &cobra.Command{
Use: "cache",
Short: "Cache control and information",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
e.log.Fatalf("Usage: golangci-lint cache")
}
if err := cmd.Help(); err != nil {
e.log.Fatalf("Can't run cache: %s", err)
}
},
}
e.rootCmd.AddCommand(cacheCmd)
cacheCmd.AddCommand(&cobra.Command{
Use: "clean",
Short: "Clean cache",
Run: e.executeCleanCache,
})
cacheCmd.AddCommand(&cobra.Command{
Use: "status",
Short: "Show cache status",
Run: e.executeCacheStatus,
})
// TODO: add trim command?
}
func (e *Executor) executeCleanCache(_ *cobra.Command, args []string) {
if len(args) != 0 {
e.log.Fatalf("Usage: golangci-lint cache clean")
}
cacheDir := cache.DefaultDir()
if err := os.RemoveAll(cacheDir); err != nil {
e.log.Fatalf("Failed to remove dir %s: %s", cacheDir, err)
}
os.Exit(exitcodes.Success)
}
func (e *Executor) executeCacheStatus(_ *cobra.Command, args []string) {
if len(args) != 0 {
e.log.Fatalf("Usage: golangci-lint cache status")
}
cacheDir := cache.DefaultDir()
fmt.Fprintf(logutils.StdOut, "Dir: %s\n", cacheDir)
cacheSizeBytes, err := dirSizeBytes(cacheDir)
if err == nil {
fmt.Fprintf(logutils.StdOut, "Size: %s\n", fsutils.PrettifyBytesCount(cacheSizeBytes))
}
os.Exit(exitcodes.Success)
}
func dirSizeBytes(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}