Skip to content

Commit

Permalink
fix(viper): config flags binding
Browse files Browse the repository at this point in the history
  • Loading branch information
folago committed May 2, 2024
1 parent 28b1e2d commit 7bdba0f
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ var RootCmd = &cobra.Command{
Short: "Send an email using Cycloid config",
Long: "Send an email using Cycloid config file in order to test different SMTP servers integration",
RunE: func(cmd *cobra.Command, _ []string) error {
viper.SetConfigFile(viper.GetString(cfg))
viper.SetConfigFile(cfgFlag)
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("error reading config file: %w", err)
}
fmt.Printf("viper: %+v\n", viper.GetString(server))
cfg := getConfig()
fmt.Printf("CONFIG: %+v\n", cfg)
err = sendEmail(cfg)
Expand Down Expand Up @@ -95,7 +96,7 @@ type config struct {
}

func getConfig() config {
return config{
ret := config{
cfgFile: cfgFlag,
skiptls: skiptlsFlag,
server: serverFlag,
Expand All @@ -104,6 +105,28 @@ func getConfig() config {
from: fromFlag,
to: toFlag,
}
if ret.cfgFile == "" {
ret.cfgFile = viper.GetString(cfg)
}
if ret.skiptls == false {
ret.skiptls = viper.GetBool(skiptls)
}
if ret.server == "" {
ret.server = viper.GetString(server)
}
if ret.username == "" {
ret.username = viper.GetString(username)
}
if ret.password == "" {
ret.password = viper.GetString(password)
}
if ret.from == "" {
ret.from = viper.GetString(from)
}
if ret.to == "" {
ret.to = viper.GetString(to)
}
return ret
}

func sendEmail(cfg config) error {
Expand Down

0 comments on commit 7bdba0f

Please sign in to comment.