-
Notifications
You must be signed in to change notification settings - Fork 158
/
read.go
47 lines (36 loc) · 959 Bytes
/
read.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 types
import (
"context"
"fmt"
"io/ioutil"
"os"
)
// ReadHatchetYAMLFileBytes reads a given YAML file from a filepath and return the parsed workflow file
func ReadHatchetYAMLFileBytes(filepath string) (*Workflow, error) {
yamlFileBytes, err := readHatchetYAMLFileBytes(filepath)
if err != nil {
return nil, err
}
workflowFile, err := ParseYAML(context.Background(), yamlFileBytes)
if err != nil {
return nil, err
}
return &workflowFile, nil
}
func readHatchetYAMLFileBytes(filepath string) ([]byte, error) {
if !fileExists(filepath) {
return nil, fmt.Errorf("file does not exist: %s", filepath)
}
yamlFileBytes, err := ioutil.ReadFile(filepath) // #nosec G304 -- files are meant to be read from user-supplied directory
if err != nil {
panic(err)
}
return yamlFileBytes, nil
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}