moth/pkg/transpile/inventory.go

42 lines
772 B
Go
Raw Normal View History

2020-09-08 17:49:02 -06:00
package transpile
import (
"log"
"sort"
2020-09-14 18:23:56 -06:00
"strings"
2020-09-08 17:49:02 -06:00
"github.com/spf13/afero"
)
// Inventory maps category names to lists of point values.
type Inventory map[string][]int
// FsInventory returns a mapping of category names to puzzle point values.
func FsInventory(fs afero.Fs) (Inventory, error) {
2020-09-11 13:03:19 -06:00
dirEnts, err := afero.ReadDir(fs, "")
2020-09-08 17:49:02 -06:00
if err != nil {
log.Print(err)
return nil, err
}
inv := make(Inventory)
for _, ent := range dirEnts {
2020-09-14 18:23:56 -06:00
if strings.HasPrefix(ent.Name(), ".") {
continue
}
2020-09-08 17:49:02 -06:00
if ent.IsDir() {
name := ent.Name()
c := NewFsCategory(fs, name)
puzzles, err := c.Inventory()
if err != nil {
2020-09-16 17:54:29 -06:00
log.Printf("Inventory: %s: %s", name, err)
continue
2020-09-08 17:49:02 -06:00
}
sort.Ints(puzzles)
inv[name] = puzzles
}
}
return inv, nil
}