-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.go
32 lines (25 loc) · 826 Bytes
/
fs.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
package fsys
import "fmt"
func GetFileContent(dirPath, filename string) ([]byte, error) {
fs, err := getInstance().FS(dirPath)
if err != nil {
return nil, fmt.Errorf("unsupported file system for path '%s': '%w'", dirPath, err)
}
file, err := fs.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening the file '%s': '%w'", filename, err)
}
defer file.Close()
info, err := file.Stat()
if err != nil {
return nil, fmt.Errorf("unexpected error reading the file info for file '%s': '%w'", filename, err)
}
if info.IsDir() {
return nil, fmt.Errorf("the provided path is a directory not a file")
}
bytes := make([]byte, info.Size())
if _, err := file.Read(bytes); err != nil {
return nil, fmt.Errorf("error reading the file content for '%s': '%w'", filename, err)
}
return bytes, nil
}