mirror of
https://github.com/restic/restic.git
synced 2024-12-25 01:06:39 +00:00
Merge pull request #2033 from j6s/feature.cache-size
Output directory size in cache command
This commit is contained in:
commit
7b91c40e21
1 changed files with 34 additions and 0 deletions
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
@ -29,6 +30,7 @@ The "cache" command allows listing and cleaning local cache directories.
|
||||||
type CacheOptions struct {
|
type CacheOptions struct {
|
||||||
Cleanup bool
|
Cleanup bool
|
||||||
MaxAge uint
|
MaxAge uint
|
||||||
|
NoSize bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var cacheOptions CacheOptions
|
var cacheOptions CacheOptions
|
||||||
|
@ -39,6 +41,7 @@ func init() {
|
||||||
f := cmdCache.Flags()
|
f := cmdCache.Flags()
|
||||||
f.BoolVar(&cacheOptions.Cleanup, "cleanup", false, "remove old cache directories")
|
f.BoolVar(&cacheOptions.Cleanup, "cleanup", false, "remove old cache directories")
|
||||||
f.UintVar(&cacheOptions.MaxAge, "max-age", 30, "max age in `days` for cache directories to be considered old")
|
f.UintVar(&cacheOptions.MaxAge, "max-age", 30, "max age in `days` for cache directories to be considered old")
|
||||||
|
f.BoolVar(&cacheOptions.NoSize, "no-size", false, "do not output the size of the cache directories")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
||||||
|
@ -92,12 +95,17 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
||||||
ID string
|
ID string
|
||||||
Last string
|
Last string
|
||||||
Old string
|
Old string
|
||||||
|
Size string
|
||||||
}
|
}
|
||||||
|
|
||||||
tab.AddColumn("Repo ID", "{{ .ID }}")
|
tab.AddColumn("Repo ID", "{{ .ID }}")
|
||||||
tab.AddColumn("Last Used", "{{ .Last }}")
|
tab.AddColumn("Last Used", "{{ .Last }}")
|
||||||
tab.AddColumn("Old", "{{ .Old }}")
|
tab.AddColumn("Old", "{{ .Old }}")
|
||||||
|
|
||||||
|
if !opts.NoSize {
|
||||||
|
tab.AddColumn("Size", "{{ .Size }}")
|
||||||
|
}
|
||||||
|
|
||||||
dirs, err := cache.All(cachedir)
|
dirs, err := cache.All(cachedir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -118,10 +126,20 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
||||||
old = "yes"
|
old = "yes"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var size string
|
||||||
|
if !opts.NoSize {
|
||||||
|
bytes, err := dirSize(filepath.Join(cachedir, entry.Name()))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
size = fmt.Sprintf("%11s", formatBytes(uint64(bytes)))
|
||||||
|
}
|
||||||
|
|
||||||
tab.AddRow(data{
|
tab.AddRow(data{
|
||||||
entry.Name()[:10],
|
entry.Name()[:10],
|
||||||
fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)),
|
fmt.Sprintf("%d days ago", uint(time.Since(entry.ModTime()).Hours()/24)),
|
||||||
old,
|
old,
|
||||||
|
size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,3 +148,19 @@ func runCache(opts CacheOptions, gopts GlobalOptions, args []string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dirSize(path string) (int64, error) {
|
||||||
|
var size int64
|
||||||
|
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil || info == nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
size += info.Size()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
return size, err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue