nitter/internal/commands/config.go
2023-03-08 14:40:04 +01:00

27 lines
450 B
Go

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
}