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.

108 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
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 readGiteaConfig() (Gitea, error) {
  23. url, a := os.LookupEnv("GITEA_URL")
  24. if !a {
  25. return Gitea{}, errors.New("missing mandatory parameter GITEA_URL, please specify your target gitea instance")
  26. }
  27. token, a := os.LookupEnv("GITEA_TOKEN")
  28. if !a {
  29. return Gitea{}, errors.New("missing mandatory parameter GITEA_TOKEN, please specify your gitea application token")
  30. }
  31. return Gitea{
  32. Url: url,
  33. Token: token,
  34. }, nil
  35. }
  36. func readGithubConfig() (Github, error) {
  37. username, present := os.LookupEnv("GITHUB_USERNAME")
  38. if !present {
  39. return Github{}, errors.New("")
  40. }
  41. var token *string = nil
  42. if val, hasToken := os.LookupEnv("GITHUB_TOKEN"); hasToken {
  43. token = &val
  44. }
  45. return Github{
  46. Username: username,
  47. Token: token,
  48. }, nil
  49. }
  50. func readMirrorMode() (string, error) {
  51. input, present := os.LookupEnv("MIRROR_MODE")
  52. if !present {
  53. return MirrorModePublic, nil
  54. }
  55. switch input {
  56. case MirrorModePublic, MirrorModePrivateAndPublic:
  57. return input, nil
  58. default:
  59. return "", fmt.Errorf("unknown mirror mode %s, please specify a valid mirror mode: PUBLIC, PRIVATE_AND_PUBLIC", input)
  60. }
  61. }
  62. type Reader struct {
  63. }
  64. func (r Reader) Read() (*Config, error) {
  65. mirrorMode, err := readMirrorMode()
  66. if err != nil {
  67. return nil, err
  68. }
  69. gitea, err := readGiteaConfig()
  70. if err != nil {
  71. return nil, err
  72. }
  73. github, err := readGithubConfig()
  74. if err != nil {
  75. return nil, err
  76. }
  77. return &Config{
  78. MirrorMode: mirrorMode,
  79. Gitea: gitea,
  80. Github: github,
  81. }, nil
  82. }
  83. func NewReader() *Reader {
  84. return &Reader{}
  85. }