diff --git a/pokesay.go b/pokesay.go index 15b09ebf..c7b55bee 100644 --- a/pokesay.go +++ b/pokesay.go @@ -85,18 +85,6 @@ func parseFlags() pokesay.Args { return args } -func EntryFpath(idx int) string { - return pokedex.EntryFpath(CowDataRoot, idx) -} - -func MetadataFpath(idx int) string { - return pokedex.MetadataFpath(MetadataRoot, idx) -} - -func CategoryFpath(category string, fname string) string { - return pokedex.CategoryFpath(CategoryRoot, category, fname) -} - func runListCategories() { categories := pokedex.ReadStructFromBytes[[]string](GOBCategoryKeys) fmt.Printf("%s\n%d %s\n", strings.Join(categories, " "), len(categories), "total categories") @@ -170,7 +158,7 @@ func runPrintRandom(args pokesay.Args) { t := timer.NewTimer("runPrintRandom", true) choice := pokesay.RandomInt(pokedex.ReadIntFromBytes(GOBTotal)) t.Mark("choose index") - metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, MetadataFpath(choice)) + metadata := pokedex.ReadMetadataFromEmbedded(GOBCowNames, pokedex.MetadataFpath(MetadataRoot, choice)) t.Mark("read metadata") final := metadata.Entries[pokesay.RandomInt(len(metadata.Entries))] diff --git a/src/bin/convert/png_convert.go b/src/bin/convert/png_convert.go index 9e86909d..33f2cc58 100644 --- a/src/bin/convert/png_convert.go +++ b/src/bin/convert/png_convert.go @@ -4,19 +4,12 @@ import ( "encoding/json" "flag" "fmt" - "log" "os" "github.com/tmck-code/pokesay/src/bin" "github.com/tmck-code/pokesay/src/pokedex" ) -func check(e error) { - if e != nil { - log.Fatal(e) - } -} - var ( DEBUG bool = false ) diff --git a/src/bin/pokedex/pokedex.go b/src/bin/pokedex/pokedex.go index 139760de..c43d8bea 100644 --- a/src/bin/pokedex/pokedex.go +++ b/src/bin/pokedex/pokedex.go @@ -3,7 +3,6 @@ package main import ( "flag" "fmt" - "log" "os" "path" "strings" @@ -12,12 +11,6 @@ import ( "github.com/tmck-code/pokesay/src/pokedex" ) -func check(e error) { - if e != nil { - log.Fatal(e) - } -} - // Strips the leading "./" from a path e.g. "./cows/ -> cows/" func normaliseRelativeDir(dirPath string) string { return strings.TrimPrefix(dirPath, "./") @@ -33,6 +26,20 @@ type PokedexArgs struct { ToTotalFname string } +type PokedexPaths struct { + EntryDirPath string + MetadataDirPath string + TotalFpath string +} + +func NewPokedexPaths(args PokedexArgs) PokedexPaths { + return PokedexPaths{ + EntryDirPath: path.Join(args.ToDir, args.ToDataSubDir), + MetadataDirPath: path.Join(args.ToDir, args.ToMetadataSubDir), + TotalFpath: path.Join(args.ToDir, args.ToTotalFname), + } +} + func parseArgs() PokedexArgs { fromDir := flag.String("from", "/tmp/cows", "from dir") fromMetadataFname := flag.String("fromMetadata", "/tmp/cows/pokemon.json", "metadata file") @@ -63,7 +70,7 @@ func parseArgs() PokedexArgs { func mkDirs(dirPaths []string) { for _, dirPath := range dirPaths { err := os.MkdirAll(dirPath, 0755) - check(err) + pokedex.Check(err) } } @@ -81,13 +88,10 @@ func mkDirs(dirPaths []string) { // - contains the total number of pokemon files, used for random selection func main() { args := parseArgs() - - totalFpath := path.Join(args.ToDir, args.ToTotalFname) - entryDirPath := path.Join(args.ToDir, args.ToDataSubDir) - metadataDirPath := path.Join(args.ToDir, args.ToMetadataSubDir) + paths := NewPokedexPaths(args) // ensure that the destination directories exist - mkDirs([]string{entryDirPath, metadataDirPath}) + mkDirs([]string{paths.EntryDirPath, paths.MetadataDirPath}) // Find all the cowfiles cowfileFpaths := pokedex.FindFiles(args.FromDir, ".cow", make([]string, 0)) @@ -102,7 +106,7 @@ func main() { data, err := os.ReadFile(fpath) pokedex.Check(err) - pokedex.WriteBytesToFile(data, pokedex.EntryFpath(entryDirPath, i), true) + pokedex.WriteBytesToFile(data, pokedex.EntryFpath(paths.EntryDirPath, i), true) pbar.Add(1) } @@ -115,7 +119,7 @@ func main() { pbar = bin.NewProgressBar(len(pokemonNames)) for key, name := range pokemonNames { metadata := pokedex.CreateNameMetadata(i, key, name, args.FromDir, cowfileFpaths) - pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(metadataDirPath, i)) + pokedex.WriteStructToFile(metadata, pokedex.MetadataFpath(paths.MetadataDirPath, i)) pokemonMetadata = append(pokemonMetadata, *metadata) uniqueNames[name.Slug] = append(uniqueNames[name.Slug], i) i++ @@ -130,12 +134,12 @@ func main() { pokedex.WriteStructToFile(categories, "build/assets/category_keys.txt") fmt.Println("- Writing total metadata to file") - pokedex.WriteIntToFile(len(pokemonMetadata), totalFpath) + pokedex.WriteIntToFile(len(pokemonMetadata), paths.TotalFpath) fmt.Println("✓ Complete! Indexed", len(cowfileFpaths), "total cowfiles") fmt.Println("wrote", i, "names to", "build/assets/names.txt") - fmt.Println("✓ Wrote gzipped metadata to", metadataDirPath) - fmt.Println("✓ Wrote gzipped cowfiles to", entryDirPath) - fmt.Println("✓ Wrote 'total' metadata to", totalFpath, len(pokemonMetadata)) + fmt.Println("✓ Wrote gzipped metadata to", paths.MetadataDirPath) + fmt.Println("✓ Wrote gzipped cowfiles to", paths.EntryDirPath) + fmt.Println("✓ Wrote 'total' metadata to", paths.TotalFpath, len(pokemonMetadata)) } diff --git a/src/pokesay/lookup.go b/src/pokesay/lookup.go index 75c39ecb..99bb4a8f 100644 --- a/src/pokesay/lookup.go +++ b/src/pokesay/lookup.go @@ -45,7 +45,7 @@ func ChooseByCategory(category string, categoryDir []fs.DirEntry, categoryFiles categoryMetadata, err := categoryFiles.ReadFile( pokedex.CategoryFpath(categoryRootDir, category, choice.Name()), ) - Check(err) + pokedex.Check(err) parts := strings.Split(string(categoryMetadata), "/") @@ -55,7 +55,7 @@ func ChooseByCategory(category string, categoryDir []fs.DirEntry, categoryFiles ) entryIndex, err := strconv.Atoi(string(parts[1])) - Check(err) + pokedex.Check(err) return metadata, metadata.Entries[entryIndex] } diff --git a/src/pokesay/utils.go b/src/pokesay/utils.go deleted file mode 100644 index 4e89f704..00000000 --- a/src/pokesay/utils.go +++ /dev/null @@ -1,9 +0,0 @@ -package pokesay - -import "log" - -func Check(e error) { - if e != nil { - log.Fatal(e) - } -} diff --git a/test/pokedex_test.go b/test/pokedex_test.go index 6a23a9b9..a988aeec 100644 --- a/test/pokedex_test.go +++ b/test/pokedex_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/tmck-code/pokesay/src/pokedex" - "github.com/tmck-code/pokesay/src/pokesay" ) var ( @@ -32,7 +31,7 @@ func TestReadEntry(test *testing.T) { result := pokedex.ReadPokemonCow(GOBCowData, "data/cows/1.cow") expected, err := os.ReadFile("data/cows/egg.cow") - pokesay.Check(err) + pokedex.Check(err) Assert(expected, result, test) }