-
Notifications
You must be signed in to change notification settings - Fork 145
/
read_all.go
73 lines (55 loc) · 1.48 KB
/
read_all.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
package types
import (
"context"
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"strings"
)
func DefaultLoader() []*Workflow {
workflowFiles, err := ReadAllValidFilesInDir("./.hatchet")
if err != nil {
panic(err)
}
return workflowFiles
}
func ReadAllValidFilesInDir(filedir string) ([]*Workflow, error) {
files, err := readYAMLFiles(filedir)
if err != nil {
return nil, err
}
var workflowFiles []*Workflow
for _, file := range files {
workflowFile, err := ParseYAML(context.Background(), file)
if err != nil {
continue
}
workflowFiles = append(workflowFiles, &workflowFile)
}
return workflowFiles, nil
}
// readYAMLFiles reads all .yaml files in a given directory, including subdirectories.
func readYAMLFiles(rootDir string) ([][]byte, error) {
yamlFiles := make([][]byte, 0)
// Walk the directory tree
err := filepath.WalkDir(rootDir, func(path string, info fs.DirEntry, err error) error {
if err != nil {
return err
}
// Check if the file is a YAML file
if !info.IsDir() && (strings.HasSuffix(info.Name(), ".yaml") || strings.HasSuffix(info.Name(), ".yml")) {
// Read the file
data, err := ioutil.ReadFile(path) // #nosec G304 -- files are meant to be read from user-supplied directory
if err != nil {
return fmt.Errorf("error reading file %s: %v", path, err)
}
yamlFiles = append(yamlFiles, data)
}
return nil
})
if err != nil {
return nil, fmt.Errorf("error walking the path %s: %v", rootDir, err)
}
return yamlFiles, nil
}