This commit is contained in:
Ian Gulliver
2023-05-14 13:06:13 -07:00
parent 393e1ddc97
commit eaad0f29c3
3 changed files with 32 additions and 19 deletions

View File

@@ -4,13 +4,13 @@ import (
"flag" "flag"
"fmt" "fmt"
"io/fs" "io/fs"
"log"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
"github.com/samber/lo" "github.com/samber/lo"
"golang.org/x/exp/slog"
) )
var mapSigs = map[string]string{ var mapSigs = map[string]string{
@@ -22,18 +22,20 @@ func main() {
flag.Parse() flag.Parse()
if *root == "" {
log.Fatal("please specify --root")
}
log.Printf("validating: %s", *root)
rootFS := os.DirFS(*root) rootFS := os.DirFS(*root)
l := slog.New(slog.NewTextHandler(os.Stderr, nil))
validateMaps(lo.Must(fs.Sub(rootFS, "Maps"))) l.Info("validating...",
"root", *root,
)
ok := validateMaps(l, lo.Must(fs.Sub(rootFS, "Maps")))
if !ok {
os.Exit(1)
}
} }
func validateMaps(mapsFS fs.FS) { func validateMaps(l *slog.Logger, mapsFS fs.FS) bool {
entries := lo.Must(mapsFS.(fs.ReadDirFS).ReadDir(".")) entries := lo.Must(mapsFS.(fs.ReadDirFS).ReadDir("."))
for _, entry := range entries { for _, entry := range entries {
@@ -41,22 +43,33 @@ func validateMaps(mapsFS fs.FS) {
continue continue
} }
log.Printf("map: %s", entry.Name()) ok := validateMap(
l.With("map", entry.Name()),
mapFS := lo.Must(fs.Sub(mapsFS, entry.Name())) lo.Must(fs.Sub(mapsFS, entry.Name())),
validateMap(mapFS) )
if !ok {
return false
} }
}
return true
} }
func validateMap(mapFS fs.FS) { func validateMap(l *slog.Logger, mapFS fs.FS) bool {
sig := getDirSig(mapFS) sig := getDirSig(mapFS)
t := mapSigs[sig] t := mapSigs[sig]
if t == "" { if t == "" {
log.Fatalf("\tunrecognized signature: %s", sig) l.Error("unrecognized signature",
"signature", sig,
)
return false
} }
log.Printf("\ttype: %s", t) l.Info("valid map",
"source", t,
)
return true
} }
func getDirSig(root fs.FS) string { func getDirSig(root fs.FS) string {

2
go.mod
View File

@@ -4,4 +4,4 @@ go 1.20
require github.com/samber/lo v1.38.1 require github.com/samber/lo v1.38.1
require golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect require golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea

4
go.sum
View File

@@ -1,4 +1,4 @@
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea h1:vLCWI/yYrdEHyN2JzIzPO3aaQJHQdp89IZBA/+azVC4=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=