From 6c9f2bb5307f7b7424f6bef96f2bd0108c122b9c Mon Sep 17 00:00:00 2001 From: Hiroyuki Tanaka Date: Wed, 15 Jul 2020 23:34:19 +0900 Subject: [PATCH] error handling --- kadai1/tanaka0325/imgconv/cmd/imgconv/main.go | 10 +++++++++- kadai1/tanaka0325/imgconv/imgconv.go | 15 ++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/kadai1/tanaka0325/imgconv/cmd/imgconv/main.go b/kadai1/tanaka0325/imgconv/cmd/imgconv/main.go index 344f749..95155e0 100644 --- a/kadai1/tanaka0325/imgconv/cmd/imgconv/main.go +++ b/kadai1/tanaka0325/imgconv/cmd/imgconv/main.go @@ -1,9 +1,17 @@ package main import ( + "fmt" + "os" + "github.com/gopherdojo/dojo8/kadai1/tanaka0325/imgconv" ) func main() { - imgconv.Run() + if err := imgconv.Run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + os.Exit(0) } diff --git a/kadai1/tanaka0325/imgconv/imgconv.go b/kadai1/tanaka0325/imgconv/imgconv.go index 2edc0d1..28410ad 100644 --- a/kadai1/tanaka0325/imgconv/imgconv.go +++ b/kadai1/tanaka0325/imgconv/imgconv.go @@ -3,7 +3,6 @@ package imgconv import ( "flag" "fmt" - "log" "os" "path/filepath" "strings" @@ -24,14 +23,14 @@ func init() { } // Run is to convert image file format -func Run() { +func Run() error { // check options ext to := strings.ToLower(*t) from := strings.ToLower(*f) targetExts := []string{to, from} for _, e := range targetExts { if err := allowedExts.include(e); err != nil { - log.Fatal(fmt.Errorf("%w. ext is only allowd in %s", err, allowedExts)) + return fmt.Errorf("%w. ext is only allowd in %s", err, allowedExts) } } @@ -40,27 +39,29 @@ func Run() { udns := uniq(dns) paths, err := getPaths(udns, from) if err != nil { - log.Fatal(err) + return err } // convert imgs, err := createConvImages(paths, from, to) if err != nil { - log.Fatal(err) + return err } for _, img := range imgs { if err := img.decode(); err != nil { - log.Fatal(err) + return err } if *dryRun { fmt.Println(img.filename+"."+img.fromExt, "=>", img.filename+"."+img.toExt) } else { if err := img.encode(); err != nil { - log.Fatal(err) + return err } } } + + return nil } func uniq(s []string) []string {