I noticed that this is perhaps a bug when viper tries to unmarshall to struct. To explain it better, consider this:
I have a cli command like below
dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2
Here is my command line source code
package cmd
import (
"fmt"
"github.com/spf13/viper"
"github.com/spf13/cobra"
)
// SubmitBugOpts is a set of flags being exposed by this Deploy command
type SubmitBugOpts struct {
Name string `mapstructure:"bug-name"`
ReasonTags []string `mapstructure:"tags"`
}
var (
submitBugOpts = SubmitBugOpts{}
)
func submitBugRun(cmd *cobra.Command, args []string) {
fmt.Printf("Bug Name is %+v\n", submitBugOpts.Name)
fmt.Printf("List of tags is %+v\n", submitBugOpts.ReasonTags)
fmt.Printf("Length of tags is %d\n", len(submitBugOpts.ReasonTags))
for index, el := range submitBugOpts.ReasonTags {
fmt.Printf("tag[%d] = %s\n", index, el)
}
}
var submitBugCmd = &cobra.Command{
Use: "submit-bug",
Short: "Deploy/Install a helm chart to Kubernetes cluster",
Run: submitBugRun,
PreRun: func(cmd *cobra.Command, args []string) {
pFlags := cmd.PersistentFlags()
viper.BindPFlag("bug-name", pFlags.Lookup("name"))
viper.BindPFlag("tags", pFlags.Lookup("tag"))
fmt.Printf("Viper all setting value: %+v\n", viper.AllSettings())
fmt.Printf("Before unmarshall: %+v\n", submitBugOpts)
viper.Unmarshal(&submitBugOpts)
fmt.Printf("After unmarshall: %+v\n", submitBugOpts)
},
}
func init() {
rootCmd.AddCommand(submitBugCmd)
pFlags := submitBugCmd.PersistentFlags()
pFlags.StringVar(&submitBugOpts.Name, "name", "", "the bug name")
pFlags.StringArrayVar(&submitBugOpts.ReasonTags, "tag", nil, "the bug's reason tag. You can define it multiple times")
submitBugCmd.MarkPersistentFlagRequired("name")
submitBugCmd.MarkPersistentFlagRequired("tag")
}
I run this command:
dd-cli submit-bug --name "Bug 1" --tag reason1 --tag reason2
And the output is below
Viper all setting value: map[bug-name:Bug 1 tags:[reason1,reason2]]
Before unmarshall: {Name:Bug 1 ReasonTags:[reason1 reason2]}
After unmarshall: {Name:Bug 1 ReasonTags:[[reason1 reason2]]}
Bug Name is Bug 1
List of tags is [[reason1 reason2]]
Length of tags is 2
tag[0] = [reason1
tag[1] = reason2]
I expect the viper.Unmarshall()
will correctly omit the [
for submitBugOpts.ReasonTags [0]
and omit the ]
for submitBugOpts.ReasonTags[1]
. So the expected value of submitBugOpts.ReasonTags doesn't contains any [
and ]
.
Any pointer how to fix this? I've submitted this issue on viper repo: https://github.com/spf13/viper/issues/527. However I am asking on SO just in case you guys know how to handle this too.