Skip to content

Commit d4e51e2

Browse files
committed
More refactor
1 parent 5b0f5e6 commit d4e51e2

13 files changed

+143
-116
lines changed

Diff for: modules/setting/cron.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import "reflect"
77

88
// GetCronSettings maps the cron subsection to the provided config
99
func GetCronSettings(name string, config interface{}) (interface{}, error) {
10-
if err := Cfg.Section("cron." + name).MapTo(config); err != nil {
10+
return getCronSettings(Cfg, name, config)
11+
}
12+
13+
func getCronSettings(rootCfg Config, name string, config interface{}) (interface{}, error) {
14+
if err := rootCfg.Section("cron." + name).MapTo(config); err != nil {
1115
return config, err
1216
}
1317

@@ -18,7 +22,7 @@ func GetCronSettings(name string, config interface{}) (interface{}, error) {
1822
field := val.Field(i)
1923
tpField := typ.Field(i)
2024
if tpField.Type.Kind() == reflect.Struct && tpField.Anonymous {
21-
if err := Cfg.Section("cron." + name).MapTo(field.Addr().Interface()); err != nil {
25+
if err := rootCfg.Section("cron." + name).MapTo(field.Addr().Interface()); err != nil {
2226
return config, err
2327
}
2428
}

Diff for: modules/setting/cron_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
ini "gopkg.in/ini.v1"
1111
)
1212

13-
func Test_GetCronSettings(t *testing.T) {
13+
func Test_getCronSettings(t *testing.T) {
1414
type BaseStruct struct {
1515
Base bool
1616
Second string
@@ -27,16 +27,16 @@ Base = true
2727
Second = white rabbit
2828
Extend = true
2929
`
30-
Cfg, _ = ini.Load([]byte(iniStr))
30+
cfg, err := ini.Load([]byte(iniStr))
31+
assert.NoError(t, err)
3132

3233
extended := &Extended{
3334
BaseStruct: BaseStruct{
3435
Second: "queen of hearts",
3536
},
3637
}
3738

38-
_, err := GetCronSettings("test", extended)
39-
39+
_, err = getCronSettings(cfg, "test", extended)
4040
assert.NoError(t, err)
4141
assert.True(t, extended.Base)
4242
assert.EqualValues(t, extended.Second, "white rabbit")

Diff for: modules/setting/indexer.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func parseIndexerSetting(rootCfg Config) {
5757

5858
// The following settings are deprecated and can be overridden by settings in [queue] or [queue.issue_indexer]
5959
// FIXME: DEPRECATED to be removed in v1.18.0
60-
deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_TYPE", "queue.issue_indexer", "TYPE")
61-
deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_DIR", "queue.issue_indexer", "DATADIR")
62-
deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_CONN_STR", "queue.issue_indexer", "CONN_STR")
63-
deprecatedSetting("indexer", "ISSUE_INDEXER_QUEUE_BATCH_NUMBER", "queue.issue_indexer", "BATCH_LENGTH")
64-
deprecatedSetting("indexer", "UPDATE_BUFFER_LEN", "queue.issue_indexer", "LENGTH")
60+
deprecatedSetting(rootCfg, "indexer", "ISSUE_INDEXER_QUEUE_TYPE", "queue.issue_indexer", "TYPE")
61+
deprecatedSetting(rootCfg, "indexer", "ISSUE_INDEXER_QUEUE_DIR", "queue.issue_indexer", "DATADIR")
62+
deprecatedSetting(rootCfg, "indexer", "ISSUE_INDEXER_QUEUE_CONN_STR", "queue.issue_indexer", "CONN_STR")
63+
deprecatedSetting(rootCfg, "indexer", "ISSUE_INDEXER_QUEUE_BATCH_NUMBER", "queue.issue_indexer", "BATCH_LENGTH")
64+
deprecatedSetting(rootCfg, "indexer", "UPDATE_BUFFER_LEN", "queue.issue_indexer", "LENGTH")
6565

6666
Indexer.RepoIndexerEnabled = sec.Key("REPO_INDEXER_ENABLED").MustBool(false)
6767
Indexer.RepoType = sec.Key("REPO_INDEXER_TYPE").MustString("bleve")

Diff for: modules/setting/lfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func parseLFSSetting(rootCfg Config) {
3636

3737
// Specifically default PATH to LFS_CONTENT_PATH
3838
// FIXME: DEPRECATED to be removed in v1.18.0
39-
deprecatedSetting("server", "LFS_CONTENT_PATH", "lfs", "PATH")
39+
deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH")
4040
lfsSec.Key("PATH").MustString(
4141
sec.Key("LFS_CONTENT_PATH").String())
4242

Diff for: modules/setting/log.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,18 @@ func ParseLogSettings(disableConsole bool) {
370370

371371
// ParseXORMLogSetting initializes xorm logger setting
372372
func ParseXORMLogSetting(disableConsole bool) {
373-
EnableXORMLog = Cfg.Section("log").Key("ENABLE_XORM_LOG").MustBool(true)
373+
parseXORMLogSetting(Cfg, disableConsole)
374+
}
375+
376+
func parseXORMLogSetting(rootCfg Config, disableConsole bool) {
377+
EnableXORMLog = rootCfg.Section("log").Key("ENABLE_XORM_LOG").MustBool(true)
374378
if EnableXORMLog {
375379
options := newDefaultLogOptions()
376380
options.filename = filepath.Join(LogRootPath, "xorm.log")
377-
options.bufferLength = Cfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
381+
options.bufferLength = rootCfg.Section("log").Key("BUFFER_LEN").MustInt64(10000)
378382
options.disableConsole = disableConsole
379383

380-
Cfg.Section("log").Key("XORM").MustString(",")
381-
generateNamedLogger(Cfg, "xorm", options)
384+
rootCfg.Section("log").Key("XORM").MustString(",")
385+
generateNamedLogger(rootCfg, "xorm", options)
382386
}
383387
}

Diff for: modules/setting/mailer.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ func parseMailerSetting(rootCfg Config) {
6464

6565
// Handle Deprecations and map on to new configuration
6666
// FIXME: DEPRECATED to be removed in v1.19.0
67-
deprecatedSetting("mailer", "MAILER_TYPE", "mailer", "PROTOCOL")
67+
deprecatedSetting(rootCfg, "mailer", "MAILER_TYPE", "mailer", "PROTOCOL")
6868
if sec.HasKey("MAILER_TYPE") && !sec.HasKey("PROTOCOL") {
6969
if sec.Key("MAILER_TYPE").String() == "sendmail" {
7070
sec.Key("PROTOCOL").MustString("sendmail")
7171
}
7272
}
7373

7474
// FIXME: DEPRECATED to be removed in v1.19.0
75-
deprecatedSetting("mailer", "HOST", "mailer", "SMTP_ADDR")
75+
deprecatedSetting(rootCfg, "mailer", "HOST", "mailer", "SMTP_ADDR")
7676
if sec.HasKey("HOST") && !sec.HasKey("SMTP_ADDR") {
7777
givenHost := sec.Key("HOST").String()
7878
addr, port, err := net.SplitHostPort(givenHost)
@@ -89,7 +89,7 @@ func parseMailerSetting(rootCfg Config) {
8989
}
9090

9191
// FIXME: DEPRECATED to be removed in v1.19.0
92-
deprecatedSetting("mailer", "IS_TLS_ENABLED", "mailer", "PROTOCOL")
92+
deprecatedSetting(rootCfg, "mailer", "IS_TLS_ENABLED", "mailer", "PROTOCOL")
9393
if sec.HasKey("IS_TLS_ENABLED") && !sec.HasKey("PROTOCOL") {
9494
if sec.Key("IS_TLS_ENABLED").MustBool() {
9595
sec.Key("PROTOCOL").MustString("smtps")
@@ -99,37 +99,37 @@ func parseMailerSetting(rootCfg Config) {
9999
}
100100

101101
// FIXME: DEPRECATED to be removed in v1.19.0
102-
deprecatedSetting("mailer", "DISABLE_HELO", "mailer", "ENABLE_HELO")
102+
deprecatedSetting(rootCfg, "mailer", "DISABLE_HELO", "mailer", "ENABLE_HELO")
103103
if sec.HasKey("DISABLE_HELO") && !sec.HasKey("ENABLE_HELO") {
104104
sec.Key("ENABLE_HELO").MustBool(!sec.Key("DISABLE_HELO").MustBool())
105105
}
106106

107107
// FIXME: DEPRECATED to be removed in v1.19.0
108-
deprecatedSetting("mailer", "SKIP_VERIFY", "mailer", "FORCE_TRUST_SERVER_CERT")
108+
deprecatedSetting(rootCfg, "mailer", "SKIP_VERIFY", "mailer", "FORCE_TRUST_SERVER_CERT")
109109
if sec.HasKey("SKIP_VERIFY") && !sec.HasKey("FORCE_TRUST_SERVER_CERT") {
110110
sec.Key("FORCE_TRUST_SERVER_CERT").MustBool(sec.Key("SKIP_VERIFY").MustBool())
111111
}
112112

113113
// FIXME: DEPRECATED to be removed in v1.19.0
114-
deprecatedSetting("mailer", "USE_CERTIFICATE", "mailer", "USE_CLIENT_CERT")
114+
deprecatedSetting(rootCfg, "mailer", "USE_CERTIFICATE", "mailer", "USE_CLIENT_CERT")
115115
if sec.HasKey("USE_CERTIFICATE") && !sec.HasKey("USE_CLIENT_CERT") {
116116
sec.Key("USE_CLIENT_CERT").MustBool(sec.Key("USE_CERTIFICATE").MustBool())
117117
}
118118

119119
// FIXME: DEPRECATED to be removed in v1.19.0
120-
deprecatedSetting("mailer", "CERT_FILE", "mailer", "CLIENT_CERT_FILE")
120+
deprecatedSetting(rootCfg, "mailer", "CERT_FILE", "mailer", "CLIENT_CERT_FILE")
121121
if sec.HasKey("CERT_FILE") && !sec.HasKey("CLIENT_CERT_FILE") {
122122
sec.Key("CERT_FILE").MustString(sec.Key("CERT_FILE").String())
123123
}
124124

125125
// FIXME: DEPRECATED to be removed in v1.19.0
126-
deprecatedSetting("mailer", "KEY_FILE", "mailer", "CLIENT_KEY_FILE")
126+
deprecatedSetting(rootCfg, "mailer", "KEY_FILE", "mailer", "CLIENT_KEY_FILE")
127127
if sec.HasKey("KEY_FILE") && !sec.HasKey("CLIENT_KEY_FILE") {
128128
sec.Key("KEY_FILE").MustString(sec.Key("KEY_FILE").String())
129129
}
130130

131131
// FIXME: DEPRECATED to be removed in v1.19.0
132-
deprecatedSetting("mailer", "ENABLE_HTML_ALTERNATIVE", "mailer", "SEND_AS_PLAIN_TEXT")
132+
deprecatedSetting(rootCfg, "mailer", "ENABLE_HTML_ALTERNATIVE", "mailer", "SEND_AS_PLAIN_TEXT")
133133
if sec.HasKey("ENABLE_HTML_ALTERNATIVE") && !sec.HasKey("SEND_AS_PLAIN_TEXT") {
134134
sec.Key("SEND_AS_PLAIN_TEXT").MustBool(!sec.Key("ENABLE_HTML_ALTERNATIVE").MustBool(false))
135135
}

Diff for: modules/setting/mirror.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func parseMirrorSetting(rootCfg Config) {
2828
// Handle old configuration through `[repository]` `DISABLE_MIRRORS`
2929
// - please note this was badly named and only disabled the creation of new pull mirrors
3030
// FIXME: DEPRECATED to be removed in v1.18.0
31-
deprecatedSetting("repository", "DISABLE_MIRRORS", "mirror", "ENABLED")
31+
deprecatedSetting(rootCfg, "repository", "DISABLE_MIRRORS", "mirror", "ENABLED")
3232
if rootCfg.Section("repository").Key("DISABLE_MIRRORS").MustBool(false) {
3333
Mirror.DisableNewPull = true
3434
}

Diff for: modules/setting/picture.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ func parsePictureSetting(rootCfg Config) {
6060
}
6161

6262
DisableGravatar = sec.Key("DISABLE_GRAVATAR").MustBool(GetDefaultDisableGravatar())
63-
deprecatedSettingDB("", "DISABLE_GRAVATAR")
63+
deprecatedSettingDB(rootCfg, "", "DISABLE_GRAVATAR")
6464
EnableFederatedAvatar = sec.Key("ENABLE_FEDERATED_AVATAR").MustBool(GetDefaultEnableFederatedAvatar(DisableGravatar))
65-
deprecatedSettingDB("", "ENABLE_FEDERATED_AVATAR")
65+
deprecatedSettingDB(rootCfg, "", "ENABLE_FEDERATED_AVATAR")
6666

6767
parseRepoAvatarSetting(rootCfg)
6868
}

Diff for: modules/setting/queue.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ var Queue = QueueSettings{}
3939

4040
// GetQueueSettings returns the queue settings for the appropriately named queue
4141
func GetQueueSettings(name string) QueueSettings {
42+
return getQueueSettings(Cfg, name)
43+
}
44+
45+
func getQueueSettings(rootCfg Config, name string) QueueSettings {
4246
q := QueueSettings{}
43-
sec := Cfg.Section("queue." + name)
47+
sec := rootCfg.Section("queue." + name)
4448
q.Name = name
4549

4650
// DataDir is not directly inheritable
@@ -85,7 +89,11 @@ func GetQueueSettings(name string) QueueSettings {
8589
// ParseQueueSettings sets up the default settings for Queues
8690
// This is exported for tests to be able to use the queue
8791
func ParseQueueSettings() {
88-
sec := Cfg.Section("queue")
92+
parseQueueSettings(Cfg)
93+
}
94+
95+
func parseQueueSettings(rootCfg Config) {
96+
sec := rootCfg.Section("queue")
8997
Queue.DataDir = filepath.ToSlash(sec.Key("DATADIR").MustString("queues/"))
9098
if !filepath.IsAbs(Queue.DataDir) {
9199
Queue.DataDir = filepath.ToSlash(filepath.Join(AppDataPath, Queue.DataDir))
@@ -108,10 +116,10 @@ func ParseQueueSettings() {
108116

109117
// Now handle the old issue_indexer configuration
110118
// FIXME: DEPRECATED to be removed in v1.18.0
111-
section := Cfg.Section("queue.issue_indexer")
119+
section := rootCfg.Section("queue.issue_indexer")
112120
directlySet := toDirectlySetKeysSet(section)
113121
if !directlySet.Contains("TYPE") && defaultType == "" {
114-
switch typ := Cfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_TYPE").MustString(""); typ {
122+
switch typ := rootCfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_TYPE").MustString(""); typ {
115123
case "levelqueue":
116124
_, _ = section.NewKey("TYPE", "level")
117125
case "channel":
@@ -125,25 +133,25 @@ func ParseQueueSettings() {
125133
}
126134
}
127135
if !directlySet.Contains("LENGTH") {
128-
length := Cfg.Section("indexer").Key("UPDATE_BUFFER_LEN").MustInt(0)
136+
length := rootCfg.Section("indexer").Key("UPDATE_BUFFER_LEN").MustInt(0)
129137
if length != 0 {
130138
_, _ = section.NewKey("LENGTH", strconv.Itoa(length))
131139
}
132140
}
133141
if !directlySet.Contains("BATCH_LENGTH") {
134-
fallback := Cfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_BATCH_NUMBER").MustInt(0)
142+
fallback := rootCfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_BATCH_NUMBER").MustInt(0)
135143
if fallback != 0 {
136144
_, _ = section.NewKey("BATCH_LENGTH", strconv.Itoa(fallback))
137145
}
138146
}
139147
if !directlySet.Contains("DATADIR") {
140-
queueDir := filepath.ToSlash(Cfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_DIR").MustString(""))
148+
queueDir := filepath.ToSlash(rootCfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_DIR").MustString(""))
141149
if queueDir != "" {
142150
_, _ = section.NewKey("DATADIR", queueDir)
143151
}
144152
}
145153
if !directlySet.Contains("CONN_STR") {
146-
connStr := Cfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_CONN_STR").MustString("")
154+
connStr := rootCfg.Section("indexer").Key("ISSUE_INDEXER_QUEUE_CONN_STR").MustString("")
147155
if connStr != "" {
148156
_, _ = section.NewKey("CONN_STR", connStr)
149157
}
@@ -153,31 +161,31 @@ func ParseQueueSettings() {
153161
// - will need to set default for [queue.*)] LENGTH appropriately though though
154162

155163
// Handle the old mailer configuration
156-
handleOldLengthConfiguration("mailer", "mailer", "SEND_BUFFER_LEN", 100)
164+
handleOldLengthConfiguration(rootCfg, "mailer", "mailer", "SEND_BUFFER_LEN", 100)
157165

158166
// Handle the old test pull requests configuration
159167
// Please note this will be a unique queue
160-
handleOldLengthConfiguration("pr_patch_checker", "repository", "PULL_REQUEST_QUEUE_LENGTH", 1000)
168+
handleOldLengthConfiguration(rootCfg, "pr_patch_checker", "repository", "PULL_REQUEST_QUEUE_LENGTH", 1000)
161169

162170
// Handle the old mirror queue configuration
163171
// Please note this will be a unique queue
164-
handleOldLengthConfiguration("mirror", "repository", "MIRROR_QUEUE_LENGTH", 1000)
172+
handleOldLengthConfiguration(rootCfg, "mirror", "repository", "MIRROR_QUEUE_LENGTH", 1000)
165173
}
166174

167175
// handleOldLengthConfiguration allows fallback to older configuration. `[queue.name]` `LENGTH` will override this configuration, but
168176
// if that is left unset then we should fallback to the older configuration. (Except where the new length woul be <=0)
169-
func handleOldLengthConfiguration(queueName, oldSection, oldKey string, defaultValue int) {
170-
if Cfg.Section(oldSection).HasKey(oldKey) {
177+
func handleOldLengthConfiguration(rootCfg Config, queueName, oldSection, oldKey string, defaultValue int) {
178+
if rootCfg.Section(oldSection).HasKey(oldKey) {
171179
log.Error("Deprecated fallback for %s queue length `[%s]` `%s` present. Use `[queue.%s]` `LENGTH`. This will be removed in v1.18.0", queueName, queueName, oldSection, oldKey)
172180
}
173-
value := Cfg.Section(oldSection).Key(oldKey).MustInt(defaultValue)
181+
value := rootCfg.Section(oldSection).Key(oldKey).MustInt(defaultValue)
174182

175183
// Don't override with 0
176184
if value <= 0 {
177185
return
178186
}
179187

180-
section := Cfg.Section("queue." + queueName)
188+
section := rootCfg.Section("queue." + queueName)
181189
directlySet := toDirectlySetKeysSet(section)
182190
if !directlySet.Contains("LENGTH") {
183191
_, _ = section.NewKey("LENGTH", strconv.Itoa(value))

Diff for: modules/setting/server.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func parseServerSetting(rootCfg Config) {
188188
if sec.HasKey("ENABLE_ACME") {
189189
EnableAcme = sec.Key("ENABLE_ACME").MustBool(false)
190190
} else {
191-
deprecatedSetting("server", "ENABLE_LETSENCRYPT", "server", "ENABLE_ACME")
191+
deprecatedSetting(rootCfg, "server", "ENABLE_LETSENCRYPT", "server", "ENABLE_ACME")
192192
EnableAcme = sec.Key("ENABLE_LETSENCRYPT").MustBool(false)
193193
}
194194
if EnableAcme {
@@ -198,7 +198,7 @@ func parseServerSetting(rootCfg Config) {
198198
if sec.HasKey("ACME_ACCEPTTOS") {
199199
AcmeTOS = sec.Key("ACME_ACCEPTTOS").MustBool(false)
200200
} else {
201-
deprecatedSetting("server", "LETSENCRYPT_ACCEPTTOS", "server", "ACME_ACCEPTTOS")
201+
deprecatedSetting(rootCfg, "server", "LETSENCRYPT_ACCEPTTOS", "server", "ACME_ACCEPTTOS")
202202
AcmeTOS = sec.Key("LETSENCRYPT_ACCEPTTOS").MustBool(false)
203203
}
204204
if !AcmeTOS {
@@ -208,14 +208,14 @@ func parseServerSetting(rootCfg Config) {
208208
if sec.HasKey("ACME_DIRECTORY") {
209209
AcmeLiveDirectory = sec.Key("ACME_DIRECTORY").MustString("https")
210210
} else {
211-
deprecatedSetting("server", "LETSENCRYPT_DIRECTORY", "server", "ACME_DIRECTORY")
211+
deprecatedSetting(rootCfg, "server", "LETSENCRYPT_DIRECTORY", "server", "ACME_DIRECTORY")
212212
AcmeLiveDirectory = sec.Key("LETSENCRYPT_DIRECTORY").MustString("https")
213213
}
214214
// FIXME: DEPRECATED to be removed in v1.18.0
215215
if sec.HasKey("ACME_EMAIL") {
216216
AcmeEmail = sec.Key("ACME_EMAIL").MustString("")
217217
} else {
218-
deprecatedSetting("server", "LETSENCRYPT_EMAIL", "server", "ACME_EMAIL")
218+
deprecatedSetting(rootCfg, "server", "LETSENCRYPT_EMAIL", "server", "ACME_EMAIL")
219219
AcmeEmail = sec.Key("LETSENCRYPT_EMAIL").MustString("")
220220
}
221221
} else {

0 commit comments

Comments
 (0)