@@ -10,27 +10,58 @@ import (
1010 "github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv"
1111)
1212
13- var options imgconv.Options
1413var args []string
15- var allowedExts = []string {"png" , "jpg" , "jpeg" , "gif" , "bmp" , "tiff" , "tif" }
14+
15+ var (
16+ from string
17+ to string
18+ dryRun bool
19+ )
20+
21+ var allowedExts = map [string ]bool {
22+ "png" : true ,
23+ "jpg" : true ,
24+ "jpeg" : true ,
25+ "gif" : true ,
26+ "bmp" : true ,
27+ "tiff" : true ,
28+ "tif" : true ,
29+ }
1630
1731func init () {
18- options .From = flag .String ("f" , "jpg" , "file extension before convert" )
19- options .To = flag .String ("t" , "png" , "file extension after convert" )
20- options .DryRun = flag .Bool ("n" , false , "dry run" )
32+ flag .StringVar (& from , "from" , "jpg" , "before ext" )
33+ flag .StringVar (& from , "f" , "jpg" , "before ext (short)" )
34+ flag .StringVar (& to , "to" , "png" , "after ext" )
35+ flag .StringVar (& to , "t" , "png" , "after ext (short)" )
36+ flag .BoolVar (& dryRun , "dry-run" , false , "use dry-run" )
37+ flag .BoolVar (& dryRun , "n" , false , "use dry-run (short)" )
2138 flag .Parse ()
2239
2340 args = flag .Args ()
2441}
2542
2643func main () {
27- if err := options .Validate (allowedExts ); err != nil {
28- onExit (err )
44+ // validate options
45+ if ok := isAllowedFileType (from ); ! ok {
46+ onExit (fmt .Errorf ("%s is invalid filetype" , from ))
47+ }
48+ if ok := isAllowedFileType (to ); ! ok {
49+ onExit (fmt .Errorf ("%s is invalid filetype" , to ))
2950 }
3051
52+ // validate arguments
3153 dirnames := uniq (args )
32- paths , err := getTargetFilenames (dirnames , * options .From )
54+ for _ , dirname := range dirnames {
55+ ok , err := isDir (dirname )
56+ if err != nil {
57+ onExit (err )
58+ }
59+ if ! ok {
60+ onExit (fmt .Errorf ("%s is not a directory" , dirname ))
61+ }
62+ }
3363
64+ paths , err := getTargetFilenames (dirnames , from )
3465 if err != nil {
3566 onExit (err )
3667 }
@@ -39,22 +70,26 @@ func main() {
3970 param := imgconv.ConvertParam {
4071 Path : path ,
4172 File : imgconv .NewFile (),
42- BeforeImage : imgconv .NewImage (* options . From ),
43- AfterImage : imgconv .NewImage (* options . To ),
44- FromExt : * options . From ,
45- ToExt : * options . To ,
73+ BeforeImage : imgconv .NewImage (from ),
74+ AfterImage : imgconv .NewImage (to ),
75+ FromExt : from ,
76+ ToExt : to ,
4677 }
4778
48- if ! * options . DryRun {
79+ if ! dryRun {
4980 if err := imgconv .Do (param ); err != nil {
5081 onExit (err )
5182 }
5283 } else {
53- fmt .Printf ("%[1]s.%[2]s => %[1]s.%[3]s\n " , path , param . FromExt , param .ToExt )
84+ fmt .Printf ("%[1]s.%[2]s => %[1]s.%[3]s\n " , path , from , param .ToExt )
5485 }
5586 }
5687}
5788
89+ func isAllowedFileType (ft string ) bool {
90+ return allowedExts [ft ]
91+ }
92+
5893func onExit (err error ) {
5994 fmt .Fprintln (os .Stderr , err )
6095 os .Exit (1 )
@@ -75,18 +110,12 @@ func uniq([]string) []string {
75110 return u
76111}
77112
78- func getTargetFilenames (ds []string , from string ) ([]string , error ) {
79- names := []string {}
113+ func getTargetFilenames (ds []string , ext string ) ([]string , error ) {
114+ var names []string
80115 for _ , n := range ds {
81- if ok , err := isDir (n ); err != nil {
82- return nil , err
83- } else if ! ok {
84- return nil , fmt .Errorf ("%s is not a directory" , n )
85- }
86-
87116 if err := filepath .Walk (n , func (name string , info os.FileInfo , err error ) error {
88- if filepath .Ext (name ) == "." + from {
89- n := strings .Replace (name , "." + from , "" , - 1 )
117+ if filepath .Ext (name ) == "." + ext {
118+ n := strings .Replace (name , "." + ext , "" , - 1 )
90119 names = append (names , n )
91120 }
92121 return nil
@@ -103,6 +132,7 @@ func isDir(path string) (bool, error) {
103132 if err != nil {
104133 return false , err
105134 }
135+ defer f .Close ()
106136
107137 fi , err := f .Stat ()
108138 if err != nil {
0 commit comments