github.com/influx6/npkg@v0.8.8/cmd/ipcsv/main.go (about) 1 package main 2 3 import ( 4 "encoding/csv" 5 "flag" 6 "fmt" 7 "io" 8 "log" 9 "os" 10 11 "github.com/influx6/npkg/nerror" 12 "github.com/influx6/npkg/nnet" 13 ) 14 15 16 type Location struct { 17 Type string `json:"type_"` 18 Street string `json:"street"` 19 ContinentCode string `json:"continent_code"` 20 ContinentName string `json:"continent_name"` 21 City string `json:"city"` 22 State string `json:"state"` 23 Postal string `json:"postal"` 24 Zip string `json:"zip"` 25 Zipcode string `json:"zip_code"` 26 CountryCode string `json:"country_code"` 27 CountryName string `json:"country_name"` 28 RegionCode string `json:"region_code"` 29 RegionName string `json:"region_name"` 30 Timezone string `json:"time_zone"` 31 Latitude string `json:"latitude"` 32 Longitude string `json:"longitude"` 33 MetroCode string `json:"metro_code"` 34 AreaCode string `json:"area_code"` 35 FromIP string `json:"from_ip"` 36 ToIP string `json:"to_ip"` 37 FromIPNumeric string `json:"from_ip_numeric"` 38 ToIPNumeric string `json:"to_ip_numeric"` 39 } 40 41 func generateFromCSV(targetFile string, output io.Writer) error { 42 var recordFile, recordErr = os.Open(targetFile) 43 if recordErr != nil { 44 return nerror.WrapOnly(recordErr) 45 } 46 47 defer recordFile.Close() 48 49 var reader = csv.NewReader(recordFile) 50 51 var _, printErr = fmt.Fprint(output, "var IP2Records = nnet.Locations{") 52 if printErr != nil { 53 return nerror.WrapOnly(printErr) 54 } 55 56 var line = -1 57 for { 58 var currentLine, curErr = reader.Read() 59 if curErr == io.EOF { 60 break 61 } 62 if curErr != nil { 63 return nerror.WrapOnly(curErr) 64 } 65 66 line++ 67 fmt.Printf("Handling: %+q on line %d\n", currentLine, line) 68 69 var loc Location 70 loc.FromIPNumeric = currentLine[0] 71 loc.ToIPNumeric = currentLine[1] 72 loc.CountryCode = currentLine[2] 73 loc.CountryName = currentLine[3] 74 loc.RegionName = currentLine[4] 75 loc.City = currentLine[5] 76 loc.Latitude = currentLine[6] 77 loc.Longitude = currentLine[7] 78 loc.Zipcode = currentLine[8] 79 loc.Timezone = currentLine[9] 80 81 if len(loc.FromIPNumeric) != 0 { 82 var fromIP, fromIPErr = nnet.IPLongNotation2IPFromString(loc.FromIPNumeric) 83 if fromIPErr != nil { 84 return nerror.WrapOnly(fromIPErr) 85 } 86 loc.FromIP = fromIP.String() 87 } 88 89 if len(loc.ToIPNumeric) != 0 { 90 var toIP, toIPErr = nnet.IPLongNotation2IPFromString(loc.ToIPNumeric) 91 if toIPErr != nil { 92 return nerror.WrapOnly(toIPErr) 93 } 94 loc.ToIP = toIP.String() 95 } 96 97 if writeErr := writeFile(output, loc); writeErr != nil { 98 return nerror.WrapOnly(writeErr) 99 } 100 } 101 102 var _, err = fmt.Fprint(output, "}") 103 if err != nil { 104 return nerror.WrapOnly(err) 105 } 106 return nil 107 } 108 109 func writeFile(output io.Writer, value Location) error { 110 var _, err = fmt.Fprintf(output, `{Street:%q,City:%q,State:%q,Postal:%q,CountryCode:%q,CountryName:%q,RegionCode:%q,RegionName:%q,Zipcode:%q,Lat:%q,Long:%q,MetroCode:%q,Timezone:%q,AreaCode:%q,FromIP:%q,ToIP:%q,FromIPNumeric:%q,ToIPNumeric:%q},`, 111 value.Street, value.City, value.State, 112 value.Postal, value.CountryCode, value.CountryName, 113 value.RegionCode, value.RegionName, value.Zipcode, 114 value.Latitude, value.Longitude, value.MetroCode, value.Timezone, 115 value.AreaCode, value.FromIP, value.ToIP, 116 value.FromIPNumeric, value.ToIPNumeric, 117 ) 118 if err != nil { 119 return nerror.WrapOnly(err) 120 } 121 return nil 122 } 123 124 func main() { 125 var targetCSVFile = flag.String("ip_file", "", "csv file from IP2Location.com") 126 var targetGoFile = flag.String("out", "", "go file") 127 var targetPackageName = flag.String("package", "", "name of package to use") 128 129 flag.Parse() 130 131 if len(*targetCSVFile) == 0 || len(*targetGoFile) == 0 || len(*targetPackageName) == 0 { 132 fmt.Println(`Generates a go file containing a list of Location objects which are mapped from a IPLocation.com 133 csv files. This allows us embed this as usable list to find a suited location if any for a target 134 IP. 135 136 Please always provide: 137 138 - ip_file: csv file from IP2Location.com 139 - out: the path to the golang file to generate 140 - package: the name of the package to use.`) 141 return 142 } 143 144 var targetGoFileWriter, targetGoFileErr = os.Create(*targetGoFile) 145 if targetGoFileErr != nil { 146 log.Fatalf("Error occurred: %+s\n", targetGoFileErr) 147 return 148 } 149 150 defer targetGoFileWriter.Close() 151 152 var _, printErr = fmt.Fprintf(targetGoFileWriter, "package %s\n", *targetPackageName) 153 if printErr != nil { 154 log.Fatalf("Error occurred: %+s\n", printErr) 155 return 156 } 157 158 if genErr := generateFromCSV(*targetCSVFile, targetGoFileWriter); genErr != nil { 159 log.Fatalf("Error occurred: %+s\n", genErr) 160 return 161 } 162 163 log.Println("Finished generating ip files") 164 }