config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package jotunheim
  2. import (
  3. "fmt"
  4. "os"
  5. "apiote.xyz/p/go-dirty"
  6. )
  7. type TyrConfig struct {
  8. ImapAddress string
  9. ImapUsername string
  10. ImapPassword string
  11. ImapFolderInbox string
  12. ImapFolderJunk string
  13. ImapFolderArchive string
  14. ImapFolderTrash string
  15. ImapFolderDrafts string
  16. ImapFolderQuarantine string
  17. ImapFolderSent string
  18. RecipientDomain string
  19. MainEmailAddress string
  20. }
  21. type HermodrConfig struct {
  22. ImapAddress string
  23. ImapUsername string
  24. ImapPassword string
  25. ImapFolderInbox string
  26. ImapFolderRedirected string
  27. Recipient string
  28. SmtpServer string
  29. SmtpUsername string
  30. PublicKey string
  31. }
  32. type MimirConfig struct {
  33. ImapAddress string
  34. ImapUsername string
  35. ImapPassword string
  36. ImapInbox string
  37. RecipientTemplate string
  38. Categories []string
  39. ForwardAddress string
  40. PersonalAddress string
  41. SmtpAddress string
  42. SmtpSender string
  43. Companion string
  44. }
  45. type EostreConfig struct {
  46. ImapAddress string
  47. ImapUsername string
  48. ImapPassword string
  49. DiaryImapAddress string
  50. DiaryImapUsername string
  51. DiaryImapPassword string
  52. DiarySmtpAddress string
  53. DiarySmtpUsername string
  54. DiarySmtpPassword string
  55. DiarySubject string
  56. DiarySender string
  57. DiaryRecipient string
  58. AuthorisedSender string
  59. PrivateKeyPass string
  60. PrivateKey string
  61. PublicKey string
  62. DiaryPrivateKey string
  63. DiaryPublicKey string
  64. }
  65. type GersemiConfig struct {
  66. FireflyToken string
  67. Firefly string
  68. ImapAddress string
  69. ImapUsername string
  70. ImapPassword string
  71. ImapInbox string
  72. MessageMime string
  73. DoneFolder string
  74. DefaultSource string
  75. WithdrawalRegexes []string
  76. DepositRegexes []string
  77. Accounts map[string]string
  78. }
  79. type Config struct {
  80. Tyr TyrConfig
  81. Hermodr HermodrConfig
  82. Mimir MimirConfig
  83. Eostre EostreConfig
  84. Gersemi GersemiConfig
  85. }
  86. func Read(configPath string) (Config, error) {
  87. config := Config{}
  88. userConfigDir, err := os.UserConfigDir()
  89. if err != nil {
  90. return config, fmt.Errorf("while getting user config dir: %w", err)
  91. }
  92. possibleConfigs := []string{
  93. configPath,
  94. "/etc/asgard.dirty",
  95. userConfigDir + "/asgard.dirty",
  96. "asgard.dirty",
  97. }
  98. finalConfigPath := ""
  99. for _, possibleConfig := range possibleConfigs {
  100. _, err := os.Stat(possibleConfig)
  101. if err == nil {
  102. finalConfigPath = possibleConfig
  103. break
  104. }
  105. }
  106. if finalConfigPath == "" {
  107. return config, fmt.Errorf("no config found")
  108. }
  109. file, err := os.Open(finalConfigPath)
  110. if err != nil {
  111. return config, fmt.Errorf("while opening config: %w", err)
  112. }
  113. defer file.Close()
  114. err = dirty.LoadStruct(file, &config)
  115. return config, err
  116. }