Skip to content

Commit 6a380c2

Browse files
authored
Merge pull request #15 from maxmind/greg/better-help
Improve help and error output
2 parents d1eaa56 + 3927704 commit 6a380c2

File tree

10 files changed

+650
-21
lines changed

10 files changed

+650
-21
lines changed

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

convert/convert.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99

1010
"github.com/mikioh/ipaddr"
11+
"github.com/pkg/errors"
1112
)
1213

1314
type headerFunc func([]string) []string
@@ -27,21 +28,21 @@ func ConvertFile(
2728
) error {
2829
outFile, err := os.Create(outputFile)
2930
if err != nil {
30-
return err
31+
return errors.Wrapf(err, "error creating output file (%s)", outputFile)
3132
}
3233
defer outFile.Close()
3334

3435
inFile, err := os.Open(inputFile)
3536
if err != nil {
36-
return err
37+
return errors.Wrapf(err, "error opening input file (%s)", inputFile)
3738
}
3839
defer inFile.Close()
3940

4041
return Convert(inFile, outFile, cidr, ipRange, intRange)
4142
}
4243

4344
// Convert writes the MaxMind GeoIP2 or GeoLite2 CSV in the `input` io.Reader
44-
// to the Writer `output` using the network represenation specified by setting
45+
// to the Writer `output` using the network representation specified by setting
4546
// `cidr`, ipRange`, or `intRange` to true. If none of these are set to true,
4647
// it will strip off the network information.
4748
func Convert(
@@ -140,38 +141,41 @@ func convert(
140141

141142
header, err := reader.Read()
142143
if err != nil {
143-
return err
144+
return errors.Wrap(err, "error reading CSV header")
144145
}
145146

146147
newHeader := makeHeader(header[1:])
147148
err = writer.Write(newHeader)
148149
if err != nil {
149-
return err
150+
return errors.Wrap(err, "error writing CSV header")
150151
}
151152

152153
for {
153154
record, err := reader.Read()
154155
if err == io.EOF {
155156
break
156157
} else if err != nil {
157-
return err
158+
return errors.Wrap(err, "error reading CSV")
158159
}
159160

160161
p, err := makePrefix(record[0])
161162
if err != nil {
162163
return err
163164
}
164-
writer.Write(makeLine(p, record[1:]))
165+
err = writer.Write(makeLine(p, record[1:]))
166+
if err != nil {
167+
return errors.Wrap(err, "error writing CSV")
168+
}
165169
}
166170

167171
writer.Flush()
168-
return writer.Error()
172+
return errors.Wrap(writer.Error(), "error writing CSV")
169173
}
170174

171175
func makePrefix(network string) (*ipaddr.Prefix, error) {
172176
_, ipn, err := net.ParseCIDR(network)
173177
if err != nil {
174-
return nil, err
178+
return nil, errors.Wrapf(err, "error parsing network (%s)", network)
175179
}
176180
return ipaddr.NewPrefix(ipn), nil
177181
}

main.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,65 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
7+
"strings"
68

79
"github.com/maxmind/geoip2-csv-converter/convert"
810
)
911

1012
func main() {
11-
required := "REQUIRED"
12-
input := flag.String("block-file", required, "The path to the block CSV file to use as input")
13-
output := flag.String("output-file", required, "The path to the output CSV")
13+
input := flag.String("block-file", "", "The path to the block CSV file to use as input (REQUIRED)")
14+
output := flag.String("output-file", "", "The path to the output CSV (REQUIRED)")
1415
ipRange := flag.Bool("include-range", false, "Include the IP range of the network in string format")
1516
intRange := flag.Bool("include-integer-range", false, "Include the IP range of the network in integer format")
1617
cidr := flag.Bool("include-cidr", false, "Include the network in CIDR format")
1718

1819
flag.Parse()
1920

20-
if *input == required || *output == required {
21-
printHelp()
22-
return
21+
var errors []string
22+
23+
if *input == "" {
24+
errors = append(errors, "-block-file is required")
25+
}
26+
27+
if *output == "" {
28+
errors = append(errors, "-output-file is required")
2329
}
2430

2531
if !*ipRange && !*intRange && !*cidr {
26-
printHelp()
27-
return
32+
errors = append(errors, "-include-cidr, -include-range, or -include-integer-range is required")
33+
}
34+
35+
args := flag.Args()
36+
if len(args) > 0 {
37+
errors = append(errors, "unknown argument(s): "+strings.Join(args, ", "))
38+
}
39+
40+
if len(errors) != 0 {
41+
printHelp(errors)
42+
os.Exit(1)
2843
}
2944

3045
err := convert.ConvertFile(*input, *output, *cidr, *ipRange, *intRange)
3146
if err != nil {
32-
fmt.Printf("Error: %v\n", err)
47+
fmt.Fprintf(flag.CommandLine.Output(), "Error: %v\n", err)
48+
os.Exit(1)
3349
}
3450
}
3551

36-
func printHelp() {
37-
flag.PrintDefaults()
38-
fmt.Println("\nAt least one of -include-* param is required")
52+
func printHelp(errors []string) {
53+
var passedFlags []string
54+
flag.Visit(func(f *flag.Flag) {
55+
passedFlags = append(passedFlags, "-"+f.Name)
56+
})
57+
58+
if len(passedFlags) > 0 {
59+
errors = append(errors, "flags passed: "+strings.Join(passedFlags, ", "))
60+
}
61+
62+
for _, message := range errors {
63+
fmt.Fprintln(flag.CommandLine.Output(), message)
64+
}
3965

66+
flag.Usage()
4067
}

vendor/github.com/pkg/errors/.gitignore

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pkg/errors/.travis.yml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pkg/errors/LICENSE

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pkg/errors/README.md

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pkg/errors/appveyor.yml

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)