config.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package config
  5. import (
  6. "os"
  7. "github.com/BurntSushi/toml"
  8. "notabug.org/apiote/gott"
  9. )
  10. // TODO change to dirty
  11. type Auth struct {
  12. ApiKey string
  13. KeyHeader string
  14. }
  15. type Config struct {
  16. FeedsPath string
  17. EnabledFeeds []string
  18. ListenAddress string
  19. Auth map[string]Auth
  20. LogLevel string
  21. }
  22. type result struct {
  23. configPath string
  24. configFile *os.File
  25. config Config
  26. }
  27. func openFile(input ...interface{}) (interface{}, error) {
  28. args := input[0].(result)
  29. configFile, err := os.Open(args.configPath)
  30. args.configFile = configFile
  31. return gott.Tuple{args}, err
  32. }
  33. func scan(input ...interface{}) (interface{}, error) {
  34. args := input[0].(result)
  35. configFile := args.configFile
  36. config := args.config
  37. decoder := toml.NewDecoder(configFile)
  38. _, err := decoder.Decode(&config)
  39. args.config = config
  40. configFile.Close()
  41. return gott.Tuple{args}, err
  42. }
  43. func Read(configPath string) (Config, error) {
  44. input := gott.Tuple{
  45. result{
  46. configPath: configPath,
  47. config: Config{
  48. EnabledFeeds: []string{},
  49. FeedsPath: "/var/lib/szczanieckiej",
  50. ListenAddress: ":51354",
  51. Auth: map[string]Auth{},
  52. LogLevel: "warn-1",
  53. },
  54. },
  55. }
  56. output, err := gott.NewResult(input).
  57. Bind(openFile).
  58. Bind(scan).
  59. Finish()
  60. if err != nil {
  61. return Config{}, err
  62. }
  63. return output.(gott.Tuple)[0].(result).config, nil
  64. }