/
dir.go
85 lines (68 loc) · 1.88 KB
/
dir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package utils
import (
"fmt"
"os"
"path/filepath"
)
func IsValidDirE(path string) error {
// Clean the path to remove any unnecessary parts.
cleanPath := filepath.Clean(path)
// Use os.Stat to check if the path exists and get file info.
info, err := os.Stat(cleanPath)
if err != nil {
if os.IsNotExist(err) {
// The path does not exist.
return fmt.Errorf("path does not exist: %s, error: %w", cleanPath, err)
}
// There was some problem accessing the path.
return fmt.Errorf("failed to stat the path: %w", err)
}
// Check if the path is a directory.
if !info.IsDir() {
// The path is not a directory.
return fmt.Errorf("path is not a directory: %s", cleanPath)
}
// The path is a valid directory.
return nil
}
func DirExistAndHasContent(dirPath string) error {
if dirPath == "" {
return fmt.Errorf("directory path cannot be empty")
}
currentDir, _ := os.Getwd()
_, err := os.Stat(dirPath)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("directory %s does not exist in current directory %s", dirPath, currentDir)
}
return fmt.Errorf("unexpected error when checking the directory %s: %v", dirPath, err)
}
return nil
}
func DirHasContentWithCertainExtension(dirPath string, extensions []string) error {
if dirPath == "" {
return fmt.Errorf("directory path cannot be empty")
}
if len(extensions) == 0 {
return fmt.Errorf("extensions cannot be empty")
}
err := DirExistAndHasContent(dirPath)
if err != nil {
return err
}
files, err := os.ReadDir(dirPath)
if err != nil {
return fmt.Errorf("failed to read the directory %s: %v", dirPath, err)
}
for _, file := range files {
if file.IsDir() {
continue
}
for _, ext := range extensions {
if filepath.Ext(file.Name()) == ext {
return nil
}
}
}
return fmt.Errorf("directory %s does not contain any files with the following extensions: %v", dirPath, extensions)
}