Skip to content

Commit 1f80c70

Browse files
committed
Implement to check whether filenames passed from args is directory or not
1 parent 7ec6ad2 commit 1f80c70

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

kadai2/tanaka0325/imgconv/imgconv.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ func Run(options Options, args Args) error {
4747

4848
func getTargetFilePaths(args Args, from string) ([]string, error) {
4949
uns := args.uniq()
50-
paths := []string{}
5150

51+
paths := []string{}
5252
for _, n := range uns {
53+
b, err := isDir(n)
54+
if err != nil {
55+
return nil, err
56+
}
57+
if !b {
58+
return nil, fmt.Errorf("%s is not a directory", n)
59+
}
60+
5361
if err := filepath.Walk(n, func(path string, info os.FileInfo, err error) error {
5462
if filepath.Ext(path) == "."+from {
5563
paths = append(paths, path)
@@ -63,6 +71,20 @@ func getTargetFilePaths(args Args, from string) ([]string, error) {
6371
return paths, nil
6472
}
6573

74+
func isDir(path string) (bool, error) {
75+
f, err := os.Open(path)
76+
if err != nil {
77+
return false, err
78+
}
79+
80+
fi, err := f.Stat()
81+
if err != nil {
82+
return false, err
83+
}
84+
85+
return fi.IsDir(), nil
86+
}
87+
6688
func createConvImages(paths []string, from, to string) ([]convImage, error) {
6789
images := []convImage{}
6890
for _, p := range paths {

0 commit comments

Comments
 (0)