Merge setting.InitXXX into one function with options (#24389)

This PR will merge 3 Init functions on setting packages as 1 and
introduce an options struct.
This commit is contained in:
Lunny Xiao 2023-05-04 11:55:35 +08:00 committed by GitHub
parent a2fe68e50b
commit 377a0a20f0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 104 additions and 137 deletions

View file

@ -35,10 +35,9 @@ type ConfigProvider interface {
}
type iniFileConfigProvider struct {
opts *Options
*ini.File
filepath string // the ini file path
newFile bool // whether the file has not existed previously
allowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
newFile bool // whether the file has not existed previously
}
// NewEmptyConfigProvider create a new empty config provider
@ -66,41 +65,47 @@ func newConfigProviderFromData(configContent string) (ConfigProvider, error) {
}, nil
}
type Options struct {
CustomConf string // the ini file path
AllowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
ExtraConfig string
DisableLoadCommonSettings bool
}
// newConfigProviderFromFile load configuration from file.
// NOTE: do not print any log except error.
func newConfigProviderFromFile(customConf string, allowEmpty bool, extraConfig string) (*iniFileConfigProvider, error) {
func newConfigProviderFromFile(opts *Options) (*iniFileConfigProvider, error) {
cfg := ini.Empty()
newFile := true
if customConf != "" {
isFile, err := util.IsFile(customConf)
if opts.CustomConf != "" {
isFile, err := util.IsFile(opts.CustomConf)
if err != nil {
return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", customConf, err)
return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", opts.CustomConf, err)
}
if isFile {
if err := cfg.Append(customConf); err != nil {
return nil, fmt.Errorf("failed to load custom conf '%s': %v", customConf, err)
if err := cfg.Append(opts.CustomConf); err != nil {
return nil, fmt.Errorf("failed to load custom conf '%s': %v", opts.CustomConf, err)
}
newFile = false
}
}
if newFile && !allowEmpty {
if newFile && !opts.AllowEmpty {
return nil, fmt.Errorf("unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c", CustomConf)
}
if extraConfig != "" {
if err := cfg.Append([]byte(extraConfig)); err != nil {
if opts.ExtraConfig != "" {
if err := cfg.Append([]byte(opts.ExtraConfig)); err != nil {
return nil, fmt.Errorf("unable to append more config: %v", err)
}
}
cfg.NameMapper = ini.SnackCase
return &iniFileConfigProvider{
File: cfg,
filepath: customConf,
newFile: newFile,
allowEmpty: allowEmpty,
opts: opts,
File: cfg,
newFile: newFile,
}, nil
}
@ -123,8 +128,8 @@ func (p *iniFileConfigProvider) DeleteSection(name string) error {
// Save save the content into file
func (p *iniFileConfigProvider) Save() error {
if p.filepath == "" {
if !p.allowEmpty {
if p.opts.CustomConf == "" {
if !p.opts.AllowEmpty {
return fmt.Errorf("custom config path must not be empty")
}
return nil
@ -135,8 +140,8 @@ func (p *iniFileConfigProvider) Save() error {
return fmt.Errorf("failed to create '%s': %v", CustomConf, err)
}
}
if err := p.SaveTo(p.filepath); err != nil {
return fmt.Errorf("failed to save '%s': %v", p.filepath, err)
if err := p.SaveTo(p.opts.CustomConf); err != nil {
return fmt.Errorf("failed to save '%s': %v", p.opts.CustomConf, err)
}
// Change permissions to be more restrictive