diff --git a/pkg/daemon/feature.go b/pkg/daemon/feature.go index d9a0a1fa..3e892130 100644 --- a/pkg/daemon/feature.go +++ b/pkg/daemon/feature.go @@ -6,8 +6,8 @@ import ( ) var ( - Features = map[string]*FeaturePlugin{} - plugins []*FeaturePlugin + features = map[string]*FeaturePlugin{} + featuresSorted []*FeaturePlugin ) type FeaturePlugin struct { @@ -27,17 +27,25 @@ type Feature interface { Close() error } +func RegisterFeature(name, desc string, New func(i *Interface) (Feature, error), order int) { + features[name] = &FeaturePlugin{ + Name: name, + Description: desc, + New: New, + Order: order, + } +} + func SortedFeatures() []*FeaturePlugin { - if plugins == nil { - for name, feat := range Features { - feat.Name = name - plugins = append(plugins, feat) + if featuresSorted == nil { + for _, feat := range features { + featuresSorted = append(featuresSorted, feat) } } - slices.SortFunc(plugins, func(a, b *FeaturePlugin) bool { + slices.SortFunc(featuresSorted, func(a, b *FeaturePlugin) bool { return a.Order < b.Order }) - return plugins + return featuresSorted } diff --git a/pkg/daemon/feature/autocfg/autocfg.go b/pkg/daemon/feature/autocfg/autocfg.go index 1a0c8d90..758317c8 100644 --- a/pkg/daemon/feature/autocfg/autocfg.go +++ b/pkg/daemon/feature/autocfg/autocfg.go @@ -18,11 +18,7 @@ import ( ) func init() { - daemon.Features["autocfg"] = &daemon.FeaturePlugin{ - New: New, - Description: "Auto-configuration", - Order: 10, - } + daemon.RegisterFeature("autocfg", "Auto configuration", New, 10) } type Interface struct { diff --git a/pkg/daemon/feature/cfgsync/cfgsync.go b/pkg/daemon/feature/cfgsync/cfgsync.go index fc66576a..2f840491 100644 --- a/pkg/daemon/feature/cfgsync/cfgsync.go +++ b/pkg/daemon/feature/cfgsync/cfgsync.go @@ -16,11 +16,7 @@ import ( ) func init() { - daemon.Features["cfgsync"] = &daemon.FeaturePlugin{ - New: New, - Description: "Config synchronization", - Order: 20, - } + daemon.RegisterFeature("cfgsync", "Config synchronization", New, 20) } type Interface struct { diff --git a/pkg/daemon/feature/epdisc/epdisc.go b/pkg/daemon/feature/epdisc/epdisc.go index 4e7822b4..66fd6057 100644 --- a/pkg/daemon/feature/epdisc/epdisc.go +++ b/pkg/daemon/feature/epdisc/epdisc.go @@ -23,11 +23,7 @@ import ( ) func init() { - daemon.Features["epdisc"] = &daemon.FeaturePlugin{ - New: New, - Description: "Endpoint discovery", - Order: 50, - } + daemon.RegisterFeature("epdisc", "Endpoint discovery", New, 50) } type Interface struct { diff --git a/pkg/daemon/feature/hooks/hooks.go b/pkg/daemon/feature/hooks/hooks.go index 52733f03..be498f4b 100644 --- a/pkg/daemon/feature/hooks/hooks.go +++ b/pkg/daemon/feature/hooks/hooks.go @@ -9,11 +9,7 @@ import ( ) func init() { - daemon.Features["hooks"] = &daemon.FeaturePlugin{ - New: New, - Description: "Hooks", - Order: 70, - } + daemon.RegisterFeature("hooks", "Hooks", New, 70) } type Hook interface { diff --git a/pkg/daemon/feature/hsync/hsync.go b/pkg/daemon/feature/hsync/hsync.go index 8b5d2280..466951eb 100644 --- a/pkg/daemon/feature/hsync/hsync.go +++ b/pkg/daemon/feature/hsync/hsync.go @@ -18,11 +18,7 @@ const ( ) func init() { - daemon.Features["hsync"] = &daemon.FeaturePlugin{ - New: New, - Description: "Hosts synchronization", - Order: 200, - } + daemon.RegisterFeature("hsync", "Hosts synchronization", New, 200) } type Interface struct { diff --git a/pkg/daemon/feature/pdisc/pdisc.go b/pkg/daemon/feature/pdisc/pdisc.go index afa637e6..3e6864f2 100644 --- a/pkg/daemon/feature/pdisc/pdisc.go +++ b/pkg/daemon/feature/pdisc/pdisc.go @@ -19,11 +19,7 @@ import ( ) func init() { - daemon.Features["pdisc"] = &daemon.FeaturePlugin{ - New: New, - Description: "Peer discovery", - Order: 60, - } + daemon.RegisterFeature("pdisc", "Peer discovery", New, 60) } type Interface struct { diff --git a/pkg/daemon/feature/rtsync/rtsync.go b/pkg/daemon/feature/rtsync/rtsync.go index b1712813..916b166c 100644 --- a/pkg/daemon/feature/rtsync/rtsync.go +++ b/pkg/daemon/feature/rtsync/rtsync.go @@ -10,11 +10,7 @@ import ( ) func init() { - daemon.Features["rtsync"] = &daemon.FeaturePlugin{ - New: New, - Description: "Route synchronization", - Order: 30, - } + daemon.RegisterFeature("rtsync", "Route synchronization", New, 30) } type Interface struct {