config.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. }
  21. type result struct {
  22. configPath string
  23. configFile *os.File
  24. config Config
  25. }
  26. func openFile(input ...interface{}) (interface{}, error) {
  27. args := input[0].(result)
  28. configFile, err := os.Open(args.configPath)
  29. args.configFile = configFile
  30. return gott.Tuple{args}, err
  31. }
  32. func scan(input ...interface{}) (interface{}, error) {
  33. args := input[0].(result)
  34. configFile := args.configFile
  35. config := args.config
  36. decoder := toml.NewDecoder(configFile)
  37. _, err := decoder.Decode(&config)
  38. args.config = config
  39. configFile.Close()
  40. return gott.Tuple{args}, err
  41. }
  42. func Read(configPath string) (Config, error) {
  43. input := gott.Tuple{
  44. result{
  45. configPath: configPath,
  46. config: Config{
  47. EnabledFeeds: []string{},
  48. FeedsPath: "/var/lib/szczanieckiej",
  49. ListenAddress: ":51354",
  50. Auth: map[string]Auth{},
  51. },
  52. },
  53. }
  54. output, err := gott.NewResult(input).
  55. Bind(openFile).
  56. Bind(scan).
  57. Finish()
  58. if err != nil {
  59. return Config{}, err
  60. }
  61. return output.(gott.Tuple)[0].(result).config, nil
  62. }