-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
kind/bugSomething isn't workingSomething isn't working
Description
Preflight Checklist
- I have searched the issue tracker for an issue that matches the one I want to file, without success.
- I am not looking for support or already pursued the available support channels without success.
- I have checked the troubleshooting guide for my problem, without success.
Viper Version
1.11.0
Go Version
1.17
Config Source
Flags, Environment variables
Format
No response
Repl.it link
No response
Code reproducing the issue
package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type Config struct {
AFlag string `mapstructure:"A_FLAG"`
}
// rootCmd is the root cobra command of ormos-web
var rootCmd = &cobra.Command{
Use: "myapp",
Short: "My app",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("running")
},
}
func InitConfig(cmd *cobra.Command) {
var conf Config
v := viper.New()
v.AddConfigPath(".")
v.SetConfigName(".env")
v.SetConfigType("env")
v.SetEnvPrefix("FOO")
v.AllowEmptyEnv(true)
v.AutomaticEnv()
v.BindPFlags(cmd.Flags())
v.Unmarshal(&conf)
fmt.Printf("%+v\n", conf)
}
func main() {
rootCmd.PersistentFlags().StringP("a-flag", "", "", "a flag")
InitConfig(rootCmd)
rootCmd.Execute()
}Expected Behavior
after
export FOO_A_FLAG=test
or either running as
go run main.go --a-flag=test
the program should be printing
{AFlag:test}
running
Actual Behavior
the program just prints
{AFlag:}
running
Steps To Reproduce
No response
Additional Information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
kind/bugSomething isn't workingSomething isn't working