jojomi / keep

Keep files by date or remove them. Think of backup files to be saved for a certain amount of hours, days, weeks, months, or years.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

keep

Godoc Reference Go Version Last Commit Coverage Go Report Card License

This library helps you decide which elements should be kept for a given number of hours, days, weeks, months, and years. Most obvious use case are backup files.

Installation

go get github.com/jojomi/keep

Algorithm

Given the configuration below (3 last, 12 hours, 7 days, 12 weeks, 12 months, and 4 years) this library will operate on the list of elements sorted by date, youngest first.

  • It will select the first 3 elements from the list (last)
  • It then will select 12 elements that are not less than an hour apart, the first of them being the youngest file there is in the set.
  • It will then reset the time and select 7 elements that are not less than a day apart, the first of them being the youngest file there is in the set.
  • It is allowed to skip definitions, so you don't have to select daily elements even if you specify hourly and weekly selections.

Usage

import "github.com/jojomi/keep"

func main() {
    j := NewDefaultJailhouse()
    j.AddElements(...)

    reqs := NewRequirementsFromMap(map[TimeRange]uint16{
        LAST: 3,
        DAY:  2,
        MONTH: 4,
    })

    j.ApplyRequirements(*reqs)
    kept := j.KeptElements()
    deletable := j.FreeElements()

    // print kept elements
    for _, k := range kept {
        // handle here, use .
    }
	
    // delete now
    for _, k := range kept {
        // handle deletion here
    }
}

Your input data must implement the TimeResource interface:

type TimeResource interface {
	GetTime() time.Time
}

If you are interested in the elements that can be removed under the rules of your, swap List for ListRemovable.

For tests or special needs, there are also functions named ListForDate and ListRemovableForDate.

About

Keep files by date or remove them. Think of backup files to be saved for a certain amount of hours, days, weeks, months, or years.

License:MIT License


Languages

Language:Go 97.5%Language:Shell 2.5%