golib/cmd/check-ip/geo.go
AJ ONeal ddd0986e20
refactor: push complexity into packages; main.go is orchestration only
- geoip.Databases: wraps city+ASN datasets with nil-safe Init/Run/PrintInfo
- geoip.(*Downloader).NewDatabases: builds Databases from downloader
- cmd/check-ip/geo.go: setupGeo() handles conf parsing, dir creation, DB path resolution
- cmd/check-ip/blacklist.go: isBlocked() + cohortSize() moved here
- cmd/check-ip/main.go: flags, source selection, init, check, print — nothing else
2026-04-20 12:15:14 -06:00

41 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/therootcompany/golib/net/geoip"
)
// setupGeo parses geoip-conf (if given) and returns a Databases ready to Init.
// Returns nil if no geoip flags were provided.
func setupGeo(confPath, cityPath, asnPath string) (*geoip.Databases, error) {
if confPath == "" && cityPath == "" && asnPath == "" {
return nil, nil
}
if confPath != "" {
cfg, err := geoip.ParseConf(confPath)
if err != nil {
return nil, fmt.Errorf("geoip-conf: %w", err)
}
dbDir := cfg.DatabaseDirectory
if dbDir == "" {
if dbDir, err = geoip.DefaultCacheDir(); err != nil {
return nil, fmt.Errorf("geoip cache dir: %w", err)
}
}
if err := os.MkdirAll(dbDir, 0o755); err != nil {
return nil, fmt.Errorf("mkdir %s: %w", dbDir, err)
}
if cityPath == "" {
cityPath = filepath.Join(dbDir, geoip.CityEdition+".mmdb")
}
if asnPath == "" {
asnPath = filepath.Join(dbDir, geoip.ASNEdition+".mmdb")
}
return geoip.New(cfg.AccountID, cfg.LicenseKey).NewDatabases(cityPath, asnPath), nil
}
return geoip.NewDatabases(cityPath, asnPath), nil
}