You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config_provider.go 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "errors"
  6. "fmt"
  7. "os"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/util"
  14. "gopkg.in/ini.v1" //nolint:depguard
  15. )
  16. type ConfigKey interface {
  17. Name() string
  18. Value() string
  19. SetValue(v string)
  20. In(defaultVal string, candidates []string) string
  21. String() string
  22. Strings(delim string) []string
  23. MustString(defaultVal string) string
  24. MustBool(defaultVal ...bool) bool
  25. MustInt(defaultVal ...int) int
  26. MustInt64(defaultVal ...int64) int64
  27. MustDuration(defaultVal ...time.Duration) time.Duration
  28. }
  29. type ConfigSection interface {
  30. Name() string
  31. MapTo(any) error
  32. HasKey(key string) bool
  33. NewKey(name, value string) (ConfigKey, error)
  34. Key(key string) ConfigKey
  35. Keys() []ConfigKey
  36. ChildSections() []ConfigSection
  37. }
  38. // ConfigProvider represents a config provider
  39. type ConfigProvider interface {
  40. Section(section string) ConfigSection
  41. Sections() []ConfigSection
  42. NewSection(name string) (ConfigSection, error)
  43. GetSection(name string) (ConfigSection, error)
  44. Save() error
  45. SaveTo(filename string) error
  46. DisableSaving()
  47. PrepareSaving() (ConfigProvider, error)
  48. IsLoadedFromEmpty() bool
  49. }
  50. type iniConfigProvider struct {
  51. file string
  52. ini *ini.File
  53. disableSaving bool // disable the "Save" method because the config options could be polluted
  54. loadedFromEmpty bool // whether the file has not existed previously
  55. }
  56. type iniConfigSection struct {
  57. sec *ini.Section
  58. }
  59. var (
  60. _ ConfigProvider = (*iniConfigProvider)(nil)
  61. _ ConfigSection = (*iniConfigSection)(nil)
  62. _ ConfigKey = (*ini.Key)(nil)
  63. )
  64. // ConfigSectionKey only searches the keys in the given section, but it is O(n).
  65. // ini package has a special behavior: with "[sec] a=1" and an empty "[sec.sub]",
  66. // then in "[sec.sub]", Key()/HasKey() can always see "a=1" because it always tries parent sections.
  67. // It returns nil if the key doesn't exist.
  68. func ConfigSectionKey(sec ConfigSection, key string) ConfigKey {
  69. if sec == nil {
  70. return nil
  71. }
  72. for _, k := range sec.Keys() {
  73. if k.Name() == key {
  74. return k
  75. }
  76. }
  77. return nil
  78. }
  79. func ConfigSectionKeyString(sec ConfigSection, key string, def ...string) string {
  80. k := ConfigSectionKey(sec, key)
  81. if k != nil && k.String() != "" {
  82. return k.String()
  83. }
  84. if len(def) > 0 {
  85. return def[0]
  86. }
  87. return ""
  88. }
  89. func ConfigSectionKeyBool(sec ConfigSection, key string, def ...bool) bool {
  90. k := ConfigSectionKey(sec, key)
  91. if k != nil && k.String() != "" {
  92. b, _ := strconv.ParseBool(k.String())
  93. return b
  94. }
  95. if len(def) > 0 {
  96. return def[0]
  97. }
  98. return false
  99. }
  100. // ConfigInheritedKey works like ini.Section.Key(), but it always returns a new key instance, it is O(n) because NewKey is O(n)
  101. // and the returned key is safe to be used with "MustXxx", it doesn't change the parent's values.
  102. // Otherwise, ini.Section.Key().MustXxx would pollute the parent section's keys.
  103. // It never returns nil.
  104. func ConfigInheritedKey(sec ConfigSection, key string) ConfigKey {
  105. k := sec.Key(key)
  106. if k != nil && k.String() != "" {
  107. newKey, _ := sec.NewKey(k.Name(), k.String())
  108. return newKey
  109. }
  110. newKey, _ := sec.NewKey(key, "")
  111. return newKey
  112. }
  113. func ConfigInheritedKeyString(sec ConfigSection, key string, def ...string) string {
  114. k := sec.Key(key)
  115. if k != nil && k.String() != "" {
  116. return k.String()
  117. }
  118. if len(def) > 0 {
  119. return def[0]
  120. }
  121. return ""
  122. }
  123. func (s *iniConfigSection) Name() string {
  124. return s.sec.Name()
  125. }
  126. func (s *iniConfigSection) MapTo(v any) error {
  127. return s.sec.MapTo(v)
  128. }
  129. func (s *iniConfigSection) HasKey(key string) bool {
  130. return s.sec.HasKey(key)
  131. }
  132. func (s *iniConfigSection) NewKey(name, value string) (ConfigKey, error) {
  133. return s.sec.NewKey(name, value)
  134. }
  135. func (s *iniConfigSection) Key(key string) ConfigKey {
  136. return s.sec.Key(key)
  137. }
  138. func (s *iniConfigSection) Keys() (keys []ConfigKey) {
  139. for _, k := range s.sec.Keys() {
  140. keys = append(keys, k)
  141. }
  142. return keys
  143. }
  144. func (s *iniConfigSection) ChildSections() (sections []ConfigSection) {
  145. for _, s := range s.sec.ChildSections() {
  146. sections = append(sections, &iniConfigSection{s})
  147. }
  148. return sections
  149. }
  150. func configProviderLoadOptions() ini.LoadOptions {
  151. return ini.LoadOptions{
  152. KeyValueDelimiterOnWrite: " = ",
  153. IgnoreContinuation: true,
  154. }
  155. }
  156. // NewConfigProviderFromData this function is mainly for testing purpose
  157. func NewConfigProviderFromData(configContent string) (ConfigProvider, error) {
  158. cfg, err := ini.LoadSources(configProviderLoadOptions(), strings.NewReader(configContent))
  159. if err != nil {
  160. return nil, err
  161. }
  162. cfg.NameMapper = ini.SnackCase
  163. return &iniConfigProvider{
  164. ini: cfg,
  165. loadedFromEmpty: true,
  166. }, nil
  167. }
  168. // NewConfigProviderFromFile load configuration from file.
  169. // NOTE: do not print any log except error.
  170. func NewConfigProviderFromFile(file string) (ConfigProvider, error) {
  171. cfg := ini.Empty(configProviderLoadOptions())
  172. loadedFromEmpty := true
  173. if file != "" {
  174. isFile, err := util.IsFile(file)
  175. if err != nil {
  176. return nil, fmt.Errorf("unable to check if %q is a file. Error: %v", file, err)
  177. }
  178. if isFile {
  179. if err = cfg.Append(file); err != nil {
  180. return nil, fmt.Errorf("failed to load config file %q: %v", file, err)
  181. }
  182. loadedFromEmpty = false
  183. }
  184. }
  185. cfg.NameMapper = ini.SnackCase
  186. return &iniConfigProvider{
  187. file: file,
  188. ini: cfg,
  189. loadedFromEmpty: loadedFromEmpty,
  190. }, nil
  191. }
  192. func (p *iniConfigProvider) Section(section string) ConfigSection {
  193. return &iniConfigSection{sec: p.ini.Section(section)}
  194. }
  195. func (p *iniConfigProvider) Sections() (sections []ConfigSection) {
  196. for _, s := range p.ini.Sections() {
  197. sections = append(sections, &iniConfigSection{s})
  198. }
  199. return sections
  200. }
  201. func (p *iniConfigProvider) NewSection(name string) (ConfigSection, error) {
  202. sec, err := p.ini.NewSection(name)
  203. if err != nil {
  204. return nil, err
  205. }
  206. return &iniConfigSection{sec: sec}, nil
  207. }
  208. func (p *iniConfigProvider) GetSection(name string) (ConfigSection, error) {
  209. sec, err := p.ini.GetSection(name)
  210. if err != nil {
  211. return nil, err
  212. }
  213. return &iniConfigSection{sec: sec}, nil
  214. }
  215. var errDisableSaving = errors.New("this config can't be saved, developers should prepare a new config to save")
  216. // Save saves the content into file
  217. func (p *iniConfigProvider) Save() error {
  218. if p.disableSaving {
  219. return errDisableSaving
  220. }
  221. filename := p.file
  222. if filename == "" {
  223. return fmt.Errorf("config file path must not be empty")
  224. }
  225. if p.loadedFromEmpty {
  226. if err := os.MkdirAll(filepath.Dir(filename), os.ModePerm); err != nil {
  227. return fmt.Errorf("failed to create %q: %v", filename, err)
  228. }
  229. }
  230. if err := p.ini.SaveTo(filename); err != nil {
  231. return fmt.Errorf("failed to save %q: %v", filename, err)
  232. }
  233. // Change permissions to be more restrictive
  234. fi, err := os.Stat(filename)
  235. if err != nil {
  236. return fmt.Errorf("failed to determine current conf file permissions: %v", err)
  237. }
  238. if fi.Mode().Perm() > 0o600 {
  239. if err = os.Chmod(filename, 0o600); err != nil {
  240. log.Warn("Failed changing conf file permissions to -rw-------. Consider changing them manually.")
  241. }
  242. }
  243. return nil
  244. }
  245. func (p *iniConfigProvider) SaveTo(filename string) error {
  246. if p.disableSaving {
  247. return errDisableSaving
  248. }
  249. return p.ini.SaveTo(filename)
  250. }
  251. // DisableSaving disables the saving function, use PrepareSaving to get clear config options.
  252. func (p *iniConfigProvider) DisableSaving() {
  253. p.disableSaving = true
  254. }
  255. // PrepareSaving loads the ini from file again to get clear config options.
  256. // Otherwise, the "MustXxx" calls would have polluted the current config provider,
  257. // it makes the "Save" outputs a lot of garbage options
  258. // After the INI package gets refactored, no "MustXxx" pollution, this workaround can be dropped.
  259. func (p *iniConfigProvider) PrepareSaving() (ConfigProvider, error) {
  260. if p.file == "" {
  261. return nil, errors.New("no config file to save")
  262. }
  263. return NewConfigProviderFromFile(p.file)
  264. }
  265. func (p *iniConfigProvider) IsLoadedFromEmpty() bool {
  266. return p.loadedFromEmpty
  267. }
  268. func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting any) {
  269. if err := rootCfg.Section(sectionName).MapTo(setting); err != nil {
  270. log.Fatal("Failed to map %s settings: %v", sectionName, err)
  271. }
  272. }
  273. // StartupProblems contains the messages for various startup problems, including: setting option, file/folder, etc
  274. var StartupProblems []string
  275. func LogStartupProblem(skip int, level log.Level, format string, args ...any) {
  276. msg := fmt.Sprintf(format, args...)
  277. log.Log(skip+1, level, "%s", msg)
  278. StartupProblems = append(StartupProblems, msg)
  279. }
  280. func deprecatedSetting(rootCfg ConfigProvider, oldSection, oldKey, newSection, newKey, version string) {
  281. if rootCfg.Section(oldSection).HasKey(oldKey) {
  282. 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)
  283. }
  284. }
  285. // deprecatedSettingDB add a hint that the configuration has been moved to database but still kept in app.ini
  286. func deprecatedSettingDB(rootCfg ConfigProvider, oldSection, oldKey string) {
  287. if rootCfg.Section(oldSection).HasKey(oldKey) {
  288. 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)
  289. }
  290. }
  291. // NewConfigProviderForLocale loads locale configuration from source and others. "string" if for a local file path, "[]byte" is for INI content
  292. func NewConfigProviderForLocale(source any, others ...any) (ConfigProvider, error) {
  293. iniFile, err := ini.LoadSources(ini.LoadOptions{
  294. IgnoreInlineComment: true,
  295. UnescapeValueCommentSymbols: true,
  296. IgnoreContinuation: true,
  297. }, source, others...)
  298. if err != nil {
  299. return nil, fmt.Errorf("unable to load locale ini: %w", err)
  300. }
  301. iniFile.BlockMode = false
  302. return &iniConfigProvider{
  303. ini: iniFile,
  304. loadedFromEmpty: true,
  305. }, nil
  306. }
  307. func init() {
  308. ini.PrettyFormat = false
  309. }