gogstea/config/config.go

78 lines
1.4 KiB
Go

package config
import (
"flag"
"path"
"runtime"
"github.com/go-playground/validator/v10"
"github.com/jinzhu/configor"
"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"`
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"`
Email struct {
Host string
From string
User string
Password string
Port uint
}
}
// 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]
}
// Get root path
_, filename, _, _ := runtime.Caller(0)
var config Config
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
}
return config, nil
}
func NewZapCfg(dev bool) zap.Config {
prodConfig := zap.NewProductionConfig()
if dev {
prodConfig = zap.NewDevelopmentConfig()
}
return prodConfig
}