config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2016 Martin Hebnes Pedersen (LA5NTA). All rights reserved.
  2. // Use of this source code is governed by the MIT-license that can be
  3. // found in the LICENSE file.
  4. package main
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path"
  12. "github.com/la5nta/pat/cfg"
  13. )
  14. func LoadConfig(path string, fallback cfg.Config) (config cfg.Config, err error) {
  15. config, err = ReadConfig(path)
  16. if os.IsNotExist(err) {
  17. return fallback, WriteConfig(fallback, path)
  18. } else if err != nil {
  19. return config, err
  20. }
  21. // Ensure the alias "telnet" exists
  22. if config.ConnectAliases == nil {
  23. config.ConnectAliases = make(map[string]string)
  24. }
  25. if _, exists := config.ConnectAliases["telnet"]; !exists {
  26. config.ConnectAliases["telnet"] = cfg.DefaultConfig.ConnectAliases["telnet"]
  27. }
  28. // Ensure ServiceCodes has a default value
  29. if len(config.ServiceCodes) == 0 {
  30. config.ServiceCodes = cfg.DefaultConfig.ServiceCodes
  31. }
  32. // Ensure Pactor has a default value
  33. if config.Pactor == (cfg.PactorConfig{}) {
  34. config.Pactor = cfg.DefaultConfig.Pactor
  35. }
  36. //TODO: Remove after some release cycles (2019-09-29)
  37. if config.GPSdAddrLegacy != "" {
  38. config.GPSd.Addr = config.GPSdAddrLegacy
  39. }
  40. return config, nil
  41. }
  42. func replaceDeprecatedCMSHostname(path string, data []byte) ([]byte, error) {
  43. const o = "@server.winlink.org:8772/wl2k"
  44. const n = "@cms.winlink.org:8772/wl2k"
  45. if !bytes.Contains(data, []byte(o)) {
  46. return data, nil
  47. }
  48. data = bytes.Replace(data, []byte(o), []byte(n), -1)
  49. f, err := os.Open(path)
  50. if err != nil {
  51. return data, err
  52. }
  53. stat, err := f.Stat()
  54. f.Close()
  55. if err != nil {
  56. return data, err
  57. }
  58. return data, ioutil.WriteFile(path, data, stat.Mode())
  59. }
  60. func ReadConfig(path string) (config cfg.Config, err error) {
  61. data, err := ioutil.ReadFile(path)
  62. if err != nil {
  63. return
  64. }
  65. //TODO: Remove after some release cycles (2017-11-09)
  66. data, err = replaceDeprecatedCMSHostname(path, data)
  67. if err != nil {
  68. fmt.Println("Failed to rewrite deprecated CMS hostname:", err)
  69. fmt.Println("Please update your config's 'telnet' connect alias manually to:")
  70. fmt.Println(cfg.DefaultConfig.ConnectAliases["telnet"])
  71. fmt.Println("")
  72. }
  73. err = json.Unmarshal(data, &config)
  74. return
  75. }
  76. func WriteConfig(config cfg.Config, filePath string) error {
  77. b, err := json.MarshalIndent(config, "", " ")
  78. if err != nil {
  79. return err
  80. }
  81. // Add trailing new-line
  82. b = append(b, '\n')
  83. // Ensure path dir is available
  84. os.Mkdir(path.Dir(filePath), os.ModePerm|os.ModeDir)
  85. return ioutil.WriteFile(filePath, b, 0600)
  86. }