]> source.dussan.org Git - gitea.git/commitdiff
Refactor startup deprecation messages (#30305)
authorwxiaoguang <wxiaoguang@gmail.com>
Sun, 7 Apr 2024 01:11:25 +0000 (09:11 +0800)
committerGitHub <noreply@github.com>
Sun, 7 Apr 2024 01:11:25 +0000 (01:11 +0000)
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

modules/setting/config_provider.go
modules/setting/indexer.go
modules/setting/oauth2.go
modules/setting/repository.go
modules/setting/server.go
modules/setting/session.go
modules/setting/setting.go
modules/setting/storage.go
routers/web/admin/admin.go
routers/web/admin/config.go
templates/admin/self_check.tmpl

index 3fa3f3b50b822b55845049152bfd9f3cff63e999..03f27ba203f295d301e762f17511d7ff69c84a87 100644 (file)
@@ -315,21 +315,25 @@ func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
        }
 }
 
-// DeprecatedWarnings contains the warning message for various deprecations, including: setting option, file/folder, etc
-var DeprecatedWarnings []string
+// StartupProblems contains the messages for various startup problems, including: setting option, file/folder, etc
+var StartupProblems []string
+
+func logStartupProblem(skip int, level log.Level, format string, args ...any) {
+       msg := fmt.Sprintf(format, args...)
+       log.Log(skip+1, level, "%s", msg)
+       StartupProblems = append(StartupProblems, msg)
+}
 
 func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) {
        if rootCfg.Section(oldSection).HasKey(oldKey) {
-               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)
-               log.Error("%v", msg)
-               DeprecatedWarnings = append(DeprecatedWarnings, msg)
+               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)
        }
 }
 
 // deprecatedSettingDB add a hint that the configuration has been moved to database but still kept in app.ini
 func deprecatedSettingDB(rootCfg ConfigProvider, oldSection, oldKey string) {
        if rootCfg.Section(oldSection).HasKey(oldKey) {
-               log.Error("Deprecated `[%s]` `%s` present which has been copied to database table sys_setting", oldSection, oldKey)
+               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)
        }
 }
 
index cec364d370ced6a50d8258a3a73f7a0c3f343b28..6877d70e3cfcd6c366bc54fe720394ed7d4fafdd 100644 (file)
@@ -58,7 +58,7 @@ func loadIndexerFrom(rootCfg ConfigProvider) {
                if !filepath.IsAbs(Indexer.IssuePath) {
                        Indexer.IssuePath = filepath.ToSlash(filepath.Join(AppWorkPath, Indexer.IssuePath))
                }
-               checkOverlappedPath("indexer.ISSUE_INDEXER_PATH", Indexer.IssuePath)
+               checkOverlappedPath("[indexer].ISSUE_INDEXER_PATH", Indexer.IssuePath)
        } else {
                Indexer.IssueConnStr = sec.Key("ISSUE_INDEXER_CONN_STR").MustString(Indexer.IssueConnStr)
                if Indexer.IssueType == "meilisearch" {
index 4d3bfd3eb60aae981badd6592bd499f16aba3a91..1429a7585c9e5dcacf59660d2576abe105921b99 100644 (file)
@@ -168,7 +168,7 @@ func GetGeneralTokenSigningSecret() []byte {
                }
                if generalSigningSecret.CompareAndSwap(old, &jwtSecret) {
                        // FIXME: in main branch, the signing token should be refactored (eg: one unique for LFS/OAuth2/etc ...)
-                       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")
+                       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")
                        return jwtSecret
                }
                return *generalSigningSecret.Load()
index a332d6adb3757a0530896d30195a9ae5edc515d1..8656ebc7ecfd0bc0d0a132b3cae7b3d7253cc521 100644 (file)
@@ -286,7 +286,7 @@ func loadRepositoryFrom(rootCfg ConfigProvider) {
                RepoRootPath = filepath.Clean(RepoRootPath)
        }
 
-       checkOverlappedPath("repository.ROOT", RepoRootPath)
+       checkOverlappedPath("[repository].ROOT", RepoRootPath)
 
        defaultDetectedCharsetsOrder := make([]string, 0, len(Repository.DetectedCharsetsOrder))
        for _, charset := range Repository.DetectedCharsetsOrder {
index 315faaeb21917940448010dc02be151d365313b5..7d6ece27279ed45ed252f0c36730e10fc2588c20 100644 (file)
@@ -331,7 +331,7 @@ func loadServerFrom(rootCfg ConfigProvider) {
        if !filepath.IsAbs(PprofDataPath) {
                PprofDataPath = filepath.Join(AppWorkPath, PprofDataPath)
        }
-       checkOverlappedPath("server.PPROF_DATA_PATH", PprofDataPath)
+       checkOverlappedPath("[server].PPROF_DATA_PATH", PprofDataPath)
 
        landingPage := sec.Key("LANDING_PAGE").MustString("home")
        switch landingPage {
index 3cb1bfe7b50f9206578d85de14e69302c79bb6b8..afe63bfdb7832605df2da53f070fd70d98d98e8b 100644 (file)
@@ -46,7 +46,7 @@ func loadSessionFrom(rootCfg ConfigProvider) {
        SessionConfig.ProviderConfig = strings.Trim(sec.Key("PROVIDER_CONFIG").MustString(filepath.Join(AppDataPath, "sessions")), "\" ")
        if SessionConfig.Provider == "file" && !filepath.IsAbs(SessionConfig.ProviderConfig) {
                SessionConfig.ProviderConfig = filepath.Join(AppWorkPath, SessionConfig.ProviderConfig)
-               checkOverlappedPath("session.PROVIDER_CONFIG", SessionConfig.ProviderConfig)
+               checkOverlappedPath("[session].PROVIDER_CONFIG", SessionConfig.ProviderConfig)
        }
        SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
        SessionConfig.CookiePath = AppSubURL
index 6aca9ec6cf2e88ba84639e4178bba9b626fd8c1c..92bb0b6541b65b211dc5b3ddc171fce53759871f 100644 (file)
@@ -235,9 +235,7 @@ var configuredPaths = make(map[string]string)
 func checkOverlappedPath(name, path string) {
        // TODO: some paths shouldn't overlap (storage.xxx.path), while some could (data path is the base path for storage path)
        if targetName, ok := configuredPaths[path]; ok && targetName != name {
-               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)
-               log.Error("%s", msg)
-               DeprecatedWarnings = append(DeprecatedWarnings, msg)
+               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)
        }
        configuredPaths[path] = name
 }
index f4e33a53afd8bd42249a609fb6702ef04e1aad2d..aeb61ac513449cd11cad7b24f4b9b3d36d880b0b 100644 (file)
@@ -240,7 +240,7 @@ func getStorageForLocal(targetSec, overrideSec ConfigSection, tp targetSecType,
                }
        }
 
-       checkOverlappedPath("storage."+name+".PATH", storage.Path)
+       checkOverlappedPath("[storage."+name+"].PATH", storage.Path)
 
        return &storage, nil
 }
index 4dc0dfdef811b98e290045baba06f227e6233156..e6585d8833bc3d1c665d46eb9f2100b752a53501 100644 (file)
@@ -117,11 +117,11 @@ func updateSystemStatus() {
        sysStatus.NumGC = m.NumGC
 }
 
-func prepareDeprecatedWarningsAlert(ctx *context.Context) {
-       if len(setting.DeprecatedWarnings) > 0 {
-               content := setting.DeprecatedWarnings[0]
-               if len(setting.DeprecatedWarnings) > 1 {
-                       content += fmt.Sprintf(" (and %d more)", len(setting.DeprecatedWarnings)-1)
+func prepareStartupProblemsAlert(ctx *context.Context) {
+       if len(setting.StartupProblems) > 0 {
+               content := setting.StartupProblems[0]
+               if len(setting.StartupProblems) > 1 {
+                       content += fmt.Sprintf(" (and %d more)", len(setting.StartupProblems)-1)
                }
                ctx.Flash.Error(content, true)
        }
@@ -136,7 +136,7 @@ func Dashboard(ctx *context.Context) {
        updateSystemStatus()
        ctx.Data["SysStatus"] = sysStatus
        ctx.Data["SSH"] = setting.SSH
-       prepareDeprecatedWarningsAlert(ctx)
+       prepareStartupProblemsAlert(ctx)
        ctx.HTML(http.StatusOK, tplDashboard)
 }
 
@@ -191,10 +191,10 @@ func DashboardPost(ctx *context.Context) {
 func SelfCheck(ctx *context.Context) {
        ctx.Data["PageIsAdminSelfCheck"] = true
 
-       ctx.Data["DeprecatedWarnings"] = setting.DeprecatedWarnings
-       if len(setting.DeprecatedWarnings) == 0 && !setting.IsProd {
+       ctx.Data["StartupProblems"] = setting.StartupProblems
+       if len(setting.StartupProblems) == 0 && !setting.IsProd {
                if time.Now().Unix()%2 == 0 {
-                       ctx.Data["DeprecatedWarnings"] = []string{"This is a test warning message in dev mode"}
+                       ctx.Data["StartupProblems"] = []string{"This is a test warning message in dev mode"}
                }
        }
 
index 2f5f17e2013f214a3f5e82df717c7a5020ae08ae..48f80dbbf1cafe5c3fe66de32cd1ec3e72e10a49 100644 (file)
@@ -165,7 +165,7 @@ func Config(ctx *context.Context) {
 
        ctx.Data["Loggers"] = log.GetManager().DumpLoggers()
        config.GetDynGetter().InvalidateCache()
-       prepareDeprecatedWarningsAlert(ctx)
+       prepareStartupProblemsAlert(ctx)
 
        ctx.HTML(http.StatusOK, tplConfig)
 }
index c100ffd504853206470cc68aa75ba43231b4f98f..a6c2ac1ac9de4a127514271646d034c5b8f37ec9 100644 (file)
@@ -5,11 +5,11 @@
                {{ctx.Locale.Tr "admin.self_check"}}
        </h4>
 
-       {{if .DeprecatedWarnings}}
+       {{if .StartupProblems}}
        <div class="ui attached segment">
                <div class="ui warning message">
                        <div>{{ctx.Locale.Tr "admin.self_check.startup_warnings"}}</div>
-                       <ul class="tw-w-full">{{range .DeprecatedWarnings}}<li>{{.}}</li>{{end}}</ul>
+                       <ul class="tw-w-full">{{range .StartupProblems}}<li>{{.}}</li>{{end}}</ul>
                </div>
        </div>
        {{end}}
@@ -40,7 +40,7 @@
        </div>
        {{end}}
 
-       {{if and (not .DeprecatedWarnings) (not .DatabaseCheckHasProblems)}}
+       {{if and (not .StartupProblems) (not .DatabaseCheckHasProblems)}}
        <div class="ui attached segment">
                {{ctx.Locale.Tr "admin.self_check.no_problem_found"}}
        </div>