gitea-sonarqube-bot/internal/settings/settings.go
justusbunsi 86ea377d64
Load gitea configuration from file
Signed-off-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com>
2021-06-20 16:49:12 +02:00

49 lines
798 B
Go

package settings
import (
"fmt"
"github.com/spf13/viper"
)
type GiteaRepository struct {
Owner string
Name string
}
type GiteaConfig struct {
Url string
Token string
WebhookSecret string `mapstructure:"webhookSecret"`
Repositories []GiteaRepository
}
var (
Gitea GiteaConfig
)
func Load(configPath string) {
viper.SetConfigName("config.yaml")
viper.SetConfigType("yaml")
viper.AddConfigPath(configPath)
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error while reading config file: %w \n", err))
}
var giteaConfig GiteaConfig
if viper.Sub("gitea") == nil {
panic("Gitea not configured")
}
err = viper.UnmarshalKey("gitea", &giteaConfig)
if err != nil {
panic(fmt.Errorf("Unable to decode into struct, %v", err))
}
Gitea = giteaConfig
}