github.com/nozzle/golangci-lint@v1.49.0-nz3/pkg/commands/cache.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/spf13/cobra" 9 10 "github.com/golangci/golangci-lint/internal/cache" 11 "github.com/golangci/golangci-lint/pkg/fsutils" 12 "github.com/golangci/golangci-lint/pkg/logutils" 13 ) 14 15 func (e *Executor) initCache() { 16 cacheCmd := &cobra.Command{ 17 Use: "cache", 18 Short: "Cache control and information", 19 Args: cobra.NoArgs, 20 RunE: func(cmd *cobra.Command, _ []string) error { 21 return cmd.Help() 22 }, 23 } 24 e.rootCmd.AddCommand(cacheCmd) 25 26 cacheCmd.AddCommand(&cobra.Command{ 27 Use: "clean", 28 Short: "Clean cache", 29 Args: cobra.NoArgs, 30 ValidArgsFunction: cobra.NoFileCompletions, 31 RunE: e.executeCleanCache, 32 }) 33 cacheCmd.AddCommand(&cobra.Command{ 34 Use: "status", 35 Short: "Show cache status", 36 Args: cobra.NoArgs, 37 ValidArgsFunction: cobra.NoFileCompletions, 38 Run: e.executeCacheStatus, 39 }) 40 41 // TODO: add trim command? 42 } 43 44 func (e *Executor) executeCleanCache(_ *cobra.Command, _ []string) error { 45 cacheDir := cache.DefaultDir() 46 if err := os.RemoveAll(cacheDir); err != nil { 47 return fmt.Errorf("failed to remove dir %s: %w", cacheDir, err) 48 } 49 50 return nil 51 } 52 53 func (e *Executor) executeCacheStatus(_ *cobra.Command, _ []string) { 54 cacheDir := cache.DefaultDir() 55 fmt.Fprintf(logutils.StdOut, "Dir: %s\n", cacheDir) 56 57 cacheSizeBytes, err := dirSizeBytes(cacheDir) 58 if err == nil { 59 fmt.Fprintf(logutils.StdOut, "Size: %s\n", fsutils.PrettifyBytesCount(cacheSizeBytes)) 60 } 61 } 62 63 func dirSizeBytes(path string) (int64, error) { 64 var size int64 65 err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error { 66 if err == nil && !info.IsDir() { 67 size = info.Size() 68 } 69 return err 70 }) 71 return size, err 72 }