-
Notifications
You must be signed in to change notification settings - Fork 126
/
files.go
47 lines (35 loc) · 1.1 KB
/
files.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
package loaderutils
import (
"fmt"
"os"
)
func GetConfigBytes(configFilePath string) ([][]byte, error) {
configFileBytes := make([][]byte, 0)
if fileExists(configFilePath) {
fileBytes, err := os.ReadFile(configFilePath) // #nosec G304 -- config files are meant to be read from user-supplied directory
if err != nil {
return nil, fmt.Errorf("could not read config file at path %s: %w", configFilePath, err)
}
configFileBytes = append(configFileBytes, fileBytes)
}
return configFileBytes, nil
}
func GetFileBytes(filename string) ([]byte, error) {
if fileExists(filename) {
fileBytes, err := os.ReadFile(filename) // #nosec G304 -- config files are meant to be read from user-supplied directory
if err != nil {
return nil, fmt.Errorf("could not read config file at path %s: %w", filename, err)
}
return fileBytes, nil
}
return nil, fmt.Errorf("could not read config file at path %s", filename)
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil && os.IsNotExist(err) {
return false
} else if err != nil {
return false
}
return !info.IsDir()
}