-
Notifications
You must be signed in to change notification settings - Fork 96
/
system.go
84 lines (75 loc) · 2.42 KB
/
system.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
// Copyright (c) 2018, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package mount
import (
"fmt"
)
// hookFn describes function prototype for function
// to be called before/after mounting a tag list
type hookFn func(*System) error
// mountFn describes function prototype for function responsible
// of mount operation
type mountFn func(*Point) error
// System defines a mount system allowing to register before/after
// hook functions for specific tag during mount phase
type System struct {
Points *Points
Mount mountFn
beforeTagHooks map[AuthorizedTag][]hookFn
afterTagHooks map[AuthorizedTag][]hookFn
}
func (b *System) init() {
if b.beforeTagHooks == nil {
b.beforeTagHooks = make(map[AuthorizedTag][]hookFn, 0)
}
if b.afterTagHooks == nil {
b.afterTagHooks = make(map[AuthorizedTag][]hookFn, 0)
}
}
// RunBeforeTag registers a hook function executed before mounting points
// of tag list
func (b *System) RunBeforeTag(tag AuthorizedTag, fn hookFn) error {
if _, ok := authorizedTags[tag]; !ok {
return fmt.Errorf("tag %s is not an authorized tag", tag)
}
b.init()
b.beforeTagHooks[tag] = append(b.beforeTagHooks[tag], fn)
return nil
}
// RunAfterTag registers a hook function executed after mounting points
// of tag list
func (b *System) RunAfterTag(tag AuthorizedTag, fn hookFn) error {
if _, ok := authorizedTags[tag]; !ok {
return fmt.Errorf("tag %s is not an authorized tag", tag)
}
b.init()
b.afterTagHooks[tag] = append(b.afterTagHooks[tag], fn)
return nil
}
// MountAll iterates over mount point list and mounts every point
// by calling hook before/after hook functions
func (b *System) MountAll() error {
b.init()
for _, tag := range GetTagList() {
for _, fn := range b.beforeTagHooks[tag] {
if err := fn(b); err != nil {
return fmt.Errorf("hook function for tag %s returns error: %s", tag, err)
}
}
for _, point := range b.Points.GetByTag(tag) {
if b.Mount != nil {
if err := b.Mount(&point); err != nil {
return fmt.Errorf("mount %s->%s error: %s", point.Source, point.Destination, err)
}
}
}
for _, fn := range b.afterTagHooks[tag] {
if err := fn(b); err != nil {
return fmt.Errorf("hook function for tag %s returns error: %s", tag, err)
}
}
}
return nil
}