|
|
|
@@ -1,8 +1,24 @@
|
|
|
|
package ezconf
|
|
|
|
package ezconf
|
|
|
|
|
|
|
|
|
|
|
|
// Integration is an interface that packages can implement to provide
|
|
|
|
type Integration struct {
|
|
|
|
|
|
|
|
Name string
|
|
|
|
|
|
|
|
ConfigPointer any
|
|
|
|
|
|
|
|
ConfigFunc func() (any, error)
|
|
|
|
|
|
|
|
GroupName string
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewIntegration(name, groupname string, cfgptr any, cfgfunc func() (any, error)) *Integration {
|
|
|
|
|
|
|
|
return &Integration{
|
|
|
|
|
|
|
|
name,
|
|
|
|
|
|
|
|
cfgptr,
|
|
|
|
|
|
|
|
cfgfunc,
|
|
|
|
|
|
|
|
groupname,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// IntegrationDepr is an interface that packages can implement to provide
|
|
|
|
// easy integration with ezconf
|
|
|
|
// easy integration with ezconf
|
|
|
|
type Integration interface {
|
|
|
|
type IntegrationDepr interface {
|
|
|
|
// Name returns the name to use when registering the config
|
|
|
|
// Name returns the name to use when registering the config
|
|
|
|
Name() string
|
|
|
|
Name() string
|
|
|
|
|
|
|
|
|
|
|
|
@@ -16,8 +32,34 @@ type Integration interface {
|
|
|
|
GroupName() string
|
|
|
|
GroupName() string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddIntegration registers a package using an Integration object returned by another package
|
|
|
|
|
|
|
|
func (cl *ConfigLoader) AddIntegration(integration *Integration) error {
|
|
|
|
|
|
|
|
// Add config struct for tag parsing
|
|
|
|
|
|
|
|
configPtr := integration.ConfigPointer
|
|
|
|
|
|
|
|
if err := cl.AddConfigStruct(configPtr, integration.GroupName); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add config function
|
|
|
|
|
|
|
|
if err := cl.AddConfigFunc(integration.Name, integration.ConfigFunc); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AddIntegrations registers multiple integrations at once
|
|
|
|
|
|
|
|
func (cl *ConfigLoader) AddIntegrations(integrations ...*Integration) error {
|
|
|
|
|
|
|
|
for _, integration := range integrations {
|
|
|
|
|
|
|
|
if err := cl.AddIntegration(integration); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RegisterIntegration registers a package that implements the Integration interface
|
|
|
|
// RegisterIntegration registers a package that implements the Integration interface
|
|
|
|
func (cl *ConfigLoader) RegisterIntegration(integration Integration) error {
|
|
|
|
func (cl *ConfigLoader) RegisterIntegration(integration IntegrationDepr) error {
|
|
|
|
// Add config struct for tag parsing
|
|
|
|
// Add config struct for tag parsing
|
|
|
|
configPtr := integration.ConfigPointer()
|
|
|
|
configPtr := integration.ConfigPointer()
|
|
|
|
if err := cl.AddConfigStruct(configPtr, integration.GroupName()); err != nil {
|
|
|
|
if err := cl.AddConfigStruct(configPtr, integration.GroupName()); err != nil {
|
|
|
|
@@ -33,7 +75,7 @@ func (cl *ConfigLoader) RegisterIntegration(integration Integration) error {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RegisterIntegrations registers multiple integrations at once
|
|
|
|
// RegisterIntegrations registers multiple integrations at once
|
|
|
|
func (cl *ConfigLoader) RegisterIntegrations(integrations ...Integration) error {
|
|
|
|
func (cl *ConfigLoader) RegisterIntegrations(integrations ...IntegrationDepr) error {
|
|
|
|
for _, integration := range integrations {
|
|
|
|
for _, integration := range integrations {
|
|
|
|
if err := cl.RegisterIntegration(integration); err != nil {
|
|
|
|
if err := cl.RegisterIntegration(integration); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
|