-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.go
78 lines (62 loc) · 2.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// geoip2-csv-converter is a utility for converting the MaxMind GeoIP2 and
// GeoLite2 CSVs to different formats for representing IP addresses such as IP
// ranges or integer ranges.
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/maxmind/geoip2-csv-converter/convert"
)
func main() {
input := flag.String("block-file", "", "The path to the block CSV file to use as input (REQUIRED)")
output := flag.String("output-file", "", "The path to the output CSV (REQUIRED)")
ipRange := flag.Bool("include-range", false, "Include the IP range of the network in string format")
intRange := flag.Bool("include-integer-range", false, "Include the IP range of the network in integer format")
hexRange := flag.Bool("include-hex-range", false, "Include the IP range of the network in hexadecimal format")
cidr := flag.Bool("include-cidr", false, "Include the network in CIDR format")
flag.Parse()
var errors []string
if *input == "" {
errors = append(errors, "-block-file is required")
}
if *output == "" {
errors = append(errors, "-output-file is required")
}
if *input != "" && *output != "" && *output == *input {
errors = append(errors, "Your output file must be different than your block file(input file).")
}
if !*ipRange && !*intRange && !*cidr && !*hexRange {
errors = append(errors, "-include-cidr, -include-range, -include-integer-range,"+
" or -include-hex-range is required")
}
args := flag.Args()
if len(args) > 0 {
errors = append(errors, "unknown argument(s): "+strings.Join(args, ", "))
}
if len(errors) != 0 {
printHelp(errors)
os.Exit(1)
}
err := convert.ConvertFile(*input, *output, *cidr, *ipRange, *intRange, *hexRange)
if err != nil {
//nolint:errcheck // We are exiting and there isn't much we can do.
fmt.Fprintf(flag.CommandLine.Output(), "Error: %v\n", err)
os.Exit(1)
}
}
func printHelp(errors []string) {
var passedFlags []string
flag.Visit(func(f *flag.Flag) {
passedFlags = append(passedFlags, "-"+f.Name)
})
if len(passedFlags) > 0 {
errors = append(errors, "flags passed: "+strings.Join(passedFlags, ", "))
}
for _, message := range errors {
//nolint:errcheck // There isn't much to do if we can't print to the output.
fmt.Fprintln(flag.CommandLine.Output(), message)
}
flag.Usage()
}