moth/cmd/mothd/mothballs.go

61 lines
1.1 KiB
Go
Raw Normal View History

2019-09-02 19:47:24 -06:00
package main
import (
2020-02-22 15:49:58 -07:00
"github.com/spf13/afero"
2019-09-02 19:47:24 -06:00
"io/ioutil"
"log"
"strings"
"time"
2019-09-02 19:47:24 -06:00
)
type Mothballs struct {
2020-02-22 15:49:58 -07:00
fs afero.Fs
2019-09-02 19:47:24 -06:00
categories map[string]*Zipfs
}
2020-02-22 15:49:58 -07:00
func NewMothballs(fs afero.Fs) *Mothballs {
2019-09-02 19:47:24 -06:00
return &Mothballs{
2020-02-22 15:49:58 -07:00
fs: fs,
2019-09-02 19:47:24 -06:00
categories: make(map[string]*Zipfs),
}
}
func (m *Mothballs) update() {
// Any new categories?
2020-02-22 15:49:58 -07:00
files, err := afero.ReadDir(m.fs, "/")
2019-09-02 19:47:24 -06:00
if err != nil {
log.Print("Error listing mothballs: ", err)
return
}
for _, f := range files {
filename := f.Name()
filepath := m.path(filename)
if !strings.HasSuffix(filename, ".mb") {
continue
}
categoryName := strings.TrimSuffix(filename, ".mb")
if _, ok := m.categories[categoryName]; !ok {
zfs, err := OpenZipfs(filepath)
if err != nil {
log.Print("Error opening ", filepath, ": ", err)
continue
}
log.Print("New mothball: ", filename)
m.categories[categoryName] = zfs
}
}
}
func (m *Mothballs) Run(updateInterval time.Duration) {
ticker := time.NewTicker(updateInterval)
m.update()
for {
select {
case when := <-ticker.C:
log.Print("Tick: ", when)
m.update()
}
}
}