Skip to content

moul/rrgc

rrgc

πŸ—‘ round-robin garbage-collector

go.dev reference License GitHub release Docker Metrics Made by Manfred Touron n Go Release PR GolangCI codecov Go Report Card CodeFactor

Gitpod ready-to-code

Usage

As a CLI tool

foo@bar:~$ rrgc -h
USAGE
  rrgc WINDOWS -- GLOBS

FLAGS
  -debug false  debug
  -keep false   print list of files to keep instead of files to delete
foo@bar:~$ ls logs
A.log
B.log
C.log
D.log
E.log
F.log
G.log
H.log
I.log
J.log
K.log
L.log
M.log
N.log
O.log
P.log
Q.log
R.log
foo@bar:~$ rrgc 24h,5 1h,5 -- ./logs/*.log
logs/B.log
logs/C.log
logs/E.log
logs/H.log
logs/M.log
logs/N.log
logs/O.log
logs/P.log
logs/Q.log
logs/R.log
foo@bar:~$ rrgc --keep 24h,5 1h,5 -- ./logs/*.log
logs/A.log
logs/D.log
logs/F.log
logs/G.log
logs/I.log
logs/J.log
logs/K.log
logs/L.log
foo@bar:~$ rrgc 24h,5 1h,5 -- ./logs/*.log | xargs rm -v
removed 'logs/B.log'
removed 'logs/C.log'
removed 'logs/E.log'
removed 'logs/H.log'
removed 'logs/M.log'
removed 'logs/N.log'
removed 'logs/O.log'
removed 'logs/P.log'
removed 'logs/Q.log'
removed 'logs/R.log'
foo@bar:~$ ls logs
A.log
D.log
F.log
G.log
I.log
J.log
K.log
L.log

As a Library

import (
	"os"
	"time"

	"moul.io/rrgc/rrgc"
)

func Example() {
	logGlobs := []string{
		"*/*.log",
		"*/*.log.gz",
	}
	windows := []rrgc.Window{
		{Every: 2 * time.Hour, MaxKeep: 5},
		{Every: time.Hour * 24, MaxKeep: 4},
		{Every: time.Hour * 24 * 7, MaxKeep: 3},
	}
	_, toDelete, _ := rrgc.GCListByPathGlobs(logGlobs, windows)
	for _, path := range toDelete {
		_ = os.Remove(path)
	}
}
FUNCTIONS

func GCListByPathGlobs(inputs []string, windows []Window) ([]string, []string, error)
    GCListByPathGlobs computes a list of paths that should be kept and deleted,
    based on a list of window rules.


TYPES

type Window struct {
	Every   time.Duration
	MaxKeep int
}
    Window defines a file preservation rule.

func ParseWindow(input string) (Window, error)
    ParseWindow parses a human-readable Window definition.

    Syntax: "Duration,MaxKeep".

    Examples: "1h,5" "1h2m3s,42".

Install

Using go

go get moul.io/rrgc

Releases

See https://github.com/moul/rrgc/releases

Contribute

Contribute <3

I really welcome contributions. Your input is the most precious material. I'm well aware of that and I thank you in advance. Everyone is encouraged to look at what they can do on their own scale; no effort is too small.

Everything on contribution is sum up here: CONTRIBUTING.md

Dev helpers

Pre-commit script for install: https://pre-commit.com

Contributors ✨

All Contributors

Thanks goes to these wonderful people (emoji key):


Manfred Touron

🚧 πŸ“– ⚠️ πŸ’»

moul-bot

🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

Stargazers over time

Stargazers over time

License

Β© 2021 Manfred Touron

Licensed under the Apache License, Version 2.0 (LICENSE-APACHE) or the MIT license (LICENSE-MIT), at your option. See the COPYRIGHT file for more details.

SPDX-License-Identifier: (Apache-2.0 OR MIT)