Skip to content

Commit aff4ea6

Browse files
author
Cornelius Weig
committed
Enable viper config files
1 parent 044105b commit aff4ea6

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

cmd/root.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"github.com/corneliusweig/ketall/pkg/options"
2121
"github.com/sirupsen/logrus"
2222
"io"
23+
"k8s.io/client-go/util/homedir"
2324
"path/filepath"
2425

2526
"github.com/corneliusweig/ketall/pkg"
26-
"github.com/mitchellh/go-homedir"
2727
"github.com/pkg/errors"
2828
"github.com/spf13/cobra"
2929
"github.com/spf13/viper"
@@ -83,6 +83,11 @@ func init() {
8383
ketallOptions.GenericCliFlags.AddFlags(rootCmd.Flags())
8484
ketallOptions.PrintFlags.AddFlags(rootCmd)
8585

86+
err := viper.BindPFlags(rootCmd.Flags())
87+
if err != nil {
88+
logrus.Errorf("Cannot bind flags: %s", err)
89+
}
90+
8691
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
8792
if err := SetUpLogs(ketallOptions.Streams.ErrOut, v); err != nil {
8893
return err
@@ -94,22 +99,17 @@ func init() {
9499
// initConfig reads in config file and ENV variables if set.
95100
func initConfig() {
96101
if ketallOptions.CfgFile != "" {
97-
// Use config file from the flag.
98102
viper.SetConfigFile(ketallOptions.CfgFile)
99103
} else {
100-
// Find home directory.
101-
home, err := homedir.Dir()
102-
if err != nil {
103-
logrus.Warnf("Could not read home dir: %s", err)
104-
return
105-
}
106-
107-
// Search config in "~/.kube/ketall" (without extension).
108-
viper.AddConfigPath(filepath.Join(home, ".kube"))
104+
// Search for "ketall.yaml" in "." and "~/.kube/"
105+
viper.AddConfigPath(".")
106+
viper.AddConfigPath(filepath.Join(homedir.HomeDir(), ".kube"))
109107
viper.SetConfigName("ketall")
110108
}
111109

112-
viper.AutomaticEnv() // read in environment variables that match
110+
// read in environment variables that match
111+
viper.SetEnvPrefix("ketall")
112+
viper.AutomaticEnv()
113113

114114
// If a config file is found, read it in.
115115
if err := viper.ReadInConfig(); err == nil {

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/imdario/mergo v0.3.7 // indirect
1313
github.com/inconshreveable/mousetrap v1.0.0 // indirect
1414
github.com/json-iterator/go v1.1.5 // indirect
15-
github.com/mitchellh/go-homedir v1.1.0
1615
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1716
github.com/modern-go/reflect2 v1.0.1 // indirect
1817
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
@@ -25,10 +24,10 @@ require (
2524
golang.org/x/oauth2 v0.0.0-20190212230446-3e8b2be13635 // indirect
2625
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
2726
gopkg.in/inf.v0 v0.9.1 // indirect
28-
k8s.io/api v0.0.0-20181121191454-a61488babbd6
27+
k8s.io/api v0.0.0-20181121191454-a61488babbd6 // indirect
2928
k8s.io/apimachinery v0.0.0-20190211022232-e355a776c090
3029
k8s.io/cli-runtime v0.0.0-20190202014047-491c94071cfa
31-
k8s.io/client-go v10.0.0+incompatible // indirect
30+
k8s.io/client-go v10.0.0+incompatible
3231
k8s.io/klog v0.2.0 // indirect
3332
sigs.k8s.io/yaml v1.1.0 // indirect
3433
)

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGi
3737
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
3838
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
3939
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
40-
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
41-
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
4240
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
4341
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
4442
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=

0 commit comments

Comments
 (0)