Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaka0325 committed Jul 15, 2020
1 parent 51e8c55 commit 6c9f2bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
10 changes: 9 additions & 1 deletion kadai1/tanaka0325/imgconv/cmd/imgconv/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 8 additions & 7 deletions kadai1/tanaka0325/imgconv/imgconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package imgconv
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -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)
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit 6c9f2bb

Please sign in to comment.