Skip to content

Commit

Permalink
Merge pull request #67 from emosbaugh/better-viper-bind
Browse files Browse the repository at this point in the history
Viper bind in pre run
  • Loading branch information
emosbaugh committed Jul 30, 2019
2 parents 652d98f + 2cca036 commit 1c2e9c2
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 29 deletions.
7 changes: 1 addition & 6 deletions pkg/cli/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ func Apply() *cobra.Command {
Short: "apply a spec to a database",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("driver", cmd.Flags().Lookup("driver"))
viper.BindPFlag("uri", cmd.Flags().Lookup("uri"))
viper.BindPFlag("spec-file", cmd.Flags().Lookup("spec-file"))
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
db := database.NewDatabase()
Expand All @@ -32,7 +29,5 @@ func Apply() *cobra.Command {
cmd.MarkFlagRequired("uri")
cmd.MarkFlagRequired("spec-file")

viper.BindPFlags(cmd.Flags())

return cmd
}
8 changes: 1 addition & 7 deletions pkg/cli/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ func Fixtures() *cobra.Command {
Short: "fixtures creates sql statements from a schemahero definition",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("driver", cmd.Flags().Lookup("driver"))
viper.BindPFlag("dbname", cmd.Flags().Lookup("dbname"))
viper.BindPFlag("input-dir", cmd.Flags().Lookup("input-dir"))
viper.BindPFlag("output-dir", cmd.Flags().Lookup("output-dir"))
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
db := database.NewDatabase()
Expand All @@ -35,7 +31,5 @@ func Fixtures() *cobra.Command {
cmd.MarkFlagRequired("input-dir")
cmd.MarkFlagRequired("output-dir")

viper.BindPFlags(cmd.Flags())

return cmd
}
9 changes: 1 addition & 8 deletions pkg/cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ func Generate() *cobra.Command {
Short: "generate schemahero custom resources from a running database instance",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("driver", cmd.Flags().Lookup("driver"))
viper.BindPFlag("uri", cmd.Flags().Lookup("uri"))
viper.BindPFlag("namespace", cmd.Flags().Lookup("namespace"))
viper.BindPFlag("dbname", cmd.Flags().Lookup("dbname"))
viper.BindPFlag("output-dir", cmd.Flags().Lookup("output-dir"))
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
g := generate.NewGenerator()
Expand All @@ -36,7 +31,5 @@ func Generate() *cobra.Command {
cmd.MarkFlagRequired("uri")
cmd.MarkFlagRequired("dbname")

viper.BindPFlags(cmd.Flags())

return cmd
}
10 changes: 2 additions & 8 deletions pkg/cli/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ func Watch() *cobra.Command {
Short: "watch a database for any changes",
Long: `...`,
PreRun: func(cmd *cobra.Command, args []string) {
// workaround for https://github.com/spf13/viper/issues/233
viper.BindPFlag("driver", cmd.Flags().Lookup("driver"))
viper.BindPFlag("uri", cmd.Flags().Lookup("uri"))
viper.BindPFlag("namespace", cmd.Flags().Lookup("namespace"))
viper.BindPFlag("instance", cmd.Flags().Lookup("instance"))
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
w := watcher.NewWatcher()
Expand All @@ -27,15 +23,13 @@ func Watch() *cobra.Command {

cmd.Flags().String("driver", "", "name of the database driver to use")
cmd.Flags().String("uri", "", "connection string uri")
cmd.Flags().String("namespace", "default", "namespace of the spwwning object")
cmd.Flags().String("namespace", "default", "namespace of the spawning object")
cmd.Flags().String("instance", "", "instance name of the spawning object")

cmd.MarkFlagRequired("driver")
cmd.MarkFlagRequired("uri")
cmd.MarkFlagRequired("namespace")
cmd.MarkFlagRequired("instance")

viper.BindPFlags(cmd.Flags())

return cmd
}

0 comments on commit 1c2e9c2

Please sign in to comment.