-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathregistry.go
93 lines (85 loc) · 2.22 KB
/
registry.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
package fork
import (
"encoding/gob"
"fmt"
"os"
"reflect"
)
// the registry of forks we know about
var forks map[string]*Function
const (
nameVar = "GOFORK_NAME"
argsVar = "GOFORK_ARGS"
)
func init() {
forks = make(map[string]*Function)
}
// Register records a Fork in the internal fork map.
// Register must be called before Fork on agiven function (func init() is a common place)
func Register(f *Function) {
if f.Name == "" {
panic("tried to register fork with no name")
}
forks[f.Name] = f
}
// RegisterFunc records a function as a fork in the internal fork map.
// Register must be called before Fork on agiven function (func init() is a common place)
func RegisterFunc(n string, fn interface{}) {
Register(NewFork(n, fn))
}
// Init should be called at the point that forks should begin to execute.
// This should likely be very early in main() or within init() (to skip main entirely)
//
// At the point where this is called, either:
//
// We are identified as a fork, we execute that function, and exit.
// or, we are not identified as afork and simply return.
func Init() {
var name string
if name = os.Getenv(nameVar); name == "" {
// no func is defined
return
}
os.Unsetenv(nameVar)
// we appear to be a fork
if f, ok := forks[name]; ok {
v := f.fn
t := v.Type()
args := []reflect.Value{}
if argsFile := os.Getenv(argsVar); argsFile != "" {
// get our arguments
f, err := os.Open(argsFile)
if err != nil {
panic("failed to open args file: " + err.Error())
}
dec := gob.NewDecoder(f)
for i := 0; i < t.NumIn(); i++ {
v := reflect.Indirect(reflect.New(t.In(i)))
err := dec.DecodeValue(v)
if err != nil {
panic("failed to decode arguments from args file: " + err.Error())
}
args = append(args, v)
}
f.Close()
os.Remove(argsFile)
os.Unsetenv(argsVar)
}
if t.NumIn() != len(args) {
panic("fork failed: incorrect number of args supplied")
}
if err := v.Call(args); err != nil {
os.Exit(1)
}
os.Exit(0)
}
panic("no fork by name: " + name)
}
// Fork calls a registered fork
func Fork(name string, args ...interface{}) (err error) {
f, ok := forks[name]
if !ok {
return fmt.Errorf("no registered function by name: %s", name)
}
return f.Fork(args...)
}