themalkolm / venom

🐍 Make viper and cobra more venomous!
MIT License
2 stars 0 forks source link

Add gen-config command #1

Closed themalkolm closed 7 years ago

themalkolm commented 7 years ago

It would be nice to automatically generate config structure for any command e.g.

var RootCmd = &cobra.Command{
    Use:          "foobar",
...
func init() {
    //
    // You need to define flags as usual.
    //
    RootCmd.PersistentFlags().String("foo", "", "Some foonees must be set")
    RootCmd.PersistentFlags().String("foo-bar", "", "Some barness must be set")
...
$ foobar gen-config -o ./config.go
...
$ cat ./config.o
type Config struct {
    Foo    string `mapstructure:"foo"`
    FooBar string `mapstructure:"foo-bar"`
}

So kind of self-updating code. The tricky part is to how to start this process. This could be done simply by having a dummy struct in the beginning to make sure code compiles.

themalkolm commented 7 years ago

What if we generate all flags from a config struct instead?