44 lines
1.2 KiB
Go
44 lines
1.2 KiB
Go
package ezconf
|
|
|
|
// Integration is an interface that packages can implement to provide
|
|
// easy integration with ezconf
|
|
type Integration interface {
|
|
// Name returns the name to use when registering the config
|
|
Name() string
|
|
|
|
// ConfigPointer returns a pointer to the config struct for tag parsing
|
|
ConfigPointer() any
|
|
|
|
// ConfigFunc returns the ConfigFromEnv function
|
|
ConfigFunc() func() (any, error)
|
|
|
|
// GroupName returns the display name for grouping environment variables
|
|
GroupName() string
|
|
}
|
|
|
|
// RegisterIntegration registers a package that implements the Integration interface
|
|
func (cl *ConfigLoader) RegisterIntegration(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
|
|
}
|
|
|
|
// RegisterIntegrations registers multiple integrations at once
|
|
func (cl *ConfigLoader) RegisterIntegrations(integrations ...Integration) error {
|
|
for _, integration := range integrations {
|
|
if err := cl.RegisterIntegration(integration); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|