jenkins-cron/v3/config.go
“xHuPo” 55f3e2b3c6 add v3
2025-05-22 11:43:23 +08:00

48 lines
1.2 KiB
Go

package main
import (
"log"
"github.com/spf13/viper"
)
type Gradle struct {
Caches []string `mapstructure:"caches"`
}
type Jenkins struct {
Schema string `mapstructure:"schema"`
URL string `mapstructure:"url"`
User string `mapstructure:"user"`
Token string `mapstructure:"token"`
Number string `mapstructure:"number"`
DefaultParameters []map[string]interface{} `mapstructure:"default_parameters"`
SpecialParameters []map[string]interface{} `mapstructure:"special_parameters"`
}
type Config struct {
Gradle Gradle
Jenkins Jenkins
Jobs []string `mapstructure:"jobs"`
Retries int `mapstructure:"retries"`
}
func LoadConfig(path string) (*Config, error) {
v := viper.New()
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(path)
if err := v.ReadInConfig(); err != nil {
log.Fatal("[config] Error reading config file:", err)
return nil, err
}
var config Config
if err := v.Unmarshal(&config); err != nil {
log.Fatal("[config] Unable to decode config into struct:", err)
return nil, err
}
return &config, nil
}