2021-06-20 15:16:06 +02:00
|
|
|
package settings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type GiteaConfig struct {
|
|
|
|
Url string
|
|
|
|
Token string
|
|
|
|
WebhookSecret string `mapstructure:"webhookSecret"`
|
|
|
|
Repositories []GiteaRepository
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
Gitea GiteaConfig
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
if viper.IsSet("gitea") == false {
|
2021-06-20 16:49:12 +02:00
|
|
|
panic("Gitea not configured")
|
|
|
|
}
|
|
|
|
|
2021-06-20 18:24:12 +02:00
|
|
|
var fullConfig struct {
|
|
|
|
Gitea GiteaConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
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 15:16:06 +02:00
|
|
|
}
|