Skip to content

Commit a3c8380

Browse files
committed
return error in package
1 parent 190db55 commit a3c8380

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

kadai1/imura81gt/imgconv/img/convert.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ func (i ImageType) Enable() []string {
4242

4343
// ConvertAll convert all image files in dirs
4444
func ConvertAll(dirs []string, iType ImageType, oType ImageType) error {
45-
files := AllImageFiles(dirs)
45+
files, err := AllImageFiles(dirs)
46+
if err != nil {
47+
return err
48+
}
4649
files = ExpectedImageFiles(files, iType)
4750
fmt.Printf("%q\n", files)
4851

@@ -54,13 +57,13 @@ func ConvertAll(dirs []string, iType ImageType, oType ImageType) error {
5457
i, err := os.Open(iPath)
5558
if err != nil {
5659
fmt.Fprintln(os.Stderr, err)
57-
os.Exit(1)
60+
return err
5861
}
5962

6063
err = os.MkdirAll(filepath.Dir(oPath), 0755)
6164
if err != nil {
6265
fmt.Fprintln(os.Stderr, err)
63-
os.Exit(1)
66+
return err
6467
}
6568

6669
w, err := os.Create(oPath)
@@ -73,13 +76,13 @@ func ConvertAll(dirs []string, iType ImageType, oType ImageType) error {
7376
}()
7477
if err != nil {
7578
fmt.Fprintln(os.Stderr, err)
76-
os.Exit(1)
79+
return err
7780
}
7881

7982
err = Convert(i, w, t)
8083
if err != nil {
8184
fmt.Fprintln(os.Stderr, err)
82-
os.Exit(1)
85+
return err
8386
}
8487
oFiles = append(oFiles, oPath)
8588
}
@@ -88,8 +91,9 @@ func ConvertAll(dirs []string, iType ImageType, oType ImageType) error {
8891
return nil
8992
}
9093

91-
func AllImageFiles(dirs []string) (files []string) {
94+
func AllImageFiles(dirs []string) ([]string, error) {
9295
// 3. ディレクトリ以下は再帰的に処理する
96+
var files []string
9397
for _, dir := range dirs {
9498
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
9599
if info.IsDir() {
@@ -104,10 +108,10 @@ func AllImageFiles(dirs []string) (files []string) {
104108
})
105109
if err != nil {
106110
fmt.Fprintln(os.Stderr, err)
107-
os.Exit(1)
111+
return nil, err
108112
}
109113
}
110-
return files
114+
return files, nil
111115
}
112116

113117
func ExpectedImageFiles(files []string, iType ImageType) (f []string) {
@@ -125,7 +129,7 @@ func IsImage(path string) bool {
125129
r, err := os.Open(path)
126130
if err != nil {
127131
fmt.Fprintln(os.Stderr, err)
128-
os.Exit(1)
132+
return false
129133
}
130134
defer r.Close()
131135

@@ -140,7 +144,7 @@ func IsExpectedImage(path string, iType ImageType) bool {
140144
r, err := os.Open(path)
141145
if err != nil {
142146
fmt.Fprintln(os.Stderr, err)
143-
os.Exit(1)
147+
return false
144148
}
145149
defer r.Close()
146150

0 commit comments

Comments
 (0)