| 1 |
// write-test-data generates test mmdb files. |
| 2 |
package main |
| 3 |
|
| 4 |
import ( |
| 5 |
"bufio" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/maxmind/MaxMind-DB/pkg/writer" |
| 13 |
) |
| 14 |
|
| 15 |
const moduleName = "github.com/maxmind/MaxMind-DB" |
| 16 |
|
| 17 |
func main() { |
| 18 |
var defaultSource, defaultTarget, defaultBadData string |
| 19 |
if root, err := findRepoRoot(); err == nil { |
| 20 |
defaultSource = filepath.Join(root, "source-data") |
| 21 |
defaultTarget = filepath.Join(root, "test-data") |
| 22 |
defaultBadData = filepath.Join(root, "bad-data", "libmaxminddb") |
| 23 |
} |
| 24 |
|
| 25 |
source := flag.String("source", defaultSource, "Source data directory") |
| 26 |
target := flag.String( |
| 27 |
"target", |
| 28 |
defaultTarget, |
| 29 |
"Destination directory for the generated mmdb files", |
| 30 |
) |
| 31 |
badData := flag.String( |
| 32 |
"bad-data", |
| 33 |
defaultBadData, |
| 34 |
"Destination directory for generated bad mmdb files", |
| 35 |
) |
| 36 |
|
| 37 |
flag.Parse() |
| 38 |
|
| 39 |
w, err := writer.New(*source, *target) |
| 40 |
if err != nil { |
| 41 |
fmt.Printf("creating writer: %+v\n", err) |
| 42 |
os.Exit(1) |
| 43 |
} |
| 44 |
|
| 45 |
if err := w.WriteIPv4TestDB(); err != nil { |
| 46 |
fmt.Printf("writing IPv4 test databases: %+v\n", err) |
| 47 |
os.Exit(1) |
| 48 |
} |
| 49 |
|
| 50 |
if err := w.WriteIPv6TestDB(); err != nil { |
| 51 |
fmt.Printf("writing IPv6 test databases: %+v\n", err) |
| 52 |
os.Exit(1) |
| 53 |
} |
| 54 |
|
| 55 |
if err := w.WriteMixedIPTestDB(); err != nil { |
| 56 |
fmt.Printf("writing IPv6 test databases: %+v\n", err) |
| 57 |
os.Exit(1) |
| 58 |
} |
| 59 |
|
| 60 |
if err := w.WriteNoIPv4TestDB(); err != nil { |
| 61 |
fmt.Printf("writing no IPv4 test databases: %+v\n", err) |
| 62 |
os.Exit(1) |
| 63 |
} |
| 64 |
|
| 65 |
if err := w.WriteNoMapTestDB(); err != nil { |
| 66 |
fmt.Printf("writing no map test databases: %+v\n", err) |
| 67 |
os.Exit(1) |
| 68 |
} |
| 69 |
|
| 70 |
if err := w.WriteMetadataPointersTestDB(); err != nil { |
| 71 |
fmt.Printf("writing metadata pointers test databases: %+v\n", err) |
| 72 |
os.Exit(1) |
| 73 |
} |
| 74 |
|
| 75 |
if err := w.WriteDecoderTestDB(); err != nil { |
| 76 |
fmt.Printf("writing decoder test databases: %+v\n", err) |
| 77 |
os.Exit(1) |
| 78 |
} |
| 79 |
|
| 80 |
if err := w.WriteDeeplyNestedStructuresTestDB(); err != nil { |
| 81 |
fmt.Printf("writing decoder test databases: %+v\n", err) |
| 82 |
os.Exit(1) |
| 83 |
} |
| 84 |
|
| 85 |
if err := w.WriteGeoIP2TestDB(); err != nil { |
| 86 |
fmt.Printf("writing GeoIP2 test databases: %+v\n", err) |
| 87 |
os.Exit(1) |
| 88 |
} |
| 89 |
|
| 90 |
if *badData != "" { |
| 91 |
if err := w.WriteBadDataDBs(*badData); err != nil { |
| 92 |
fmt.Printf("writing bad data test databases: %+v\n", err) |
| 93 |
os.Exit(1) |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// findRepoRoot walks up from the current working directory looking for a |
| 99 |
// go.mod that belongs to this module. It returns the directory containing |
| 100 |
// that go.mod, or an error if none is found. |
| 101 |
func findRepoRoot() (string, error) { |
| 102 |
dir, err := os.Getwd() |
| 103 |
if err != nil { |
| 104 |
return "", fmt.Errorf("getting working directory: %w", err) |
| 105 |
} |
| 106 |
|
| 107 |
for { |
| 108 |
if hasModuleFile(dir) { |
| 109 |
return dir, nil |
| 110 |
} |
| 111 |
parent := filepath.Dir(dir) |
| 112 |
if parent == dir { |
| 113 |
return "", fmt.Errorf( |
| 114 |
"could not find go.mod for %s in any parent directory", |
| 115 |
moduleName, |
| 116 |
) |
| 117 |
} |
| 118 |
dir = parent |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// hasModuleFile reports whether dir contains a go.mod whose first "module" |
| 123 |
// directive matches moduleName. |
| 124 |
func hasModuleFile(dir string) bool { |
| 125 |
f, err := os.Open(filepath.Clean(filepath.Join(dir, "go.mod"))) |
| 126 |
if err != nil { |
| 127 |
return false |
| 128 |
} |
| 129 |
defer f.Close() |
| 130 |
|
| 131 |
scanner := bufio.NewScanner(f) |
| 132 |
for scanner.Scan() { |
| 133 |
line := strings.TrimSpace(scanner.Text()) |
| 134 |
if after, ok := strings.CutPrefix(line, "module "); ok { |
| 135 |
return strings.TrimSpace(after) == moduleName |
| 136 |
} |
| 137 |
} |
| 138 |
return false |
| 139 |
} |