-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocedures.go
81 lines (68 loc) · 2.32 KB
/
procedures.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
package doze
import (
"fmt"
"strings"
)
// Procedure is an interface that should be implemented by the operator to customize the type
// of work that doze can do. A Procedure can only be called from doze after being registered.
type Procedure interface {
GetProcedureInfo() ProcedureInfo
Execute(*Rule) error
}
// A ProcedureInfo represents a registered doze Procedure.
type ProcedureInfo struct {
// ID is the full name of the Procedure. It is unique and namespaced.
ID ProcedureID
// New returns a pointer to a new instance of the Procedure.
New func() Procedure
}
// ProcedureID is a string that uniquely identifies a doze Procedure.
// It is made up of colon separated labels which form a hierarchy from left to right,
// with the last label making up the Procedure name and the labels before that making
// its namespace.
type ProcedureID string
// Namespace returns the namespace portion of a ProcedureID if it exists or an empty string.
func (id ProcedureID) Namespace() string {
lastSeparator := strings.LastIndex(string(id), procedureIDSeparator)
if lastSeparator < 0 {
return ""
}
return string(id)[:lastSeparator]
}
// Name returns the name portion of a ProcedureID.
func (id ProcedureID) Name() string {
if id == "" {
return ""
}
portions := strings.Split(string(id), procedureIDSeparator)
return portions[len(portions)-1]
}
// RegisterProcedure registers a new Procedure in the doze global Procedure store.
// Any amount of Procedure instances may be created from it.
// Duplicate Procedures are not accepted.
func RegisterProcedure(instance Procedure) {
procInfo := instance.GetProcedureInfo()
if procInfo.ID == "" {
panic("procedure ID is missing")
}
if procInfo.New == nil {
panic("missing ProcedureInfo.New")
}
if p := procInfo.New(); p == nil {
panic("ProcedureInfo.New must return a non-nil procedure instance")
}
if _, ok := procedures[procInfo.ID]; ok {
panic(fmt.Sprintf("procedure already registered: '%v'", procInfo.ID))
}
procedures[procInfo.ID] = procInfo
}
// GetProcedure returns a Procedure information from its ID.
func GetProcedure(id ProcedureID) (ProcedureInfo, error) {
p, ok := procedures[id]
if !ok {
return ProcedureInfo{}, fmt.Errorf("procedure not registered: '%v'", id)
}
return p, nil
}
const procedureIDSeparator = ":"
var procedures = make(map[ProcedureID]ProcedureInfo)