-
Notifications
You must be signed in to change notification settings - Fork 4
/
reader.go
61 lines (56 loc) · 1.3 KB
/
reader.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
package reader
import (
"os"
"path"
"path/filepath"
)
// Inputs models a set of files (Schema files and other files) that have been found in a
// given directory.
type Inputs struct {
Directory string
SchemaFiles []*SchemaFile
}
type SchemaFile struct {
FileName string
Contents string
}
// FromDir constructs an Inputs instance by selecting relevant
// files from the given directory.
//
// So far it only looks for *.keel files and puts those in the SchemaFiles field.
func FromDir(dirName string) (*Inputs, error) {
inputs := &Inputs{
Directory: dirName,
SchemaFiles: []*SchemaFile{},
}
globPattern := filepath.Join(dirName, "*.keel")
schemaFileNames, err := filepath.Glob(globPattern)
if err != nil {
return nil, err
}
for _, fName := range schemaFileNames {
fileBytes, err := os.ReadFile(fName)
if err != nil {
return nil, err
}
inputs.SchemaFiles = append(inputs.SchemaFiles, &SchemaFile{
FileName: fName,
Contents: string(fileBytes),
})
}
return inputs, nil
}
func FromFile(filename string) (*Inputs, error) {
fileBytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
schemaFile := &SchemaFile{
FileName: filename,
Contents: string(fileBytes),
}
return &Inputs{
Directory: path.Dir(filename),
SchemaFiles: []*SchemaFile{schemaFile},
}, nil
}