e4.0.0 Refactored, file watcher and improved builder, doc update

This commit is contained in:
2025-07-17 03:44:08 -04:00
parent 16dc829fd5
commit 2934ea9548
25 changed files with 3567 additions and 1828 deletions

View File

@ -86,16 +86,22 @@ func (c *Config) GenerateFlags() *flag.FlagSet {
// BindFlags updates configuration from parsed flag.FlagSet
func (c *Config) BindFlags(fs *flag.FlagSet) error {
var errors []error
needsInvalidation := false
fs.Visit(func(f *flag.Flag) {
value := f.Value.String()
parsed := parseValue(value)
if err := c.SetSource(f.Name, SourceCLI, parsed); err != nil {
// Let mapstructure handle type conversion
if err := c.SetSource(f.Name, SourceCLI, value); err != nil {
errors = append(errors, fmt.Errorf("flag %s: %w", f.Name, err))
} else {
needsInvalidation = true
}
})
if needsInvalidation {
c.invalidateCache() // Batch invalidation after all flags
}
if len(errors) > 0 {
return fmt.Errorf("failed to bind %d flags: %w", len(errors), errors[0])
}
@ -141,16 +147,6 @@ func (c *Config) Validate(required ...string) error {
return nil
}
// Watch returns a channel that receives updates when configuration values change
// This is useful for hot-reloading configurations
// Note: This is a placeholder for future enhancement
func (c *Config) Watch() <-chan string {
// TODO: Implement file watching and config reload
ch := make(chan string)
close(ch) // Close immediately for now
return ch
}
// Debug returns a formatted string showing all configuration values and their sources
func (c *Config) Debug() string {
c.mutex.RLock()
@ -228,4 +224,13 @@ func (c *Config) Clone() *Config {
}
return clone
}
// QuickTyped creates a fully configured Config with a typed target
func QuickTyped[T any](target *T, envPrefix, configFile string) (*Config, error) {
return NewBuilder().
WithTarget(target).
WithEnvPrefix(envPrefix).
WithFile(configFile).
Build()
}