github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/hook/hooks.go (about) 1 package hook 2 3 import "fmt" 4 5 type HookSet map[Phase]Hook 6 type Hook func() 7 8 type Phase string 9 10 const ( 11 PARENT_BEFORE_CLONE Phase = "parent-before-clone" 12 PARENT_AFTER_CLONE Phase = "parent-after-clone" 13 CHILD_AFTER_PIVOT Phase = "child-after-pivot" 14 ) 15 16 var DefaultHookSet HookSet = make(map[Phase]Hook) 17 18 func Main(args []string) { 19 DefaultHookSet.Main(Phase(args[0])) 20 } 21 22 func Register(name Phase, fn Hook) { 23 DefaultHookSet.Register(name, fn) 24 } 25 26 func (h HookSet) Main(phase Phase) { 27 if fn, ok := h[phase]; ok { 28 fn() 29 } else { 30 panic(fmt.Sprintf("hooks: no such hook: %s", phase)) 31 } 32 } 33 34 func (h HookSet) Register(name Phase, fn Hook) { 35 if _, exists := h[name]; exists { 36 panic(fmt.Sprintf("hooks: already registered hook: %s", name)) 37 } 38 39 h[name] = fn 40 }