nitter/internal/commands/config.go

28 lines
450 B
Go
Raw Normal View History

2023-03-08 13:40:04 +00:00
package commands
import (
"strings"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
func LoadConfig[T any](flags *pflag.FlagSet) (*T, error) {
var cfg T
vipr := viper.New()
vipr.SetEnvPrefix("nitter")
vipr.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
vipr.AutomaticEnv()
if err := vipr.BindPFlags(flags); err != nil {
return nil, err
}
if err := vipr.Unmarshal(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}