From 64cc9baaefbc8b6141cbb2dd34e8d589843c91b87b21417ebce4cc5ec46939c5 Mon Sep 17 00:00:00 2001 From: Lixen Wraith Date: Sat, 12 Jul 2025 13:00:30 -0400 Subject: [PATCH] e3.0.1 Bug fix. --- .gitignore | 2 ++ builder.go | 15 +++++++++++---- convenience.go | 6 +++--- go.mod | 2 +- register.go | 4 ++-- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 587e5df..bacfe38 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,7 @@ data dev log +logs +script *.log bin diff --git a/builder.go b/builder.go index 76c2dbb..a9dbf2b 100644 --- a/builder.go +++ b/builder.go @@ -2,6 +2,7 @@ package config import ( + "errors" "fmt" "os" ) @@ -10,7 +11,7 @@ import ( type Builder struct { cfg *Config opts LoadOptions - defaults interface{} + defaults any prefix string file string args []string @@ -27,7 +28,7 @@ func NewBuilder() *Builder { } // WithDefaults sets the struct containing default values -func (b *Builder) WithDefaults(defaults interface{}) *Builder { +func (b *Builder) WithDefaults(defaults any) *Builder { b.defaults = defaults return b } @@ -94,7 +95,9 @@ func (b *Builder) Build() (*Config, error) { // Load configuration if err := b.cfg.LoadWithOptions(b.file, b.args, b.opts); err != nil { - return nil, err + // The error might be non-fatal (e.g., file not found). + // Return the config object so it can be used with other sources. + return b.cfg, err } return b.cfg, nil @@ -104,7 +107,11 @@ func (b *Builder) Build() (*Config, error) { func (b *Builder) MustBuild() *Config { cfg, err := b.Build() if err != nil { - panic(fmt.Sprintf("config build failed: %v", err)) + // Ignore ErrConfigNotFound as it is not a fatal error for MustBuild. + // The application can proceed with defaults/env vars. + if !errors.Is(err, ErrConfigNotFound) { + panic(fmt.Sprintf("config build failed: %v", err)) + } } return cfg } \ No newline at end of file diff --git a/convenience.go b/convenience.go index 78b7aab..7dec015 100644 --- a/convenience.go +++ b/convenience.go @@ -12,7 +12,7 @@ import ( // Quick creates a fully configured Config instance with a single call // This is the recommended way to initialize configuration for most applications -func Quick(structDefaults interface{}, envPrefix, configFile string) (*Config, error) { +func Quick(structDefaults any, envPrefix, configFile string) (*Config, error) { cfg := New() // Register defaults from struct if provided @@ -31,7 +31,7 @@ func Quick(structDefaults interface{}, envPrefix, configFile string) (*Config, e } // QuickCustom creates a Config with custom options -func QuickCustom(structDefaults interface{}, opts LoadOptions, configFile string) (*Config, error) { +func QuickCustom(structDefaults any, opts LoadOptions, configFile string) (*Config, error) { cfg := NewWithOptions(opts) // Register defaults from struct if provided @@ -46,7 +46,7 @@ func QuickCustom(structDefaults interface{}, opts LoadOptions, configFile string } // MustQuick is like Quick but panics on error -func MustQuick(structDefaults interface{}, envPrefix, configFile string) *Config { +func MustQuick(structDefaults any, envPrefix, configFile string) *Config { cfg, err := Quick(structDefaults, envPrefix, configFile) if err != nil { panic(fmt.Sprintf("config initialization failed: %v", err)) diff --git a/go.mod b/go.mod index 2f379b8..6db8229 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/lixenwraith/config -go 1.24.2 +go 1.24.5 require ( github.com/BurntSushi/toml v1.5.0 diff --git a/register.go b/register.go index 856a97f..eeb466d 100644 --- a/register.go +++ b/register.go @@ -101,7 +101,7 @@ func (c *Config) Unregister(path string) error { // RegisterStruct registers configuration values derived from a struct. // It uses struct tags (`toml:"..."`) to determine the configuration paths. // The prefix is prepended to all paths (e.g., "log."). An empty prefix is allowed. -func (c *Config) RegisterStruct(prefix string, structWithDefaults interface{}) error { +func (c *Config) RegisterStruct(prefix string, structWithDefaults any) error { v := reflect.ValueOf(structWithDefaults) // Handle pointer or direct struct value @@ -129,7 +129,7 @@ func (c *Config) RegisterStruct(prefix string, structWithDefaults interface{}) e } // RegisterStructWithTags is like RegisterStruct but allows custom tag names -func (c *Config) RegisterStructWithTags(prefix string, structWithDefaults interface{}, tagName string) error { +func (c *Config) RegisterStructWithTags(prefix string, structWithDefaults any, tagName string) error { // Save current tag preference oldTag := "toml"