/
file.go
52 lines (42 loc) · 1.28 KB
/
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
package transport
import (
"context"
"os"
"path/filepath"
"github.com/go-logr/logr"
"github.com/tinkerbell/tink/internal/agent/event"
"github.com/tinkerbell/tink/internal/agent/workflow"
"gopkg.in/yaml.v2"
)
// File is a transport implementation that executes a single workflow stored as a file.
type File struct {
// Log is a logger for debugging.
Log logr.Logger
// Path to the workflow to run.
Path string
}
// Start begins watching f.Dir for files. When it finds a file it hasn't handled before, it
// attempts to parse it and offload to the handler. It will run workflows once where a workflow
// is determined by its file name.
func (f *File) Start(ctx context.Context, _ string, handler WorkflowHandler) error {
path, err := filepath.Abs(f.Path)
if err != nil {
return err
}
fh, err := os.Open(path)
if err != nil {
return err
}
var wrkflow workflow.Workflow
if err := yaml.NewDecoder(fh).Decode(&wrkflow); err != nil {
return err
}
handler.HandleWorkflow(ctx, wrkflow, f)
return nil
}
func (f *File) RecordEvent(_ context.Context, e event.Event) error {
// Noop because we don't particularly care about events for File based transports. Maybe
// we'll record this in a dedicated file one day.
f.Log.Info("Recording event", "event", e.GetName())
return nil
}