From d428e377bbd317f9c1a00752285ec8c85ad4f141 Mon Sep 17 00:00:00 2001 From: Dennis Jekubczyk Date: Mon, 24 May 2021 13:07:03 +0200 Subject: [PATCH] parse gitea and mirror mode config --- internal/config/read.go | 70 +++++++++++++++++++++ internal/config/read_test.go | 115 +++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 internal/config/read.go create mode 100644 internal/config/read_test.go diff --git a/internal/config/read.go b/internal/config/read.go new file mode 100644 index 0000000..b251a1b --- /dev/null +++ b/internal/config/read.go @@ -0,0 +1,70 @@ +package config + +import ( + "errors" + "os" +) + +const MirrorModePublic = "PUBLIC" +const MirrorModePrivateAndPublic = "PRIVATE_AND_PUBLIC" + +type Config struct { + MirrorMode string + Gitea GiteaConfig +} + +type GiteaConfig struct { + GiteaUrl string + GiteaToken string +} + +func Read() (*Config, error) { + mirrorMode, err := readMirrorMode() + if err != nil { + return nil, err + } + + gitea, err := readGiteaConfig() + if err != nil { + return nil, err + } + + return &Config{ + MirrorMode: mirrorMode, + Gitea: gitea, + }, nil +} + +func readGiteaConfig() (GiteaConfig, error) { + url, a := os.LookupEnv("GITEA_URL") + if !a { + return GiteaConfig{}, errors.New("") + } + + token, a := os.LookupEnv("GITEA_TOKEN") + if !a { + return GiteaConfig{}, errors.New("") + } + + return GiteaConfig{ + GiteaUrl: url, + GiteaToken: token, + }, nil + +} + +func readMirrorMode() (string, error) { + input, present := os.LookupEnv("MIRROR_MODE") + + if !present { + return MirrorModePublic, nil + } + + switch input { + case MirrorModePublic, MirrorModePrivateAndPublic: + return input, nil + default: + return "", errors.New("") + } + +} diff --git a/internal/config/read_test.go b/internal/config/read_test.go new file mode 100644 index 0000000..cf241eb --- /dev/null +++ b/internal/config/read_test.go @@ -0,0 +1,115 @@ +package config_test + +import ( + "github.com/jaedle/mirror-to-gitea/internal/config" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "os" +) + +const mirrorMode = "MIRROR_MODE" +const giteaUrl = "GITEA_URL" +const giteaToken = "GITEA_TOKEN" + +const publicMirrorMode = "PUBLIC" +const privateAndPublicMirrorMode = "PRIVATE_AND_PUBLIC" +const unknownMirrorMode = "UNKNOWN" + +var _ = Describe("Read", func() { + BeforeEach(func() { + os.Clearenv() + }) + + Context("Gitea", func() { + It("parses configuration", func() { + aValidEnv() + setEnv(giteaUrl, "https://gitea.url/api") + setEnv(giteaToken, "a-gitea-token") + + c, err := config.Read() + + Expect(err).NotTo(HaveOccurred()) + Expect(c.Gitea.GiteaUrl).To(Equal("https://gitea.url/api")) + Expect(c.Gitea.GiteaToken).To(Equal("a-gitea-token")) + }) + + It("fails on missing gitea url", func() { + aValidEnv() + unsetEnv(giteaUrl) + + c, err := config.Read() + + Expect(err).To(HaveOccurred()) + Expect(c).To(BeNil()) + }) + + It("fails on missing gitea token", func() { + aValidEnv() + unsetEnv(giteaToken) + + c, err := config.Read() + + Expect(err).To(HaveOccurred()) + Expect(c).To(BeNil()) + }) + }) + + Context("mirror mode", func() { + It("sets default mirror mode", func() { + aValidEnv() + unsetEnv(mirrorMode) + + c, err := config.Read() + + Expect(err).NotTo(HaveOccurred()) + Expect(c.MirrorMode).To(Equal(config.MirrorModePublic)) + }) + + It("allows public mirror mode PUBLIC", func() { + aValidEnv() + setEnv(mirrorMode, publicMirrorMode) + + c, err := config.Read() + + Expect(err).NotTo(HaveOccurred()) + Expect(c.MirrorMode).To(Equal(config.MirrorModePublic)) + }) + + It("allows mirror mode PRIVATE_AND_PUBLIC", func() { + aValidEnv() + setEnv(mirrorMode, privateAndPublicMirrorMode) + + c, err := config.Read() + + Expect(err).NotTo(HaveOccurred()) + Expect(c.MirrorMode).To(Equal(config.MirrorModePrivateAndPublic)) + }) + + It("fails on unknown mirror mode", func() { + aValidEnv() + setEnv(mirrorMode, unknownMirrorMode) + + c, err := config.Read() + + Expect(err).To(HaveOccurred()) + Expect(c).To(BeNil()) + }) + + }) +}) + +func setEnv(k string, v string) { + err := os.Setenv(k, v) + Expect(err).NotTo(HaveOccurred()) +} + +func unsetEnv(k string) { + err := os.Unsetenv(k) + Expect(err).NotTo(HaveOccurred()) +} + +func aValidEnv() { + setEnv(mirrorMode, "PUBLIC") + setEnv(giteaUrl, "https://gitea.url") + setEnv(giteaToken, "valid") +}