42 lines
968 B
Go
42 lines
968 B
Go
package config
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var C *Config
|
|
|
|
type Config struct {
|
|
Phone string `mapstructure:"phone"`
|
|
AppID int `mapstructure:"app_id"`
|
|
AppHash string `mapstructure:"app_hash"`
|
|
BotToken string `mapstructure:"bot_token"`
|
|
SessionFile string `mapstructure:"session_file"`
|
|
LogFile string `mapstructure:"log_file"`
|
|
MaxSize string `mapstructure:"max_size"`
|
|
DSN string `mapstructure:"dsn"`
|
|
}
|
|
|
|
// GetMaxSize
|
|
func (c *Config) GetMaxSize() uint {
|
|
// parse 50m to 50 * 1024 * 1024
|
|
return viper.GetSizeInBytes(c.MaxSize)
|
|
}
|
|
|
|
func Load(path string) error {
|
|
// Load the config file
|
|
viper.SetConfigFile(path)
|
|
viper.AutomaticEnv()
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
return errors.Wrapf(err, "failed to read the config file %s", path)
|
|
}
|
|
|
|
if err := viper.Unmarshal(&C); err != nil {
|
|
return errors.Wrapf(err, "failed to unmarshal the config %s", path)
|
|
}
|
|
|
|
return nil
|
|
}
|