@@ -10,27 +10,58 @@ import (
10
10
"github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv"
11
11
)
12
12
13
- var options imgconv.Options
14
13
var 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
+ }
16
30
17
31
func 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)" )
21
38
flag .Parse ()
22
39
23
40
args = flag .Args ()
24
41
}
25
42
26
43
func 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 ))
29
50
}
30
51
52
+ // validate arguments
31
53
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
+ }
33
63
64
+ paths , err := getTargetFilenames (dirnames , from )
34
65
if err != nil {
35
66
onExit (err )
36
67
}
@@ -39,22 +70,26 @@ func main() {
39
70
param := imgconv.ConvertParam {
40
71
Path : path ,
41
72
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 ,
46
77
}
47
78
48
- if ! * options . DryRun {
79
+ if ! dryRun {
49
80
if err := imgconv .Do (param ); err != nil {
50
81
onExit (err )
51
82
}
52
83
} 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 )
54
85
}
55
86
}
56
87
}
57
88
89
+ func isAllowedFileType (ft string ) bool {
90
+ return allowedExts [ft ]
91
+ }
92
+
58
93
func onExit (err error ) {
59
94
fmt .Fprintln (os .Stderr , err )
60
95
os .Exit (1 )
@@ -75,18 +110,12 @@ func uniq([]string) []string {
75
110
return u
76
111
}
77
112
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
80
115
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
-
87
116
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 )
90
119
names = append (names , n )
91
120
}
92
121
return nil
@@ -103,6 +132,7 @@ func isDir(path string) (bool, error) {
103
132
if err != nil {
104
133
return false , err
105
134
}
135
+ defer f .Close ()
106
136
107
137
fi , err := f .Stat ()
108
138
if err != nil {
0 commit comments