-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_file.go
86 lines (72 loc) · 2.2 KB
/
load_file.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
74
75
76
77
78
79
80
81
82
83
84
85
86
package nodes
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
oferrors "github.com/hackborn/onefunc/errors"
"github.com/hackborn/onefunc/pipeline"
)
type LoadFileNode struct {
loadFileData
}
type loadFileData struct {
// Name of the filesystem to read from. If this is an
// empty string, the local filesystem is used, otherwise
// the name must have been previously registered with
// RegisterFs.
Fs string
// Glob pattern used to select which files are loaded.
Glob string
}
func (n *LoadFileNode) Start(input pipeline.StartInput) error {
data := n.loadFileData
input.SetNodeData(&data)
return nil
}
func (n *LoadFileNode) Run(state *pipeline.State, input pipeline.RunInput, output *pipeline.RunOutput) error {
data := state.NodeData.(*loadFileData)
if data.Fs == "" {
return n.runLocal(data, input, output)
}
return n.runFs(data, input, output)
}
func (n *LoadFileNode) runFs(data *loadFileData, input pipeline.RunInput, output *pipeline.RunOutput) error {
fsys, ok := pipeline.FindFs(data.Fs)
if !ok {
return fmt.Errorf("LoadFileNode: no registered filesystem named \"%v\"", data.Fs)
}
eb := &oferrors.FirstBlock{}
matches, err := fs.Glob(fsys, data.Glob)
eb.AddError(err)
for _, fn := range matches {
dat, err := fs.ReadFile(fsys, fn)
eb.AddError(err)
base := path.Base(fn)
output.Pins = append(output.Pins, pipeline.Pin{Payload: &pipeline.ContentData{Name: base, Data: string(dat)}})
}
return eb.Err
}
func (n *LoadFileNode) runLocal(data *loadFileData, input pipeline.RunInput, output *pipeline.RunOutput) error {
eb := &oferrors.FirstBlock{}
matches, err := filepath.Glob(filepath.FromSlash(data.Glob))
eb.AddError(err)
for _, fn := range matches {
dat, err := os.ReadFile(fn)
eb.AddError(err)
base := filepath.Base(fn)
output.Pins = append(output.Pins, pipeline.Pin{Payload: &pipeline.ContentData{Name: base, Data: string(dat)}})
}
return eb.Err
}
func (n *LoadFileNode) getFilenames(fsname string, glob string) ([]string, error) {
if fsname == "" {
return filepath.Glob(filepath.FromSlash(glob))
}
fsys, ok := pipeline.FindFs(fsname)
if !ok {
return nil, fmt.Errorf("LoadFileNode: no registered filesystem named \"%v\"", fsname)
}
return fs.Glob(fsys, glob)
}