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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  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 Gitea
  12. Github Github
  13. }
  14. type Gitea struct {
  15. Url string
  16. Token string
  17. }
  18. type Github 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() (Gitea, error) {
  42. url, a := os.LookupEnv("GITEA_URL")
  43. if !a {
  44. return Gitea{}, 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 Gitea{}, errors.New("missing mandatory parameter GITEA_TOKEN, please specify your gitea application token")
  49. }
  50. return Gitea{
  51. Url: url,
  52. Token: token,
  53. }, nil
  54. }
  55. func readGithubConfig() (Github, error) {
  56. username, present := os.LookupEnv("GITHUB_USERNAME")
  57. if !present {
  58. return Github{}, errors.New("")
  59. }
  60. var token *string = nil
  61. if val, hasToken := os.LookupEnv("GITHUB_TOKEN"); hasToken {
  62. token = &val
  63. }
  64. return Github{
  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. }