Files
tg_exporter/config/config.go
2024-09-04 13:50:11 +08:00

51 lines
1.3 KiB
Go

package config
import (
"path/filepath"
"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"`
Output string `mapstructure:"output"`
PublishHost string `mapstructure:"publish_host"`
PublishToken string `mapstructure:"publish_token"`
}
// GetMaxSize
func (c *Config) GetMaxSize() uint {
// parse 50mb to 50 * 1024 * 1024
return viper.GetSizeInBytes("max_size")
}
func Load(path string) error {
path, err := filepath.Abs(path)
if err != nil {
return errors.Wrapf(err, "failed to get the absolute path of the config file %s", path)
}
// 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
}