An alternative way of decoupling?
mdouchement opened this issue · 0 comments
mdouchement commented
I like your idea of decoupling Cobra from Viper because I use a lot Cobra but I never use Viper which brings lots of dependencies.
But I feel bad to use a fork of an active project so I want to propose an alternative that can do the job in the Cobra projet.
The idea is to use a Viper interface inside Cobra to avoid coupling:
type Viper interface {
AddConfigPath(string)
BindPFlag(string, *pflag.Flag) error
ConfigFileUsed() string
GetBool(string) bool
GetString(string) string
IsSet(string) bool
Set(string, interface{})
SetConfigFile(string)
SetConfigName(string)
SetConfigType(string)
SetDefault(string, interface{})
ReadInConfig() error
}
var viper Viper
func Register(v Viper) {
viper = v
}
The Cobra's code using Viper can add a condition to check if the variable is not nil like in the following example:
if viper != nil && viper.GetBool("useViper") {
cobra.CheckErr(goGet("github.com/spf13/viper"))
}
Then developpers who want to use Viper can define the following statement in their programs:
func main() {
cobra.Register(viper.GetViper())
}