Browse Source

parse gitea and mirror mode config

migrate-to-golang
Dennis Jekubczyk 3 years ago
parent
commit
d428e377bb
2 changed files with 185 additions and 0 deletions
  1. +70
    -0
      internal/config/read.go
  2. +115
    -0
      internal/config/read_test.go

+ 70
- 0
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("")
}
}

+ 115
- 0
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")
}

Loading…
Cancel
Save