Factor out sig functions
This commit is contained in:
@@ -2,13 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/firestuff/dnd/internal"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"golang.org/x/exp/slog"
|
"golang.org/x/exp/slog"
|
||||||
)
|
)
|
||||||
@@ -56,7 +54,7 @@ func validateMaps(l *slog.Logger, mapsFS fs.FS) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func validateMap(l *slog.Logger, mapFS fs.FS) bool {
|
func validateMap(l *slog.Logger, mapFS fs.FS) bool {
|
||||||
sig := getDirSig(mapFS)
|
sig := internal.DirSig(mapFS)
|
||||||
|
|
||||||
t := mapSigs[sig]
|
t := mapSigs[sig]
|
||||||
if t == "" {
|
if t == "" {
|
||||||
@@ -71,31 +69,3 @@ func validateMap(l *slog.Logger, mapFS fs.FS) bool {
|
|||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDirSig(root fs.FS) string {
|
|
||||||
sigSet := map[string]bool{}
|
|
||||||
|
|
||||||
fs.WalkDir(root, ".", func(path string, entry fs.DirEntry, err error) error {
|
|
||||||
if entry.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasPrefix(entry.Name(), ".") {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
sigSet[getPathSig(path)] = true
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
sigs := lo.Keys(sigSet)
|
|
||||||
sort.Strings(sigs)
|
|
||||||
|
|
||||||
return strings.Join(sigs, ";")
|
|
||||||
}
|
|
||||||
|
|
||||||
func getPathSig(path string) string {
|
|
||||||
parts := strings.Split(filepath.Base(path), ".")
|
|
||||||
return fmt.Sprintf("%s/*.%s", filepath.Dir(path), lo.Must(lo.Last(parts)))
|
|
||||||
}
|
|
||||||
|
|||||||
39
internal/sig.go
Normal file
39
internal/sig.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func DirSig(root fs.FS) string {
|
||||||
|
sigSet := map[string]bool{}
|
||||||
|
|
||||||
|
fs.WalkDir(root, ".", func(path string, entry fs.DirEntry, err error) error {
|
||||||
|
if entry.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(entry.Name(), ".") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
sigSet[PathSig(path)] = true
|
||||||
|
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
sigs := lo.Keys(sigSet)
|
||||||
|
sort.Strings(sigs)
|
||||||
|
|
||||||
|
return strings.Join(sigs, ";")
|
||||||
|
}
|
||||||
|
|
||||||
|
func PathSig(path string) string {
|
||||||
|
parts := strings.Split(filepath.Base(path), ".")
|
||||||
|
return fmt.Sprintf("%s/*.%s", filepath.Dir(path), lo.Must(lo.Last(parts)))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user