summaryrefslogtreecommitdiffstats
path: root/modules/setting
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-05-04 11:55:35 +0800
committerGitHub <noreply@github.com>2023-05-04 11:55:35 +0800
commit377a0a20f01a62f15a1504a3bba6cf6cc0c98bea (patch)
treee86a1818f23be1605a4cd707fadc7dcdce546d18 /modules/setting
parenta2fe68e50ba819daed9b0e28166a749d18c58750 (diff)
downloadgitea-377a0a20f01a62f15a1504a3bba6cf6cc0c98bea.tar.gz
gitea-377a0a20f01a62f15a1504a3bba6cf6cc0c98bea.zip
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.
Diffstat (limited to 'modules/setting')
-rw-r--r--modules/setting/config_provider.go45
-rw-r--r--modules/setting/setting.go41
2 files changed, 32 insertions, 54 deletions
diff --git a/modules/setting/config_provider.go b/modules/setting/config_provider.go
index 92c8c97fe9..1685958298 100644
--- a/modules/setting/config_provider.go
+++ b/modules/setting/config_provider.go
@@ -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
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 9ab55e91c5..b085a7b321 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -15,7 +15,6 @@ import (
"strings"
"time"
- "code.gitea.io/gitea/modules/auth/password/hash"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/user"
)
@@ -203,44 +202,18 @@ func PrepareAppDataPath() error {
return nil
}
-// InitProviderFromExistingFile initializes config provider from an existing config file (app.ini)
-func InitProviderFromExistingFile() {
- var err error
- CfgProvider, err = newConfigProviderFromFile(CustomConf, false, "")
- if err != nil {
- log.Fatal("InitProviderFromExistingFile: %v", err)
- }
-}
-
-// InitProviderAllowEmpty initializes config provider from file, it's also fine that if the config file (app.ini) doesn't exist
-func InitProviderAllowEmpty() {
- var err error
- CfgProvider, err = newConfigProviderFromFile(CustomConf, true, "")
- if err != nil {
- log.Fatal("InitProviderAllowEmpty: %v", err)
+func Init(opts *Options) {
+ if opts.CustomConf == "" {
+ opts.CustomConf = CustomConf
}
-}
-
-// InitProviderAndLoadCommonSettingsForTest initializes config provider and load common setttings for tests
-func InitProviderAndLoadCommonSettingsForTest(extraConfigs ...string) {
var err error
- CfgProvider, err = newConfigProviderFromFile(CustomConf, true, strings.Join(extraConfigs, "\n"))
+ CfgProvider, err = newConfigProviderFromFile(opts)
if err != nil {
- log.Fatal("InitProviderAndLoadCommonSettingsForTest: %v", err)
+ log.Fatal("Init[%v]: %v", opts, err)
}
- loadCommonSettingsFrom(CfgProvider)
- if err := PrepareAppDataPath(); err != nil {
- log.Fatal("Can not prepare APP_DATA_PATH: %v", err)
+ if !opts.DisableLoadCommonSettings {
+ loadCommonSettingsFrom(CfgProvider)
}
- // register the dummy hash algorithm function used in the test fixtures
- _ = hash.Register("dummy", hash.NewDummyHasher)
-
- PasswordHashAlgo, _ = hash.SetDefaultPasswordHashAlgorithm("dummy")
-}
-
-// LoadCommonSettings loads common configurations from a configuration provider.
-func LoadCommonSettings() {
- loadCommonSettingsFrom(CfgProvider)
}
// loadCommonSettingsFrom loads common configurations from a configuration provider.