2021-06-20 15:16:06 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-20 19:47:55 +02:00
|
|
|
"io/ioutil"
|
2021-06-20 18:24:12 +02:00
|
|
|
"strings"
|
2021-06-20 15:16:06 +02:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
type GiteaRepository struct {
|
|
|
|
Owner string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
type WebhookSecret struct {
|
|
|
|
Value string
|
|
|
|
File string
|
|
|
|
}
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
type GiteaConfig struct {
|
|
|
|
Url string
|
|
|
|
Token string
|
2021-06-20 19:47:55 +02:00
|
|
|
WebhookSecret WebhookSecret `mapstructure:"webhookSecret"`
|
2021-06-20 16:49:12 +02:00
|
|
|
Repositories []GiteaRepository
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
type SonarQubeConfig struct {
|
|
|
|
Url string
|
|
|
|
Token string
|
|
|
|
WebhookSecret WebhookSecret `mapstructure:"webhookSecret"`
|
|
|
|
Projects []string
|
|
|
|
}
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
var (
|
|
|
|
Gitea GiteaConfig
|
2021-06-20 19:47:55 +02:00
|
|
|
SonarQube SonarQubeConfig
|
2021-06-20 16:49:12 +02:00
|
|
|
)
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
func init() {
|
2021-06-20 15:16:06 +02:00
|
|
|
viper.SetConfigName("config.yaml")
|
|
|
|
viper.SetConfigType("yaml")
|
2021-06-20 18:24:12 +02:00
|
|
|
viper.SetEnvPrefix("prbot")
|
|
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
viper.AllowEmptyEnv(true)
|
|
|
|
viper.AutomaticEnv()
|
2021-06-20 19:47:55 +02:00
|
|
|
|
|
|
|
ApplyConfigDefaults()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ApplyConfigDefaults() {
|
|
|
|
viper.SetDefault("gitea.url", "")
|
|
|
|
viper.SetDefault("gitea.token", "")
|
|
|
|
viper.SetDefault("gitea.webhookSecret.value", "")
|
|
|
|
viper.SetDefault("gitea.webhookSecret.file", "")
|
2021-06-21 10:31:11 +02:00
|
|
|
viper.SetDefault("gitea.repositories", []GiteaRepository{})
|
2021-06-20 19:47:55 +02:00
|
|
|
viper.SetDefault("sonarqube.url", "")
|
|
|
|
viper.SetDefault("sonarqube.token", "")
|
|
|
|
viper.SetDefault("sonarqube.webhookSecret.value", "")
|
|
|
|
viper.SetDefault("sonarqube.webhookSecret.file", "")
|
|
|
|
viper.SetDefault("sonarqube.projects", []string{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadSecretFile(file string) string {
|
|
|
|
content, err := ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Cannot read '%s' or it is no regular file. %w", file, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(content)
|
2021-06-20 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Load(configPath string) {
|
2021-06-20 15:16:06 +02:00
|
|
|
viper.AddConfigPath(configPath)
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
2021-06-20 16:49:12 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Fatal error while reading config file: %w \n", err))
|
|
|
|
}
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
var fullConfig struct {
|
|
|
|
Gitea GiteaConfig
|
2021-06-20 19:47:55 +02:00
|
|
|
SonarQube SonarQubeConfig `mapstructure:"sonarqube"`
|
2021-06-20 18:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = viper.Unmarshal(&fullConfig)
|
2021-06-20 16:49:12 +02:00
|
|
|
if err != nil {
|
2021-06-20 18:24:12 +02:00
|
|
|
panic(fmt.Errorf("Unable to load config into struct, %v", err))
|
2021-06-20 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
Gitea = fullConfig.Gitea
|
2021-06-20 19:47:55 +02:00
|
|
|
SonarQube = fullConfig.SonarQube
|
|
|
|
|
|
|
|
if Gitea.WebhookSecret.File != "" {
|
|
|
|
Gitea.WebhookSecret.Value = ReadSecretFile(Gitea.WebhookSecret.File)
|
|
|
|
}
|
|
|
|
|
|
|
|
if SonarQube.WebhookSecret.File != "" {
|
|
|
|
SonarQube.WebhookSecret.Value = ReadSecretFile(SonarQube.WebhookSecret.File)
|
|
|
|
}
|
2021-06-20 15:16:06 +02:00
|
|
|
}
|