diff --git a/cmd/mothd/main.go b/cmd/mothd/main.go index df1bea4..98ea8b1 100644 --- a/cmd/mothd/main.go +++ b/cmd/mothd/main.go @@ -40,10 +40,11 @@ func main() { stateFs := afero.NewBasePathFs(afero.NewOsFs(), *statePath) themeFs := afero.NewBasePathFs(afero.NewOsFs(), *themePath) + mothballFs := afero.NewBasePathFs(afero.NewOsFs(), *mothballPath) theme := NewTheme(themeFs) state := NewState(stateFs) - puzzles := NewMothballs(*puzzlePath) + puzzles := NewMothballs(mothballFs) go state.Run(*refreshInterval) go puzzles.Run(*refreshInterval) diff --git a/cmd/mothd/mothballs.go b/cmd/mothd/mothballs.go index 1ceaac6..e4c5d51 100644 --- a/cmd/mothd/mothballs.go +++ b/cmd/mothd/mothballs.go @@ -1,6 +1,7 @@ package main import ( + "github.com/spf13/afero" "io/ioutil" "log" "strings" @@ -8,22 +9,20 @@ import ( ) type Mothballs struct { - Component + fs afero.Fs categories map[string]*Zipfs } -func NewMothballs(baseDir string) *Mothballs { +func NewMothballs(fs afero.Fs) *Mothballs { return &Mothballs{ - Component: Component{ - baseDir: baseDir, - }, + fs: fs, categories: make(map[string]*Zipfs), } } func (m *Mothballs) update() { // Any new categories? - files, err := ioutil.ReadDir(m.path()) + files, err := afero.ReadDir(m.fs, "/") if err != nil { log.Print("Error listing mothballs: ", err) return diff --git a/cmd/mothd/zipfs.go b/cmd/mothd/zipfs.go index 1b0f00a..9c27e62 100644 --- a/cmd/mothd/zipfs.go +++ b/cmd/mothd/zipfs.go @@ -12,6 +12,7 @@ import ( type Zipfs struct { zf *zip.ReadCloser + fs afero.Fs filename string mtime time.Time } @@ -111,9 +112,10 @@ func (zfsf *ZipfsFile) Close() error { return zfsf.f.Close() } -func OpenZipfs(filename string) (*Zipfs, error) { +func OpenZipfs(fs afero.fs, filename string) (*Zipfs, error) { var zfs Zipfs + zfs.fs = fs zfs.filename = filename err := zfs.Refresh() @@ -129,7 +131,7 @@ func (zfs *Zipfs) Close() error { } func (zfs *Zipfs) Refresh() error { - info, err := os.Stat(zfs.filename) + info, err := zfs.fs.Stat(zfs.filename) if err != nil { return err } @@ -139,6 +141,11 @@ func (zfs *Zipfs) Refresh() error { return nil } + f, err := zfs.fs.Open(zfs.filename) + if err != nil { + return err + } + zf, err := zip.OpenReader(zfs.filename) if err != nil { return err