gogstea/config/config.go

78 lines
1.4 KiB
Go
Raw Normal View History

2024-08-30 13:45:18 +02:00
package config
import (
2025-02-19 18:47:11 +01:00
"flag"
"path"
"runtime"
2024-08-30 13:45:18 +02:00
2025-02-19 18:47:11 +01:00
"github.com/go-playground/validator/v10"
"github.com/jinzhu/configor"
2024-08-30 13:45:18 +02:00
"go.uber.org/zap"
)
type Config struct {
Gitea struct {
URL string `yaml:"url"`
ApiKey string `yaml:"api_key"`
} `yaml:"gitea"`
Gogs struct {
DSN string `yaml:"dsn"`
} `yaml:"gogs"`
2024-08-30 13:45:18 +02:00
Users struct {
DefaultPassword string `yaml:"default_password"`
CSVPath string `yaml:"csv_path"`
} `yaml:"users"`
Organizations struct {
CSVPath string `yaml:"csv_path"`
} `yaml:"organizations"`
Issues struct {
CSVPath string `yaml:"csv_path"`
} `yaml:"issues"`
Collaborators struct {
CSVPath string `yaml:"csv_path"`
} `yaml:"collaborators"`
2025-02-19 18:47:11 +01:00
Email struct {
Host string
From string
User string
Password string
Port uint
}
2024-08-30 13:45:18 +02:00
}
2025-02-19 18:47:11 +01:00
// LoadConfig
func LoadConfig(c ...string) (Config, error) {
cfg := "app.yml"
test := flag.Lookup("test.v") != nil
if test {
cfg = "app.test.yml"
}
if len(c) > 0 {
cfg = c[0]
2024-08-30 13:45:18 +02:00
}
2025-02-19 18:47:11 +01:00
// Get root path
_, filename, _, _ := runtime.Caller(0)
2024-08-30 13:45:18 +02:00
var config Config
2025-02-19 18:47:11 +01:00
if err := configor.Load(&config, path.Join(path.Dir(filename), cfg)); err != nil {
return config, err
}
v := validator.New()
if err := v.Struct(config); err != nil {
return config, err
2024-08-30 13:45:18 +02:00
}
2025-02-19 18:47:11 +01:00
return config, nil
2024-08-30 13:45:18 +02:00
}
func NewZapCfg(dev bool) zap.Config {
prodConfig := zap.NewProductionConfig()
if dev {
prodConfig = zap.NewDevelopmentConfig()
}
return prodConfig
}