Skip to content

Commit

Permalink
Merge pull request #50 from asteris-llc/fix/viper-error
Browse files Browse the repository at this point in the history
don't ignore the error when Viper binds to PFlags
  • Loading branch information
BrianHicks committed Jun 7, 2016
2 parents 3c1a43b + 037e5de commit bea9c71
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
3 changes: 1 addition & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// applyCmd represents the plan command
Expand Down Expand Up @@ -109,7 +108,7 @@ var applyCmd = &cobra.Command{

func init() {
addParamsArguments(applyCmd.Flags())
viper.BindPFlags(applyCmd.Flags())
viperBindPFlags(applyCmd.Flags())

RootCmd.AddCommand(applyCmd)
}
3 changes: 1 addition & 2 deletions cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// planCmd represents the plan command
Expand Down Expand Up @@ -88,7 +87,7 @@ var planCmd = &cobra.Command{

func init() {
addParamsArguments(planCmd.Flags())
viper.BindPFlags(planCmd.Flags())
viperBindPFlags(planCmd.Flags())

RootCmd.AddCommand(planCmd)
}
5 changes: 1 addition & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"

"github.com/Sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -54,9 +53,7 @@ func init() {

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.converge.yaml)")

if err := viper.BindPFlags(RootCmd.PersistentFlags()); err != nil {
logrus.WithError(err).Fatal("could not bind flags")
}
viperBindPFlags(RootCmd.PersistentFlags())
}

// initConfig reads in config file and ENV variables if set.
Expand Down
28 changes: 28 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)

// bind a set of PFlags to Viper, failing and exiting on error
func viperBindPFlags(flags *pflag.FlagSet) {
if err := viper.BindPFlags(flags); err != nil {
logrus.WithError(err).Fatal("could not bind flags")
}
}

0 comments on commit bea9c71

Please sign in to comment.