Allow passing environment variables into config
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
This commit is contained in:
parent
86ea377d64
commit
6c2bb413cd
|
@ -2,6 +2,7 @@ package settings
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -22,27 +23,35 @@ var (
|
||||||
Gitea GiteaConfig
|
Gitea GiteaConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
func Load(configPath string) {
|
func init() {
|
||||||
viper.SetConfigName("config.yaml")
|
viper.SetConfigName("config.yaml")
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
|
viper.SetEnvPrefix("prbot")
|
||||||
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||||
|
viper.AllowEmptyEnv(true)
|
||||||
|
viper.AutomaticEnv()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(configPath string) {
|
||||||
viper.AddConfigPath(configPath)
|
viper.AddConfigPath(configPath)
|
||||||
|
|
||||||
err := viper.ReadInConfig()
|
err := viper.ReadInConfig()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("Fatal error while reading config file: %w \n", err))
|
panic(fmt.Errorf("Fatal error while reading config file: %w \n", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
var giteaConfig GiteaConfig
|
if viper.IsSet("gitea") == false {
|
||||||
|
|
||||||
if viper.Sub("gitea") == nil {
|
|
||||||
panic("Gitea not configured")
|
panic("Gitea not configured")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = viper.UnmarshalKey("gitea", &giteaConfig)
|
var fullConfig struct {
|
||||||
if err != nil {
|
Gitea GiteaConfig
|
||||||
panic(fmt.Errorf("Unable to decode into struct, %v", err))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Gitea = giteaConfig
|
err = viper.Unmarshal(&fullConfig)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("Unable to load config into struct, %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
Gitea = fullConfig.Gitea
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,13 @@ import (
|
||||||
var defaultConfigInlineSecrets []byte = []byte(
|
var defaultConfigInlineSecrets []byte = []byte(
|
||||||
`gitea:
|
`gitea:
|
||||||
url: https://example.com/gitea
|
url: https://example.com/gitea
|
||||||
token: 1337
|
token: d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565
|
||||||
webhookSecret: ""
|
webhookSecret: "haxxor"
|
||||||
repositories: []
|
repositories: []
|
||||||
sonarqube:
|
sonarqube:
|
||||||
url: https://example.com/sonarqube
|
url: https://example.com/sonarqube
|
||||||
token: 42
|
token: a09eb5785b25bb2cbacf48808a677a0709f02d8e
|
||||||
webhookSecret: ""
|
webhookSecret: "haxxor"
|
||||||
projects: []
|
projects: []
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
@ -53,12 +53,33 @@ func TestLoadGiteaStructure(t *testing.T) {
|
||||||
WriteConfigFile(t, defaultConfigInlineSecrets)
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
||||||
Load(os.TempDir())
|
Load(os.TempDir())
|
||||||
|
|
||||||
expected := &GiteaConfig{
|
expected := GiteaConfig{
|
||||||
Url: "https://example.com/gitea",
|
Url: "https://example.com/gitea",
|
||||||
Token: "1337",
|
Token: "d0fcdeb5eaa99c506831f9eb4e63fc7cc484a565",
|
||||||
WebhookSecret: "",
|
WebhookSecret: "haxxor",
|
||||||
Repositories: []GiteaRepository{},
|
Repositories: []GiteaRepository{},
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.True(t, assert.ObjectsAreEqualValues(&Gitea, expected))
|
assert.EqualValues(t, expected, Gitea)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadGiteaStructureWithEnvInjectedWebhookSecret(t *testing.T) {
|
||||||
|
os.Setenv("PRBOT_GITEA_WEBHOOKSECRET", "injected-secret")
|
||||||
|
os.Setenv("PRBOT_GITEA_TOKEN", "injected-token")
|
||||||
|
WriteConfigFile(t, defaultConfigInlineSecrets)
|
||||||
|
Load(os.TempDir())
|
||||||
|
|
||||||
|
expected := GiteaConfig{
|
||||||
|
Url: "https://example.com/gitea",
|
||||||
|
Token: "injected-token",
|
||||||
|
WebhookSecret: "injected-secret",
|
||||||
|
Repositories: []GiteaRepository{},
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.EqualValues(t, expected, Gitea)
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
os.Unsetenv("PRBOT_GITEA_WEBHOOKSECRET")
|
||||||
|
os.Unsetenv("PRBOT_GITEA_TOKEN")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue