package csvimporter import ( "encoding/csv" "os" "strconv" "git.ar21.de/yolokube/country-geo-locations/internal/database" "git.ar21.de/yolokube/country-geo-locations/pkg/geoloc" ) func parseCSV(filePath string) ([]geoloc.IPInfo, error) { file, fileErr := os.Open(filePath) if fileErr != nil { return nil, fileErr } defer file.Close() reader := csv.NewReader(file) var data []geoloc.IPInfo for { record, err := reader.Read() if err != nil { break } ipnumfrom, err := strconv.ParseUint(record[0], 10, 64) if err != nil { continue } ipnumto, err := strconv.ParseUint(record[1], 10, 64) if err != nil { continue } if record[2] == "-" { record[2] = "" } if record[3] == "-" { record[3] = "" } if record[4] == "-" { record[4] = "" } if record[5] == "-" { record[5] = "" } latitude, err := strconv.ParseFloat(record[6], 32) if err != nil { latitude = 0 } longitude, err := strconv.ParseFloat(record[7], 32) if err != nil { longitude = 0 } ipinfo := geoloc.IPInfo{ IPNumFrom: uint(ipnumfrom), IPNumTo: uint(ipnumto), Code: record[2], Country: record[3], State: record[4], City: record[5], Latitude: float32(latitude), Longitude: float32(longitude), } data = append(data, ipinfo) } return data, nil } func ImportCSV(filePath string, db *database.Database) error { ipinfos, err := parseCSV(filePath) if err != nil { return err } return db.Load(ipinfos) }