-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
context.go
129 lines (99 loc) · 2.52 KB
/
context.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This package provides a context for Tasks
// and a registry for their usage in flows.
package context
import (
gocontext "context"
"io"
"sync"
"cuelang.org/go/cue"
"github.com/hofstadter-io/hof/flow/task"
)
// A Context provides context for running a task.
type Context struct {
RootValue cue.Value
GoContext gocontext.Context
FlowStack []string
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Value cue.Value
Error error
// debug / internal
Verbosity int
Middlewares []Middleware
TaskRegistry *sync.Map
// BOOKKEEPING
Tasks *sync.Map
// experimental
BaseTask *task.BaseTask
// Middleware
Pools *sync.Map
// how can the below become middleware, extensions, plugin?
// Global (for this context, tbd shared) lock around CUE evaluator
CUELock *sync.Mutex
// map of cue.Values
ValStore *sync.Map
// map of chan?
Mailbox *sync.Map
// channels for
// - stats & progress
}
func New() *Context {
return &Context{
GoContext: gocontext.Background(),
FlowStack: []string{},
CUELock: new(sync.Mutex),
ValStore: new(sync.Map),
Mailbox: new(sync.Map),
Middlewares: make([]Middleware, 0),
TaskRegistry: new(sync.Map),
Tasks: new(sync.Map),
Pools: new(sync.Map),
}
}
func Copy(ctx *Context) *Context {
return &Context{
RootValue: ctx.RootValue,
GoContext: ctx.GoContext,
FlowStack: ctx.FlowStack,
Stdin: ctx.Stdin,
Stdout: ctx.Stdout,
Stderr: ctx.Stderr,
Verbosity: ctx.Verbosity,
CUELock: ctx.CUELock,
Mailbox: ctx.Mailbox,
ValStore: ctx.ValStore,
Middlewares: ctx.Middlewares,
TaskRegistry: ctx.TaskRegistry,
Tasks: ctx.Tasks,
Pools: ctx.Pools,
}
}
func (C *Context) Use(m Middleware) {
C.Middlewares = append(C.Middlewares, m)
}
// Register registers a task for cue commands.
func (C *Context) Register(key string, f RunnerFunc) {
C.TaskRegistry.Store(key, f)
}
// Lookup returns the RunnerFunc for a key.
func (C *Context) Lookup(key string) RunnerFunc {
v, ok := C.TaskRegistry.Load(key)
if !ok {
return nil
}
return v.(RunnerFunc)
}
// Middleware to apply to RunnerFuncs
// should wrap and call Run of the passed RunnerFunc?
type Middleware interface {
Apply(*Context, RunnerFunc) RunnerFunc
}
// A RunnerFunc creates a Runner.
type RunnerFunc func(v cue.Value) (Runner, error)
// A Runner defines a task type.
type Runner interface {
// Runner runs given the current value and returns a new value which is to
// be unified with the original result.
Run(ctx *Context) (results interface{}, err error)
}