Skip to content

Commit

Permalink
Merge pull request #10 from thecasualcoder/default-cfgPath
Browse files Browse the repository at this point in the history
Set default configuration path
  • Loading branch information
aswinkarthik authored Oct 14, 2019
2 parents e42f26c + 48a3635 commit 2bb8c93
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cmd
import (
"fmt"
"os"
"path"

homedir "github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -27,22 +28,18 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kube-tmuxp.yaml)")
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
configFileName := ".kube-tmuxp.yaml"
cfgFile = path.Join(home, configFileName)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", cfgFile, "config file")
}

func initConfig() {
if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

viper.AddConfigPath(home)
viper.SetConfigName(".kube-tmuxp")
}
viper.SetConfigFile(cfgFile)

viper.AutomaticEnv()

Expand Down

0 comments on commit 2bb8c93

Please sign in to comment.