diff options
Diffstat (limited to 'internal/cli/cli.go')
| -rw-r--r-- | internal/cli/cli.go | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/internal/cli/cli.go b/internal/cli/cli.go index a13f2f1..5c05713 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -39,6 +39,7 @@ Flags: -l, --lectionary WHICH new|trad (trad -> traditional) -g, --lang LANG traditional lectionary language: pl|en -u, --update harvest sigla maximally to the horizon (idempotent) + -C, --clean prune cached readings older than a year -v, --version print the version and exit -h, --help this help @@ -97,7 +98,7 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { return 2 } - var all, raw, refresh, offline, update bool + var all, raw, refresh, offline, update, clean bool var bibleVer, compareList, lectionary, lang string var width int @@ -125,6 +126,8 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { fs.StringVar(&lang, "lang", "", "pl|en") fs.BoolVar(&update, "u", false, "harvest sigla maximally to the horizon") fs.BoolVar(&update, "update", false, "harvest sigla maximally to the horizon") + fs.BoolVar(&clean, "C", false, "prune cached readings older than a year") + fs.BoolVar(&clean, "clean", false, "prune cached readings older than a year") if err := fs.Parse(rest); err != nil { return 2 @@ -144,6 +147,9 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { return 2 } + if clean { + return runClean(stdout, stderr) + } if update { return runHarvest(date, stdout, stderr) } @@ -247,6 +253,49 @@ func runHarvest(date string, stdout, stderr io.Writer) int { return 0 } +// runClean handles -C/--clean: prune cached readings older than one year +// (relative to today()) and print a human-readable summary of what was +// removed. Like -u/--update, this is a maintenance mode -- it ignores DATE +// and every render flag, and is dispatched before the render paths. If both +// --clean and -u/--update are given, --clean takes precedence (see Run). +func runClean(stdout, stderr io.Writer) int { + now, err := time.Parse("2006-01-02", today()) + if err != nil { + fmt.Fprintln(stderr, "lectio:", err) + return 1 + } + before := now.AddDate(-1, 0, 0) + + removed, freed, err := liturgy.CleanCache(before) + if err != nil { + fmt.Fprintln(stderr, "lectio:", err) + return 1 + } + + cutoff := before.Format("2006-01-02") + if removed == 0 { + fmt.Fprintf(stdout, "cache already clean (nothing older than %s)\n", cutoff) + return 0 + } + entries := "entries" + if removed == 1 { + entries = "entry" + } + fmt.Fprintf(stdout, "cleaned %d cache %s older than %s (freed %s)\n", removed, entries, cutoff, formatFreed(freed)) + return 0 +} + +// formatFreed renders a byte count the way -C/--clean's summary line wants +// it: megabytes with one decimal once it's a meaningful size, kilobytes +// (also one decimal) for anything smaller. +func formatFreed(bytes int64) string { + const mb = 1024 * 1024 + if bytes >= mb { + return fmt.Sprintf("%.1f MB", float64(bytes)/mb) + } + return fmt.Sprintf("%.1f KB", float64(bytes)/1024) +} + // fetchAndPrint is the shared single-version render path (default version or // -b/--bible): fetch via the readings router, apply the offline version // swap, then render each section's heading and render.GatherVersion blocks. |
