-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.go
68 lines (60 loc) · 1.87 KB
/
all.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
// The all package facilitates the registration of Juju components into
// the relevant machinery. It is intended as the one place in Juju where
// the components (horizontal design layers) and the machinery
// (vertical/architectural layers) intersect. This approach helps
// alleviate interdependence between the components and the machinery.
//
// This is done in an independent package to avoid circular imports.
package all
import (
"github.com/juju/errors"
"github.com/juju/utils/set"
)
type component interface {
registerForServer() error
registerForClient() error
}
var components = []component{
&payloads{},
&resources{},
}
// RegisterForServer registers all the parts of the components with the
// Juju machinery for use as a server (e.g. jujud, jujuc).
func RegisterForServer() error {
for _, c := range components {
if err := c.registerForServer(); err != nil {
return errors.Trace(err)
}
}
return nil
}
// RegisterForClient registers all the parts of the components with the
// Juju machinery for use as a client (e.g. juju).
func RegisterForClient() error {
for _, c := range components {
if err := c.registerForClient(); err != nil {
return errors.Trace(err)
}
}
return nil
}
// registered tracks which parts of each component have been registered.
var registered = map[string]set.Strings{}
// markRegistered helps components track which things they've
// registered. If the part has already been registered then false is
// returned, indicating that marking failed. This way components can
// ensure a part is registered only once.
func markRegistered(component, part string) bool {
parts, ok := registered[component]
if !ok {
parts = set.NewStrings()
registered[component] = parts
}
if parts.Contains(part) {
return false
}
parts.Add(part)
return true
}