summaryrefslogtreecommitdiffstats
path: root/modules/setting/config_provider.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/setting/config_provider.go')
-rw-r--r--modules/setting/config_provider.go80
1 files changed, 35 insertions, 45 deletions
diff --git a/modules/setting/config_provider.go b/modules/setting/config_provider.go
index deec5cc586..94dd989850 100644
--- a/modules/setting/config_provider.go
+++ b/modules/setting/config_provider.go
@@ -55,15 +55,15 @@ type ConfigProvider interface {
DisableSaving()
PrepareSaving() (ConfigProvider, error)
+ IsLoadedFromEmpty() bool
}
type iniConfigProvider struct {
- opts *Options
+ file string
ini *ini.File
- disableSaving bool
-
- newFile bool // whether the file has not existed previously
+ disableSaving bool // disable the "Save" method because the config options could be polluted
+ loadedFromEmpty bool // whether the file has not existed previously
}
type iniConfigSection struct {
@@ -182,53 +182,43 @@ func NewConfigProviderFromData(configContent string) (ConfigProvider, error) {
}
cfg.NameMapper = ini.SnackCase
return &iniConfigProvider{
- ini: cfg,
- newFile: true,
+ ini: cfg,
+ loadedFromEmpty: true,
}, nil
}
-type Options struct {
- CustomConf string // the ini file path
- AllowEmpty bool // whether not finding configuration files is allowed
- ExtraConfig string
-
- DisableLoadCommonSettings bool // only used by "Init()", not used by "NewConfigProvider()"
-}
-
// NewConfigProviderFromFile load configuration from file.
// NOTE: do not print any log except error.
-func NewConfigProviderFromFile(opts *Options) (ConfigProvider, error) {
+func NewConfigProviderFromFile(file string, extraConfigs ...string) (ConfigProvider, error) {
cfg := ini.Empty(ini.LoadOptions{KeyValueDelimiterOnWrite: " = "})
- newFile := true
+ loadedFromEmpty := true
- if opts.CustomConf != "" {
- isFile, err := util.IsFile(opts.CustomConf)
+ if file != "" {
+ isFile, err := util.IsFile(file)
if err != nil {
- return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", opts.CustomConf, err)
+ return nil, fmt.Errorf("unable to check if %q is a file. Error: %v", file, err)
}
if isFile {
- if err := cfg.Append(opts.CustomConf); err != nil {
- return nil, fmt.Errorf("failed to load custom conf '%s': %v", opts.CustomConf, err)
+ if err = cfg.Append(file); err != nil {
+ return nil, fmt.Errorf("failed to load config file %q: %v", file, err)
}
- newFile = false
+ loadedFromEmpty = false
}
}
- 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 opts.ExtraConfig != "" {
- if err := cfg.Append([]byte(opts.ExtraConfig)); err != nil {
- return nil, fmt.Errorf("unable to append more config: %v", err)
+ if len(extraConfigs) > 0 {
+ for _, s := range extraConfigs {
+ if err := cfg.Append([]byte(s)); err != nil {
+ return nil, fmt.Errorf("unable to append more config: %v", err)
+ }
}
}
cfg.NameMapper = ini.SnackCase
return &iniConfigProvider{
- opts: opts,
- ini: cfg,
- newFile: newFile,
+ file: file,
+ ini: cfg,
+ loadedFromEmpty: loadedFromEmpty,
}, nil
}
@@ -266,20 +256,17 @@ func (p *iniConfigProvider) Save() error {
if p.disableSaving {
return errDisableSaving
}
- filename := p.opts.CustomConf
+ filename := p.file
if filename == "" {
- if !p.opts.AllowEmpty {
- return fmt.Errorf("custom config path must not be empty")
- }
- return nil
+ return fmt.Errorf("config file path must not be empty")
}
- if p.newFile {
+ if p.loadedFromEmpty {
if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
- return fmt.Errorf("failed to create '%s': %v", filename, err)
+ return fmt.Errorf("failed to create %q: %v", filename, err)
}
}
if err := p.ini.SaveTo(filename); err != nil {
- return fmt.Errorf("failed to save '%s': %v", filename, err)
+ return fmt.Errorf("failed to save %q: %v", filename, err)
}
// Change permissions to be more restrictive
@@ -313,11 +300,14 @@ func (p *iniConfigProvider) DisableSaving() {
// it makes the "Save" outputs a lot of garbage options
// After the INI package gets refactored, no "MustXxx" pollution, this workaround can be dropped.
func (p *iniConfigProvider) PrepareSaving() (ConfigProvider, error) {
- cfgFile := p.opts.CustomConf
- if cfgFile == "" {
+ if p.file == "" {
return nil, errors.New("no config file to save")
}
- return NewConfigProviderFromFile(p.opts)
+ return NewConfigProviderFromFile(p.file)
+}
+
+func (p *iniConfigProvider) IsLoadedFromEmpty() bool {
+ return p.loadedFromEmpty
}
func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
@@ -356,8 +346,8 @@ func NewConfigProviderForLocale(source any, others ...any) (ConfigProvider, erro
}
iniFile.BlockMode = false
return &iniConfigProvider{
- ini: iniFile,
- newFile: true,
+ ini: iniFile,
+ loadedFromEmpty: true,
}, nil
}