update.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package config
  17. import (
  18. "io/ioutil"
  19. "gopkg.in/yaml.v2"
  20. )
  21. func Update(path, basePath string) error {
  22. oldCfgData, err := ioutil.ReadFile(path)
  23. if err != nil {
  24. return err
  25. }
  26. oldCfg := make(RecursiveMap)
  27. err = yaml.Unmarshal(oldCfgData, &oldCfg)
  28. if err != nil {
  29. return err
  30. }
  31. baseCfgData, err := ioutil.ReadFile(basePath)
  32. if err != nil {
  33. return err
  34. }
  35. baseCfg := make(RecursiveMap)
  36. err = yaml.Unmarshal(baseCfgData, &baseCfg)
  37. if err != nil {
  38. return err
  39. }
  40. err = runUpdate(oldCfg, baseCfg)
  41. if err != nil {
  42. return err
  43. }
  44. newCfgData, err := yaml.Marshal(&baseCfg)
  45. if err != nil {
  46. return err
  47. }
  48. return ioutil.WriteFile(path, newCfgData, 0600)
  49. }
  50. func runUpdate(oldCfg, newCfg RecursiveMap) error {
  51. cp := func(path string) {
  52. newCfg.CopyFrom(oldCfg, path)
  53. }
  54. cp("homeserver.address")
  55. cp("homeserver.domain")
  56. cp("appservice.address")
  57. cp("appservice.hostname")
  58. cp("appservice.port")
  59. cp("appservice.database.type")
  60. cp("appservice.database.uri")
  61. cp("appservice.state_store_path")
  62. cp("appservice.id")
  63. cp("appservice.bot.username")
  64. cp("appservice.bot.displayname")
  65. cp("appservice.bot.avatar")
  66. cp("appservice.bot.as_token")
  67. cp("appservice.bot.hs_token")
  68. cp("bridge.username_template")
  69. cp("bridge.displayname_template")
  70. cp("bridge.command_prefix")
  71. cp("bridge.permissions")
  72. cp("logging.directory")
  73. cp("logging.file_name_format")
  74. cp("logging.file_date_format")
  75. cp("logging.file_mode")
  76. cp("logging.timestamp_format")
  77. cp("logging.print_level")
  78. return nil
  79. }