gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "sync"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/modules/setting/config"
  8. )
  9. type PictureStruct struct {
  10. DisableGravatar *config.Value[bool]
  11. EnableFederatedAvatar *config.Value[bool]
  12. }
  13. type OpenWithEditorApp struct {
  14. DisplayName string
  15. OpenURL string
  16. }
  17. type OpenWithEditorAppsType []OpenWithEditorApp
  18. func (t OpenWithEditorAppsType) ToTextareaString() string {
  19. ret := ""
  20. for _, app := range t {
  21. ret += app.DisplayName + " = " + app.OpenURL + "\n"
  22. }
  23. return ret
  24. }
  25. func DefaultOpenWithEditorApps() OpenWithEditorAppsType {
  26. return OpenWithEditorAppsType{
  27. {
  28. DisplayName: "VS Code",
  29. OpenURL: "vscode://vscode.git/clone?url={url}",
  30. },
  31. {
  32. DisplayName: "VSCodium",
  33. OpenURL: "vscodium://vscode.git/clone?url={url}",
  34. },
  35. {
  36. DisplayName: "Intellij IDEA",
  37. OpenURL: "jetbrains://idea/checkout/git?idea.required.plugins.id=Git4Idea&checkout.repo={url}",
  38. },
  39. }
  40. }
  41. type RepositoryStruct struct {
  42. OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
  43. GitGuideRemoteName *config.Value[string]
  44. }
  45. type ConfigStruct struct {
  46. Picture *PictureStruct
  47. Repository *RepositoryStruct
  48. }
  49. var (
  50. defaultConfig *ConfigStruct
  51. defaultConfigOnce sync.Once
  52. )
  53. func initDefaultConfig() {
  54. config.SetCfgSecKeyGetter(&cfgSecKeyGetter{})
  55. defaultConfig = &ConfigStruct{
  56. Picture: &PictureStruct{
  57. DisableGravatar: config.ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "DISABLE_GRAVATAR"}),
  58. EnableFederatedAvatar: config.ValueJSON[bool]("picture.enable_federated_avatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "ENABLE_FEDERATED_AVATAR"}),
  59. },
  60. Repository: &RepositoryStruct{
  61. OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
  62. GitGuideRemoteName: config.ValueJSON[string]("repository.git-guide-remote-name").WithDefault("origin"),
  63. },
  64. }
  65. }
  66. func Config() *ConfigStruct {
  67. defaultConfigOnce.Do(initDefaultConfig)
  68. return defaultConfig
  69. }
  70. type cfgSecKeyGetter struct{}
  71. func (c cfgSecKeyGetter) GetValue(sec, key string) (v string, has bool) {
  72. if key == "" {
  73. return "", false
  74. }
  75. cfgSec, err := CfgProvider.GetSection(sec)
  76. if err != nil {
  77. log.Error("Unable to get config section: %q", sec)
  78. return "", false
  79. }
  80. cfgKey := ConfigSectionKey(cfgSec, key)
  81. if cfgKey == nil {
  82. return "", false
  83. }
  84. return cfgKey.Value(), true
  85. }