config.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "log"
  4. "os"
  5. "time"
  6. "gopkg.in/yaml.v2"
  7. )
  8. const (
  9. HttpUserAgent = "gof"
  10. )
  11. var (
  12. configFile string
  13. )
  14. type feed struct {
  15. URL, Template string
  16. Format string
  17. Visibility string
  18. Sensitive bool
  19. ContentWarning string
  20. }
  21. type config struct {
  22. Accounts []account
  23. LastUpdated time.Time
  24. HttpConfig httpConfig
  25. }
  26. type account struct {
  27. AccessToken string
  28. Name string
  29. InstanceURL string
  30. Feeds []feed
  31. }
  32. type httpConfig struct {
  33. UserAgent string
  34. }
  35. func readConfig(fileName string) *config {
  36. log.Println("reading config...")
  37. configFile = fileName
  38. config := new(config)
  39. cf, err := os.ReadFile(configFile)
  40. if err != nil {
  41. log.Fatalln("Failed to read config: ", err)
  42. }
  43. err = yaml.Unmarshal(cf, &config)
  44. if err != nil {
  45. log.Panic(err)
  46. }
  47. if debug {
  48. log.Printf("Config:\n\n%v", config)
  49. }
  50. config.HttpConfig.UserAgent = HttpUserAgent
  51. return config
  52. }
  53. func (cf *config) updateLastUpdated() {
  54. log.Println("updating LastUpdated key...")
  55. cf.LastUpdated = time.Now()
  56. }
  57. func (cf *config) Save() error {
  58. log.Println("saving config to file...")
  59. cfBytes, err := yaml.Marshal(cf)
  60. if err != nil {
  61. return err
  62. }
  63. err = os.WriteFile(configFile, cfBytes, 0644)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }