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.

101 lines
1.8 KiB

  1. package config
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. )
  7. const MirrorModePublic = "PUBLIC"
  8. const MirrorModePrivateAndPublic = "PRIVATE_AND_PUBLIC"
  9. type Config struct {
  10. MirrorMode string
  11. Gitea GiteaConfig
  12. Github GithubConfig
  13. }
  14. type GiteaConfig struct {
  15. GiteaUrl string
  16. GiteaToken string
  17. }
  18. type GithubConfig struct {
  19. Username string
  20. Token *string
  21. }
  22. func Read() (*Config, error) {
  23. mirrorMode, err := readMirrorMode()
  24. if err != nil {
  25. return nil, err
  26. }
  27. gitea, err := readGiteaConfig()
  28. if err != nil {
  29. return nil, err
  30. }
  31. github, err := readGithubConfig()
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &Config{
  36. MirrorMode: mirrorMode,
  37. Gitea: gitea,
  38. Github: github,
  39. }, nil
  40. }
  41. func readGiteaConfig() (GiteaConfig, error) {
  42. url, a := os.LookupEnv("GITEA_URL")
  43. if !a {
  44. return GiteaConfig{}, errors.New("missing mandatory parameter GITEA_URL, please specify your target gitea instance")
  45. }
  46. token, a := os.LookupEnv("GITEA_TOKEN")
  47. if !a {
  48. return GiteaConfig{}, errors.New("missing mandatory parameter GITEA_TOKEN, please specify your gitea application token")
  49. }
  50. return GiteaConfig{
  51. GiteaUrl: url,
  52. GiteaToken: token,
  53. }, nil
  54. }
  55. func readGithubConfig() (GithubConfig, error) {
  56. username, present := os.LookupEnv("GITHUB_USERNAME")
  57. if !present {
  58. return GithubConfig{}, errors.New("")
  59. }
  60. var token *string = nil
  61. if val, hasToken := os.LookupEnv("GITHUB_TOKEN"); hasToken {
  62. token = &val
  63. }
  64. return GithubConfig{
  65. Username: username,
  66. Token: token,
  67. }, nil
  68. }
  69. func readMirrorMode() (string, error) {
  70. input, present := os.LookupEnv("MIRROR_MODE")
  71. if !present {
  72. return MirrorModePublic, nil
  73. }
  74. switch input {
  75. case MirrorModePublic, MirrorModePrivateAndPublic:
  76. return input, nil
  77. default:
  78. return "", fmt.Errorf("unknown mirror mode %s, please specify a valid mirror mode: PUBLIC, PRIVATE_AND_PUBLIC", input)
  79. }
  80. }