diff options
-rw-r--r-- | skate/cmd/skate-verify/main.go | 51 |
1 files changed, 25 insertions, 26 deletions
diff --git a/skate/cmd/skate-verify/main.go b/skate/cmd/skate-verify/main.go index 895d508..ab198fd 100644 --- a/skate/cmd/skate-verify/main.go +++ b/skate/cmd/skate-verify/main.go @@ -10,6 +10,7 @@ package main import ( "bufio" "flag" + "io" "log" "os" "runtime" @@ -52,6 +53,12 @@ func main() { pprof.StartCPUProfile(file) defer pprof.StopCPUProfile() } + var ( + f, g io.ReadCloser + err error + bw = bufio.NewWriter(os.Stdout) + ) + defer bw.Flush() switch *mode { case "exact": // Fixed zip mode for DOI. @@ -65,18 +72,11 @@ func main() { } log.Fatalf("need a reason for the record, one of: %s", strings.Join(keys, ", ")) } - f, err := os.Open(*releasesFile) - if err != nil { + if f, g, err = readersFromFilenames(*releasesFile, *refsFile); err != nil { log.Fatal(err) } defer f.Close() - g, err := os.Open(*refsFile) - if err != nil { - log.Fatal(err) - } defer g.Close() - bw := bufio.NewWriter(os.Stdout) - defer bw.Flush() mr, ok := matchResults[*exactReason] if !ok { mr = matchResults["unknown"] @@ -90,18 +90,11 @@ func main() { if *refsFile == "" || *releasesFile == "" { log.Fatal("zip mode requires -F and -R to be set") } - f, err := os.Open(*releasesFile) - if err != nil { + if f, g, err = readersFromFilenames(*releasesFile, *refsFile); err != nil { log.Fatal(err) } defer f.Close() - g, err := os.Open(*refsFile) - if err != nil { - log.Fatal(err) - } defer g.Close() - bw := bufio.NewWriter(os.Stdout) - defer bw.Flush() if err := skate.ZippyVerifyRefs(f, g, bw); err != nil { log.Fatal(err) } @@ -126,23 +119,16 @@ func main() { if *wikiFile == "" || *releasesFile == "" { log.Fatal("mode requires -W and -F to be set") } - f, err := os.Open(*releasesFile) - if err != nil { + if f, g, err = readersFromFilenames(*releasesFile, *wikiFile); err != nil { log.Fatal(err) } defer f.Close() - g, err := os.Open(*wikiFile) - if err != nil { - log.Fatal(err) - } defer g.Close() - bw := bufio.NewWriter(os.Stdout) - defer bw.Flush() - if err := skate.ZippyFixedWiki(f, g, skate.MatchResult{skate.StatusExact, skate.ReasonDOI}, "wiki", bw); err != nil { + if err = skate.ZippyFixedWiki(f, g, skate.MatchResult{skate.StatusExact, skate.ReasonDOI}, "wiki", bw); err != nil { log.Fatal(err) } default: - log.Fatal("not implemented, only: zip, ref, bref") + log.Fatal("not implemented, only: exact, zip, ref, bref, wiki") } if *memProfile != "" { f, err := os.Create(*memProfile) @@ -156,3 +142,16 @@ func main() { } } } + +// readersFromFilenames lets the called check for a single error only. +func readersFromFilenames(f0, f1 string) (io.ReadCloser, io.ReadCloser, error) { + f, err := os.Open(f0) + if err != nil { + return nil, nil, err + } + g, err := os.Open(f1) + if err != nil { + return nil, nil, err + } + return f, g, nil +} |