Skip to content

Commit 5cf7da6

Browse files
lunnydelvh
andauthored
Refactor config provider (#24245)
This PR introduces more abstract about `ConfigProvider` and hides more `ini` references. --------- Co-authored-by: delvh <dev.lh@web.de>
1 parent 56d4893 commit 5cf7da6

17 files changed

+227
-164
lines changed

cmd/web.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/felixge/fgprof"
2626
"github.com/urfave/cli"
27-
ini "gopkg.in/ini.v1"
2827
)
2928

3029
// PIDFile could be set from build tag
@@ -223,9 +222,10 @@ func setPort(port string) error {
223222
defaultLocalURL += ":" + setting.HTTPPort + "/"
224223

225224
// Save LOCAL_ROOT_URL if port changed
226-
setting.CreateOrAppendToCustomConf("server.LOCAL_ROOT_URL", func(cfg *ini.File) {
227-
cfg.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
228-
})
225+
setting.CfgProvider.Section("server").Key("LOCAL_ROOT_URL").SetValue(defaultLocalURL)
226+
if err := setting.CfgProvider.Save(); err != nil {
227+
return fmt.Errorf("Failed to save config file: %v", err)
228+
}
229229
}
230230
return nil
231231
}

modules/indexer/issues/indexer_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
_ "code.gitea.io/gitea/models"
1717

1818
"github.com/stretchr/testify/assert"
19-
"gopkg.in/ini.v1"
2019
)
2120

2221
func TestMain(m *testing.M) {
@@ -27,7 +26,7 @@ func TestMain(m *testing.M) {
2726

2827
func TestBleveSearchIssues(t *testing.T) {
2928
assert.NoError(t, unittest.PrepareTestDatabase())
30-
setting.CfgProvider = ini.Empty()
29+
setting.CfgProvider = setting.NewEmptyConfigProvider()
3130

3231
tmpIndexerDir := t.TempDir()
3332

modules/indexer/stats/indexer_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
_ "code.gitea.io/gitea/models"
1919

2020
"github.com/stretchr/testify/assert"
21-
"gopkg.in/ini.v1"
2221
)
2322

2423
func TestMain(m *testing.M) {
@@ -29,7 +28,7 @@ func TestMain(m *testing.M) {
2928

3029
func TestRepoStatsIndex(t *testing.T) {
3130
assert.NoError(t, unittest.PrepareTestDatabase())
32-
setting.CfgProvider = ini.Empty()
31+
setting.CfgProvider = setting.NewEmptyConfigProvider()
3332

3433
setting.LoadQueueSettings()
3534

modules/setting/config_provider.go

+141-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,157 @@
44
package setting
55

66
import (
7+
"fmt"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
712
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/util"
814

915
ini "gopkg.in/ini.v1"
1016
)
1117

18+
type ConfigSection interface {
19+
Name() string
20+
MapTo(interface{}) error
21+
HasKey(key string) bool
22+
NewKey(name, value string) (*ini.Key, error)
23+
Key(key string) *ini.Key
24+
Keys() []*ini.Key
25+
ChildSections() []*ini.Section
26+
}
27+
1228
// ConfigProvider represents a config provider
1329
type ConfigProvider interface {
14-
Section(section string) *ini.Section
15-
NewSection(name string) (*ini.Section, error)
16-
GetSection(name string) (*ini.Section, error)
30+
Section(section string) ConfigSection
31+
NewSection(name string) (ConfigSection, error)
32+
GetSection(name string) (ConfigSection, error)
33+
DeleteSection(name string) error
34+
Save() error
35+
}
36+
37+
type iniFileConfigProvider struct {
38+
*ini.File
39+
filepath string // the ini file path
40+
newFile bool // whether the file has not existed previously
41+
allowEmpty bool // whether not finding configuration files is allowed (only true for the tests)
42+
}
43+
44+
// NewEmptyConfigProvider create a new empty config provider
45+
func NewEmptyConfigProvider() ConfigProvider {
46+
cp, _ := newConfigProviderFromData("")
47+
return cp
48+
}
49+
50+
// newConfigProviderFromData this function is only for testing
51+
func newConfigProviderFromData(configContent string) (ConfigProvider, error) {
52+
var cfg *ini.File
53+
var err error
54+
if configContent == "" {
55+
cfg = ini.Empty()
56+
} else {
57+
cfg, err = ini.Load(strings.NewReader(configContent))
58+
if err != nil {
59+
return nil, err
60+
}
61+
}
62+
cfg.NameMapper = ini.SnackCase
63+
return &iniFileConfigProvider{
64+
File: cfg,
65+
newFile: true,
66+
}, nil
67+
}
68+
69+
// newConfigProviderFromFile load configuration from file.
70+
// NOTE: do not print any log except error.
71+
func newConfigProviderFromFile(customConf string, allowEmpty bool, extraConfig string) (*iniFileConfigProvider, error) {
72+
cfg := ini.Empty()
73+
newFile := true
74+
75+
if customConf != "" {
76+
isFile, err := util.IsFile(customConf)
77+
if err != nil {
78+
return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", customConf, err)
79+
}
80+
if isFile {
81+
if err := cfg.Append(customConf); err != nil {
82+
return nil, fmt.Errorf("failed to load custom conf '%s': %v", customConf, err)
83+
}
84+
newFile = false
85+
}
86+
}
87+
88+
if newFile && !allowEmpty {
89+
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)
90+
}
91+
92+
if extraConfig != "" {
93+
if err := cfg.Append([]byte(extraConfig)); err != nil {
94+
return nil, fmt.Errorf("unable to append more config: %v", err)
95+
}
96+
}
97+
98+
cfg.NameMapper = ini.SnackCase
99+
return &iniFileConfigProvider{
100+
File: cfg,
101+
filepath: customConf,
102+
newFile: newFile,
103+
allowEmpty: allowEmpty,
104+
}, nil
105+
}
106+
107+
func (p *iniFileConfigProvider) Section(section string) ConfigSection {
108+
return p.File.Section(section)
109+
}
110+
111+
func (p *iniFileConfigProvider) NewSection(name string) (ConfigSection, error) {
112+
return p.File.NewSection(name)
113+
}
114+
115+
func (p *iniFileConfigProvider) GetSection(name string) (ConfigSection, error) {
116+
return p.File.GetSection(name)
117+
}
118+
119+
func (p *iniFileConfigProvider) DeleteSection(name string) error {
120+
p.File.DeleteSection(name)
121+
return nil
122+
}
123+
124+
// Save save the content into file
125+
func (p *iniFileConfigProvider) Save() error {
126+
if p.filepath == "" {
127+
if !p.allowEmpty {
128+
return fmt.Errorf("custom config path must not be empty")
129+
}
130+
return nil
131+
}
132+
133+
if p.newFile {
134+
if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil {
135+
return fmt.Errorf("failed to create '%s': %v", CustomConf, err)
136+
}
137+
}
138+
if err := p.SaveTo(p.filepath); err != nil {
139+
return fmt.Errorf("failed to save '%s': %v", p.filepath, err)
140+
}
141+
142+
// Change permissions to be more restrictive
143+
fi, err := os.Stat(CustomConf)
144+
if err != nil {
145+
return fmt.Errorf("failed to determine current conf file permissions: %v", err)
146+
}
147+
148+
if fi.Mode().Perm() > 0o600 {
149+
if err = os.Chmod(CustomConf, 0o600); err != nil {
150+
log.Warn("Failed changing conf file permissions to -rw-------. Consider changing them manually.")
151+
}
152+
}
153+
return nil
17154
}
18155

19156
// a file is an implementation ConfigProvider and other implementations are possible, i.e. from docker, k8s, …
20-
var _ ConfigProvider = &ini.File{}
157+
var _ ConfigProvider = &iniFileConfigProvider{}
21158

22159
func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting interface{}) {
23160
if err := rootCfg.Section(sectionName).MapTo(setting); err != nil {

modules/setting/cron_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10-
ini "gopkg.in/ini.v1"
1110
)
1211

1312
func Test_getCronSettings(t *testing.T) {
@@ -23,11 +22,11 @@ func Test_getCronSettings(t *testing.T) {
2322

2423
iniStr := `
2524
[cron.test]
26-
Base = true
27-
Second = white rabbit
28-
Extend = true
25+
BASE = true
26+
SECOND = white rabbit
27+
EXTEND = true
2928
`
30-
cfg, err := ini.Load([]byte(iniStr))
29+
cfg, err := newConfigProviderFromData(iniStr)
3130
assert.NoError(t, err)
3231

3332
extended := &Extended{

modules/setting/lfs.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99

1010
"code.gitea.io/gitea/modules/generate"
1111
"code.gitea.io/gitea/modules/log"
12-
13-
ini "gopkg.in/ini.v1"
1412
)
1513

1614
// LFS represents the configuration for Git LFS
@@ -38,8 +36,7 @@ func loadLFSFrom(rootCfg ConfigProvider) {
3836
// DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version
3937
// if these are removed, the warning will not be shown
4038
deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0")
41-
lfsSec.Key("PATH").MustString(
42-
sec.Key("LFS_CONTENT_PATH").String())
39+
lfsSec.Key("PATH").MustString(sec.Key("LFS_CONTENT_PATH").String())
4340

4441
LFS.Storage = getStorage(rootCfg, "lfs", storageType, lfsSec)
4542

@@ -62,9 +59,11 @@ func loadLFSFrom(rootCfg ConfigProvider) {
6259
}
6360

6461
// Save secret
65-
CreateOrAppendToCustomConf("server.LFS_JWT_SECRET", func(cfg *ini.File) {
66-
cfg.Section("server").Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
67-
})
62+
sec.Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64)
63+
if err := rootCfg.Save(); err != nil {
64+
log.Fatal("Error saving JWT Secret for custom config: %v", err)
65+
return
66+
}
6867
}
6968
}
7069
}

modules/setting/log.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/json"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/util"
18-
19-
ini "gopkg.in/ini.v1"
2018
)
2119

2220
var (
@@ -131,12 +129,12 @@ type LogDescription struct {
131129
SubLogDescriptions []SubLogDescription
132130
}
133131

134-
func getLogLevel(section *ini.Section, key string, defaultValue log.Level) log.Level {
132+
func getLogLevel(section ConfigSection, key string, defaultValue log.Level) log.Level {
135133
value := section.Key(key).MustString(defaultValue.String())
136134
return log.FromString(value)
137135
}
138136

139-
func getStacktraceLogLevel(section *ini.Section, key, defaultValue string) string {
137+
func getStacktraceLogLevel(section ConfigSection, key, defaultValue string) string {
140138
value := section.Key(key).MustString(defaultValue)
141139
return log.FromString(value).String()
142140
}
@@ -165,7 +163,7 @@ func loadLogFrom(rootCfg ConfigProvider) {
165163
Log.EnableXORMLog = rootCfg.Section("log").Key("ENABLE_XORM_LOG").MustBool(true)
166164
}
167165

168-
func generateLogConfig(sec *ini.Section, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
166+
func generateLogConfig(sec ConfigSection, name string, defaults defaultLogOptions) (mode, jsonConfig, levelName string) {
169167
level := getLogLevel(sec, "LEVEL", Log.Level)
170168
levelName = level.String()
171169
stacktraceLevelName := getStacktraceLogLevel(sec, "STACKTRACE_LEVEL", Log.StacktraceLogLevel)

modules/setting/mailer_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77
"testing"
88

99
"github.com/stretchr/testify/assert"
10-
ini "gopkg.in/ini.v1"
1110
)
1211

1312
func Test_loadMailerFrom(t *testing.T) {
14-
iniFile := ini.Empty()
13+
iniFile := NewEmptyConfigProvider()
1514
kases := map[string]*Mailer{
1615
"smtp.mydomain.com": {
1716
SMTPAddr: "smtp.mydomain.com",

modules/setting/markup.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strings"
99

1010
"code.gitea.io/gitea/modules/log"
11-
12-
"gopkg.in/ini.v1"
1311
)
1412

1513
// ExternalMarkupRenderers represents the external markup renderers
@@ -82,7 +80,7 @@ func loadMarkupFrom(rootCfg ConfigProvider) {
8280
}
8381
}
8482

85-
func newMarkupSanitizer(name string, sec *ini.Section) {
83+
func newMarkupSanitizer(name string, sec ConfigSection) {
8684
rule, ok := createMarkupSanitizerRule(name, sec)
8785
if ok {
8886
if strings.HasPrefix(name, "sanitizer.") {
@@ -99,7 +97,7 @@ func newMarkupSanitizer(name string, sec *ini.Section) {
9997
}
10098
}
10199

102-
func createMarkupSanitizerRule(name string, sec *ini.Section) (MarkupSanitizerRule, bool) {
100+
func createMarkupSanitizerRule(name string, sec ConfigSection) (MarkupSanitizerRule, bool) {
103101
var rule MarkupSanitizerRule
104102

105103
ok := false
@@ -141,7 +139,7 @@ func createMarkupSanitizerRule(name string, sec *ini.Section) (MarkupSanitizerRu
141139
return rule, true
142140
}
143141

144-
func newMarkupRenderer(name string, sec *ini.Section) {
142+
func newMarkupRenderer(name string, sec ConfigSection) {
145143
extensionReg := regexp.MustCompile(`\.\w`)
146144

147145
extensions := sec.Key("FILE_EXTENSIONS").Strings(",")

0 commit comments

Comments
 (0)