Skip to content

Commit a29e505

Browse files
GiteaBotwxiaoguang
andauthored
Refactor startup deprecation messages (#30305) (#30312)
Backport #30305 by wxiaoguang It doesn't change logic, it only does: 1. Rename the variable and function names 2. Use more consistent format when mentioning config section&key 3. Improve some messages Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
1 parent be5518e commit a29e505

File tree

11 files changed

+30
-28
lines changed

11 files changed

+30
-28
lines changed

modules/setting/config_provider.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,25 @@ func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
315315
}
316316
}
317317

318-
// DeprecatedWarnings contains the warning message for various deprecations, including: setting option, file/folder, etc
319-
var DeprecatedWarnings []string
318+
// StartupProblems contains the messages for various startup problems, including: setting option, file/folder, etc
319+
var StartupProblems []string
320+
321+
func logStartupProblem(skip int, level log.Level, format string, args ...any) {
322+
msg := fmt.Sprintf(format, args...)
323+
log.Log(skip+1, level, "%s", msg)
324+
StartupProblems = append(StartupProblems, msg)
325+
}
320326

321327
func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) {
322328
if rootCfg.Section(oldSection).HasKey(oldKey) {
323-
msg := fmt.Sprintf("Deprecated config option `[%s]` `%s` present. Use `[%s]` `%s` instead. This fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version)
324-
log.Error("%v", msg)
325-
DeprecatedWarnings = append(DeprecatedWarnings, msg)
329+
logStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents, please use `[%s].%s` instead because this fallback will be/has been removed in %s", oldSection, oldKey, newSection, newKey, version)
326330
}
327331
}
328332

329333
// deprecatedSettingDB add a hint that the configuration has been moved to database but still kept in app.ini
330334
func deprecatedSettingDB(rootCfg ConfigProvider, oldSection, oldKey string) {
331335
if rootCfg.Section(oldSection).HasKey(oldKey) {
332-
log.Error("Deprecated `[%s]` `%s` present which has been copied to database table sys_setting", oldSection, oldKey)
336+
logStartupProblem(1, log.ERROR, "Deprecation: config option `[%s].%s` presents but it won't take effect because it has been moved to admin panel -> config setting", oldSection, oldKey)
333337
}
334338
}
335339

modules/setting/indexer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func loadIndexerFrom(rootCfg ConfigProvider) {
5858
if !filepath.IsAbs(Indexer.IssuePath) {
5959
Indexer.IssuePath = filepath.ToSlash(filepath.Join(AppWorkPath, Indexer.IssuePath))
6060
}
61-
checkOverlappedPath("indexer.ISSUE_INDEXER_PATH", Indexer.IssuePath)
61+
checkOverlappedPath("[indexer].ISSUE_INDEXER_PATH", Indexer.IssuePath)
6262
} else {
6363
Indexer.IssueConnStr = sec.Key("ISSUE_INDEXER_CONN_STR").MustString(Indexer.IssueConnStr)
6464
if Indexer.IssueType == "meilisearch" {

modules/setting/oauth2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func GetGeneralTokenSigningSecret() []byte {
168168
}
169169
if generalSigningSecret.CompareAndSwap(old, &jwtSecret) {
170170
// FIXME: in main branch, the signing token should be refactored (eg: one unique for LFS/OAuth2/etc ...)
171-
log.Warn("OAuth2 is not enabled, unable to use a persistent signing secret, a new one is generated, which is not persistent between restarts and cluster nodes")
171+
logStartupProblem(1, log.WARN, "OAuth2 is not enabled, unable to use a persistent signing secret, a new one is generated, which is not persistent between restarts and cluster nodes")
172172
return jwtSecret
173173
}
174174
return *generalSigningSecret.Load()

modules/setting/repository.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) {
286286
RepoRootPath = filepath.Clean(RepoRootPath)
287287
}
288288

289-
checkOverlappedPath("repository.ROOT", RepoRootPath)
289+
checkOverlappedPath("[repository].ROOT", RepoRootPath)
290290

291291
defaultDetectedCharsetsOrder := make([]string, 0, len(Repository.DetectedCharsetsOrder))
292292
for _, charset := range Repository.DetectedCharsetsOrder {

modules/setting/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ func loadServerFrom(rootCfg ConfigProvider) {
331331
if !filepath.IsAbs(PprofDataPath) {
332332
PprofDataPath = filepath.Join(AppWorkPath, PprofDataPath)
333333
}
334-
checkOverlappedPath("server.PPROF_DATA_PATH", PprofDataPath)
334+
checkOverlappedPath("[server].PPROF_DATA_PATH", PprofDataPath)
335335

336336
landingPage := sec.Key("LANDING_PAGE").MustString("home")
337337
switch landingPage {

modules/setting/session.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func loadSessionFrom(rootCfg ConfigProvider) {
4646
SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(filepath.Join(AppDataPath, "sessions")), "\" ")
4747
if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
4848
SessionConfig.ProviderConfig = filepath.Join(AppWorkPath, SessionConfig.ProviderConfig)
49-
checkOverlappedPath("session.PROVIDER_CONFIG", SessionConfig.ProviderConfig)
49+
checkOverlappedPath("[session].PROVIDER_CONFIG", SessionConfig.ProviderConfig)
5050
}
5151
SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
5252
SessionConfig.CookiePath = AppSubURL

modules/setting/setting.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ var configuredPaths = make(map[string]string)
235235
func checkOverlappedPath(name, path string) {
236236
// TODO: some paths shouldn't overlap (storage.xxx.path), while some could (data path is the base path for storage path)
237237
if targetName, ok := configuredPaths[path]; ok && targetName != name {
238-
msg := fmt.Sprintf("Configured path %q is used by %q and %q at the same time. The paths must be unique to prevent data loss.", path, targetName, name)
239-
log.Error("%s", msg)
240-
DeprecatedWarnings = append(DeprecatedWarnings, msg)
238+
logStartupProblem(1, log.ERROR, "Configured path %q is used by %q and %q at the same time. The paths must be unique to prevent data loss.", path, targetName, name)
241239
}
242240
configuredPaths[path] = name
243241
}

modules/setting/storage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func getStorageForLocal(targetSec, overrideSec ConfigSection, tp targetSecType,
240240
}
241241
}
242242

243-
checkOverlappedPath("storage."+name+".PATH", storage.Path)
243+
checkOverlappedPath("[storage."+name+"].PATH", storage.Path)
244244

245245
return &storage, nil
246246
}

routers/web/admin/admin.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ func updateSystemStatus() {
117117
sysStatus.NumGC = m.NumGC
118118
}
119119

120-
func prepareDeprecatedWarningsAlert(ctx *context.Context) {
121-
if len(setting.DeprecatedWarnings) > 0 {
122-
content := setting.DeprecatedWarnings[0]
123-
if len(setting.DeprecatedWarnings) > 1 {
124-
content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1)
120+
func prepareStartupProblemsAlert(ctx *context.Context) {
121+
if len(setting.StartupProblems) > 0 {
122+
content := setting.StartupProblems[0]
123+
if len(setting.StartupProblems) > 1 {
124+
content += fmt.Sprintf(" (and %d more)", len(setting.StartupProblems)-1)
125125
}
126126
ctx.Flash.Error(content, true)
127127
}
@@ -136,7 +136,7 @@ func Dashboard(ctx *context.Context) {
136136
updateSystemStatus()
137137
ctx.Data["SysStatus"] = sysStatus
138138
ctx.Data["SSH"] = setting.SSH
139-
prepareDeprecatedWarningsAlert(ctx)
139+
prepareStartupProblemsAlert(ctx)
140140
ctx.HTML(http.StatusOK, tplDashboard)
141141
}
142142

@@ -191,10 +191,10 @@ func DashboardPost(ctx *context.Context) {
191191
func SelfCheck(ctx *context.Context) {
192192
ctx.Data["PageIsAdminSelfCheck"] = true
193193

194-
ctx.Data["DeprecatedWarnings"] = setting.DeprecatedWarnings
195-
if len(setting.DeprecatedWarnings) == 0 && !setting.IsProd {
194+
ctx.Data["StartupProblems"] = setting.StartupProblems
195+
if len(setting.StartupProblems) == 0 && !setting.IsProd {
196196
if time.Now().Unix()%2 == 0 {
197-
ctx.Data["DeprecatedWarnings"] = []string{"This is a test warning message in dev mode"}
197+
ctx.Data["StartupProblems"] = []string{"This is a test warning message in dev mode"}
198198
}
199199
}
200200

routers/web/admin/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func Config(ctx *context.Context) {
165165

166166
ctx.Data["Loggers"] = log.GetManager().DumpLoggers()
167167
config.GetDynGetter().InvalidateCache()
168-
prepareDeprecatedWarningsAlert(ctx)
168+
prepareStartupProblemsAlert(ctx)
169169

170170
ctx.HTML(http.StatusOK, tplConfig)
171171
}

templates/admin/self_check.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
{{ctx.Locale.Tr "admin.self_check"}}
66
</h4>
77

8-
{{if .DeprecatedWarnings}}
8+
{{if .StartupProblems}}
99
<div class="ui attached segment">
1010
<div class="ui warning message">
1111
<div>{{ctx.Locale.Tr "admin.self_check.startup_warnings"}}</div>
12-
<ul class="tw-w-full">{{range .DeprecatedWarnings}}<li>{{.}}</li>{{end}}</ul>
12+
<ul class="tw-w-full">{{range .StartupProblems}}<li>{{.}}</li>{{end}}</ul>
1313
</div>
1414
</div>
1515
{{end}}
@@ -40,7 +40,7 @@
4040
</div>
4141
{{end}}
4242

43-
{{if and (not .DeprecatedWarnings) (not .DatabaseCheckHasProblems)}}
43+
{{if and (not .StartupProblems) (not .DatabaseCheckHasProblems)}}
4444
<div class="ui attached segment">
4545
{{ctx.Locale.Tr "admin.self_check.no_problem_found"}}
4646
</div>

0 commit comments

Comments
 (0)