Skip to content

Commit 2bb8c93

Browse files
authored
Merge pull request #10 from thecasualcoder/default-cfgPath
Set default configuration path
2 parents e42f26c + 48a3635 commit 2bb8c93

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

cmd/root.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"path"
67

7-
homedir "github.com/mitchellh/go-homedir"
8+
"github.com/mitchellh/go-homedir"
89
"github.com/spf13/cobra"
910
"github.com/spf13/viper"
1011
)
@@ -27,22 +28,18 @@ func Execute() {
2728

2829
func init() {
2930
cobra.OnInitialize(initConfig)
30-
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kube-tmuxp.yaml)")
31+
home, err := homedir.Dir()
32+
if err != nil {
33+
fmt.Println(err)
34+
os.Exit(1)
35+
}
36+
configFileName := ".kube-tmuxp.yaml"
37+
cfgFile = path.Join(home, configFileName)
38+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", cfgFile, "config file")
3139
}
3240

3341
func initConfig() {
34-
if cfgFile != "" {
35-
viper.SetConfigFile(cfgFile)
36-
} else {
37-
home, err := homedir.Dir()
38-
if err != nil {
39-
fmt.Println(err)
40-
os.Exit(1)
41-
}
42-
43-
viper.AddConfigPath(home)
44-
viper.SetConfigName(".kube-tmuxp")
45-
}
42+
viper.SetConfigFile(cfgFile)
4643

4744
viper.AutomaticEnv()
4845

0 commit comments

Comments
 (0)