e6.0.0 Added file format change and security option support.
This commit is contained in:
27
builder.go
27
builder.go
@ -15,6 +15,8 @@ type Builder struct {
|
||||
opts LoadOptions
|
||||
defaults any
|
||||
tagName string
|
||||
fileFormat string
|
||||
securityOpts *SecurityOptions
|
||||
prefix string
|
||||
file string
|
||||
args []string
|
||||
@ -50,6 +52,14 @@ func (b *Builder) Build() (*Config, error) {
|
||||
tagName = "toml"
|
||||
}
|
||||
|
||||
// Set format and security settings
|
||||
if b.fileFormat != "" {
|
||||
b.cfg.fileFormat = b.fileFormat
|
||||
}
|
||||
if b.securityOpts != nil {
|
||||
b.cfg.securityOpts = b.securityOpts
|
||||
}
|
||||
|
||||
// 1. Register defaults
|
||||
// If WithDefaults() was called, it takes precedence.
|
||||
// If not, but WithTarget() was called, use the target struct for defaults.
|
||||
@ -148,6 +158,23 @@ func (b *Builder) WithTagName(tagName string) *Builder {
|
||||
return b
|
||||
}
|
||||
|
||||
// WithFileFormat sets the expected file format
|
||||
func (b *Builder) WithFileFormat(format string) *Builder {
|
||||
switch format {
|
||||
case "toml", "json", "yaml", "auto":
|
||||
b.fileFormat = format
|
||||
default:
|
||||
b.err = fmt.Errorf("unsupported file format %q", format)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// WithSecurityOptions sets security options for file loading
|
||||
func (b *Builder) WithSecurityOptions(opts SecurityOptions) *Builder {
|
||||
b.securityOpts = &opts
|
||||
return b
|
||||
}
|
||||
|
||||
// WithPrefix sets the prefix for struct registration
|
||||
func (b *Builder) WithPrefix(prefix string) *Builder {
|
||||
b.prefix = prefix
|
||||
|
||||
Reference in New Issue
Block a user