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-21 11:08:56 +02:00
|
|
|
type Token struct {
|
|
|
|
Value string
|
|
|
|
File string
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:00:31 +02:00
|
|
|
type Webhook struct {
|
|
|
|
Secret string
|
|
|
|
SecretFile string
|
2021-06-20 19:47:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
type GiteaConfig struct {
|
|
|
|
Url string
|
2021-06-21 11:08:56 +02:00
|
|
|
Token Token
|
|
|
|
Webhook Webhook
|
2021-06-20 16:49:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
type SonarQubeConfig struct {
|
|
|
|
Url string
|
2021-06-21 11:08:56 +02:00
|
|
|
Token Token
|
|
|
|
Webhook Webhook
|
2021-06-22 11:27:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Project struct {
|
|
|
|
SonarQube struct {
|
|
|
|
Key string
|
|
|
|
} `mapstructure:"sonarqube"`
|
|
|
|
Gitea GiteaRepository
|
2021-06-20 19:47:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-20 16:49:12 +02:00
|
|
|
var (
|
|
|
|
Gitea GiteaConfig
|
2021-06-20 19:47:55 +02:00
|
|
|
SonarQube SonarQubeConfig
|
2021-06-22 11:27:53 +02:00
|
|
|
Projects []Project
|
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", "")
|
2021-06-21 11:08:56 +02:00
|
|
|
viper.SetDefault("gitea.token.value", "")
|
|
|
|
viper.SetDefault("gitea.token.file", "")
|
2021-06-21 11:00:31 +02:00
|
|
|
viper.SetDefault("gitea.webhook.secret", "")
|
|
|
|
viper.SetDefault("gitea.webhook.secretFile", "")
|
2021-06-21 11:08:56 +02:00
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
viper.SetDefault("sonarqube.url", "")
|
2021-06-21 11:08:56 +02:00
|
|
|
viper.SetDefault("sonarqube.token.value", "")
|
|
|
|
viper.SetDefault("sonarqube.token.file", "")
|
2021-06-21 11:00:31 +02:00
|
|
|
viper.SetDefault("sonarqube.webhook.secret", "")
|
|
|
|
viper.SetDefault("sonarqube.webhook.secretFile", "")
|
2021-06-22 11:27:53 +02:00
|
|
|
|
|
|
|
viper.SetDefault("projects", []Project{})
|
2021-06-20 19:47:55 +02:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:30:02 +02:00
|
|
|
func ReadSecretFile(file string, defaultValue string) (string) {
|
|
|
|
if file == "" {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2021-06-20 19:47:55 +02:00
|
|
|
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-22 11:27:53 +02:00
|
|
|
Projects []Project
|
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-24 10:36:52 +02:00
|
|
|
if len(fullConfig.Projects) == 0 {
|
|
|
|
panic("Invalid configuration. At least one project mapping is necessary.")
|
|
|
|
}
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
Gitea = fullConfig.Gitea
|
2021-06-20 19:47:55 +02:00
|
|
|
SonarQube = fullConfig.SonarQube
|
2021-06-22 11:27:53 +02:00
|
|
|
Projects = fullConfig.Projects
|
2021-06-20 19:47:55 +02:00
|
|
|
|
2021-06-21 11:30:02 +02:00
|
|
|
Gitea.Webhook.Secret = ReadSecretFile(Gitea.Webhook.SecretFile, Gitea.Webhook.Secret)
|
|
|
|
Gitea.Token.Value = ReadSecretFile(Gitea.Token.File, Gitea.Token.Value)
|
|
|
|
SonarQube.Webhook.Secret = ReadSecretFile(SonarQube.Webhook.SecretFile, SonarQube.Webhook.Secret)
|
|
|
|
SonarQube.Token.Value = ReadSecretFile(SonarQube.Token.File, SonarQube.Token.Value)
|
2021-06-20 15:16:06 +02:00
|
|
|
}
|