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
|
|
|
"log"
|
2020-02-29 16:51:32 -07:00
|
|
|
"io"
|
2019-11-24 15:58:43 -07:00
|
|
|
"strings"
|
2019-09-02 19:47:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Mothballs struct {
|
|
|
|
categories map[string]*Zipfs
|
2020-02-29 16:51:32 -07:00
|
|
|
afero.Fs
|
2019-09-02 19:47:24 -06:00
|
|
|
}
|
|
|
|
|
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-29 16:51:32 -07:00
|
|
|
Fs: fs,
|
2019-09-02 19:47:24 -06:00
|
|
|
categories: make(map[string]*Zipfs),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 16:51:32 -07:00
|
|
|
func (m *Mothballs) Metadata(cat string, points int) (io.ReadCloser, error) {
|
|
|
|
f, err := m.Fs.Open("/dev/null")
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mothballs) Open(cat string, points int, filename string) (io.ReadCloser, error) {
|
|
|
|
f, err := m.Fs.Open("/dev/null")
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mothballs) Inventory() []Category {
|
|
|
|
return []Category{}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Mothballs) Update() {
|
2019-09-02 19:47:24 -06:00
|
|
|
// Any new categories?
|
2020-02-29 16:51:32 -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()
|
|
|
|
if !strings.HasSuffix(filename, ".mb") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
categoryName := strings.TrimSuffix(filename, ".mb")
|
|
|
|
|
|
|
|
if _, ok := m.categories[categoryName]; !ok {
|
2020-02-29 16:51:32 -07:00
|
|
|
zfs, err := OpenZipfs(m.Fs, filename)
|
2019-09-02 19:47:24 -06:00
|
|
|
if err != nil {
|
2020-02-29 16:51:32 -07:00
|
|
|
log.Print("Error opening ", filename, ": ", err)
|
2019-09-02 19:47:24 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Print("New mothball: ", filename)
|
|
|
|
m.categories[categoryName] = zfs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|