package opt import ( "fmt" "github.com/loveuer/nf/nft/log" "github.com/spf13/viper" "ultone/internal/tool" ) type listen struct { Http string `mapstructure:"http"` Grpc string `mapstructure:"grpc"` } type db struct { Type string `mapstructure:"-"` // postgres, mysql, sqlite Uri string `mapstructure:"uri"` } type cache struct { Uri string `mapstructure:"uri"` } type es struct { Uri string `mapstructure:"uri"` Index struct { Staff string `mapstructure:"staff"` } `mapstructure:"index"` } type Nebula struct { Uri string `mapstructure:"uri"` Username string `mapstructure:"username"` Password string `mapstructure:"password"` Space string `mapstructure:"space"` } type config struct { Name string `mapstructure:"name"` Debug bool `mapstructure:"-"` Listen listen `mapstructure:"listen"` DB db `mapstructure:"db"` Cache cache `mapstructure:"cache"` ES es `mapstructure:"es"` Nebula Nebula `mapstructure:"nebula"` } var ( Cfg = &config{} ) func Init(filename string) error { var ( err error ) log.Info("opt.Init: start reading config file: %s", filename) viper.SetConfigFile(filename) if err = viper.ReadInConfig(); err != nil { return fmt.Errorf("opt.Init: readin config failed, err = %s", err.Error()) } if err = viper.Unmarshal(Cfg); err != nil { return fmt.Errorf("opt.Init: unmarshal config failed, err = %s", err.Error()) } tool.TablePrinter(Cfg) return nil }