You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.1 KiB

  1. package config
  2. import (
  3. "errors"
  4. "os"
  5. )
  6. const MirrorModePublic = "PUBLIC"
  7. const MirrorModePrivateAndPublic = "PRIVATE_AND_PUBLIC"
  8. type Config struct {
  9. MirrorMode string
  10. Gitea GiteaConfig
  11. }
  12. type GiteaConfig struct {
  13. GiteaUrl string
  14. GiteaToken string
  15. }
  16. func Read() (*Config, error) {
  17. mirrorMode, err := readMirrorMode()
  18. if err != nil {
  19. return nil, err
  20. }
  21. gitea, err := readGiteaConfig()
  22. if err != nil {
  23. return nil, err
  24. }
  25. return &Config{
  26. MirrorMode: mirrorMode,
  27. Gitea: gitea,
  28. }, nil
  29. }
  30. func readGiteaConfig() (GiteaConfig, error) {
  31. url, a := os.LookupEnv("GITEA_URL")
  32. if !a {
  33. return GiteaConfig{}, errors.New("")
  34. }
  35. token, a := os.LookupEnv("GITEA_TOKEN")
  36. if !a {
  37. return GiteaConfig{}, errors.New("")
  38. }
  39. return GiteaConfig{
  40. GiteaUrl: url,
  41. GiteaToken: token,
  42. }, nil
  43. }
  44. func readMirrorMode() (string, error) {
  45. input, present := os.LookupEnv("MIRROR_MODE")
  46. if !present {
  47. return MirrorModePublic, nil
  48. }
  49. switch input {
  50. case MirrorModePublic, MirrorModePrivateAndPublic:
  51. return input, nil
  52. default:
  53. return "", errors.New("")
  54. }
  55. }