Browse Source

refactoring

migrate-to-golang
Dennis Jekubczyk 3 years ago
parent
commit
5bde8d1f36
5 changed files with 73 additions and 33 deletions
  1. +13
    -0
      internal/app/app_suite_test.go
  2. +7
    -0
      internal/app/mirror.go
  3. +9
    -0
      internal/app/mirror_test.go
  4. +30
    -23
      internal/config/read.go
  5. +14
    -10
      internal/config/read_test.go

+ 13
- 0
internal/app/app_suite_test.go

@ -0,0 +1,13 @@
package app_test
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestApp(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "App Suite")
}

+ 7
- 0
internal/app/mirror.go

@ -0,0 +1,7 @@
package app
import "github.com/jaedle/mirror-to-gitea/internal/config"
type ConfigReader interface {
Read() (*config.Config, error)
}

+ 9
- 0
internal/app/mirror_test.go

@ -0,0 +1,9 @@
package app_test
import (
. "github.com/onsi/ginkgo"
)
var _ = Describe("Mirror", func() {
})

+ 30
- 23
internal/config/read.go

@ -25,29 +25,6 @@ type Github struct {
Token *string Token *string
} }
func Read() (*Config, error) {
mirrorMode, err := readMirrorMode()
if err != nil {
return nil, err
}
gitea, err := readGiteaConfig()
if err != nil {
return nil, err
}
github, err := readGithubConfig()
if err != nil {
return nil, err
}
return &Config{
MirrorMode: mirrorMode,
Gitea: gitea,
Github: github,
}, nil
}
func readGiteaConfig() (Gitea, error) { func readGiteaConfig() (Gitea, error) {
url, a := os.LookupEnv("GITEA_URL") url, a := os.LookupEnv("GITEA_URL")
if !a { if !a {
@ -99,3 +76,33 @@ func readMirrorMode() (string, error) {
} }
} }
type Reader struct {
}
func (r Reader) Read() (*Config, error) {
mirrorMode, err := readMirrorMode()
if err != nil {
return nil, err
}
gitea, err := readGiteaConfig()
if err != nil {
return nil, err
}
github, err := readGithubConfig()
if err != nil {
return nil, err
}
return &Config{
MirrorMode: mirrorMode,
Gitea: gitea,
Github: github,
}, nil
}
func NewReader() *Reader {
return &Reader{}
}

+ 14
- 10
internal/config/read_test.go

@ -23,14 +23,18 @@ const aGithubUsername = "a-github-user"
const aGithubToken = "a-github-token" const aGithubToken = "a-github-token"
var _ = Describe("Read", func() { var _ = Describe("Read", func() {
var reader *config.Reader
BeforeEach(func() { BeforeEach(func() {
os.Clearenv() os.Clearenv()
reader = config.NewReader()
}) })
It("parses valid configuration", func() { It("parses valid configuration", func() {
aValidEnv() aValidEnv()
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c).ToNot(BeNil()) Expect(c).ToNot(BeNil())
}) })
@ -41,7 +45,7 @@ var _ = Describe("Read", func() {
setEnv(githubUsername, aGithubUsername) setEnv(githubUsername, aGithubUsername)
unsetEnv(githubToken) unsetEnv(githubToken)
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.Github.Username).To(Equal(aGithubUsername)) Expect(c.Github.Username).To(Equal(aGithubUsername))
@ -53,7 +57,7 @@ var _ = Describe("Read", func() {
setEnv(githubUsername, aGithubUsername) setEnv(githubUsername, aGithubUsername)
setEnv(githubToken, aGithubToken) setEnv(githubToken, aGithubToken)
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.Github.Username).To(Equal(aGithubUsername)) Expect(c.Github.Username).To(Equal(aGithubUsername))
@ -64,7 +68,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
unsetEnv(githubUsername) unsetEnv(githubUsername)
c, err := config.Read()
c, err := reader.Read()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(c).To(BeNil()) Expect(c).To(BeNil())
@ -78,7 +82,7 @@ var _ = Describe("Read", func() {
setEnv(giteaUrl, "https://gitea.url/api") setEnv(giteaUrl, "https://gitea.url/api")
setEnv(giteaToken, "a-gitea-token") setEnv(giteaToken, "a-gitea-token")
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.Gitea.Url).To(Equal("https://gitea.url/api")) Expect(c.Gitea.Url).To(Equal("https://gitea.url/api"))
@ -89,7 +93,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
unsetEnv(giteaUrl) unsetEnv(giteaUrl)
c, err := config.Read()
c, err := reader.Read()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(c).To(BeNil()) Expect(c).To(BeNil())
@ -100,7 +104,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
unsetEnv(giteaToken) unsetEnv(giteaToken)
c, err := config.Read()
c, err := reader.Read()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(c).To(BeNil()) Expect(c).To(BeNil())
@ -114,7 +118,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
unsetEnv(mirrorMode) unsetEnv(mirrorMode)
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.MirrorMode).To(Equal(config.MirrorModePublic)) Expect(c.MirrorMode).To(Equal(config.MirrorModePublic))
@ -124,7 +128,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
setEnv(mirrorMode, in) setEnv(mirrorMode, in)
c, err := config.Read()
c, err := reader.Read()
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.MirrorMode).To(Equal(exp)) Expect(c.MirrorMode).To(Equal(exp))
@ -137,7 +141,7 @@ var _ = Describe("Read", func() {
aValidEnv() aValidEnv()
setEnv(mirrorMode, unknownMirrorMode) setEnv(mirrorMode, unknownMirrorMode)
c, err := config.Read()
c, err := reader.Read()
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
Expect(c).To(BeNil()) Expect(c).To(BeNil())

Loading…
Cancel
Save