cmd: Make detection of $HOME work on all systems

Use github.com/mitchellh/go-homedir

Fix #430
This commit is contained in:
Albert Nigmatzianov 2017-05-04 22:28:09 +02:00
parent 6bcf163261
commit 84cba621a0
3 changed files with 34 additions and 14 deletions

View file

@ -25,7 +25,6 @@ import (
func init() { func init() {
addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command") addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")
RootCmd.AddCommand(addCmd)
} }
var parentName string var parentName string

View file

@ -23,10 +23,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func init() {
RootCmd.AddCommand(initCmd)
}
var initCmd = &cobra.Command{ var initCmd = &cobra.Command{
Use: "init [name]", Use: "init [name]",
Aliases: []string{"initialize", "initialise", "create"}, Aliases: []string{"initialize", "initialise", "create"},
@ -132,6 +128,7 @@ import (
"fmt" "fmt"
"os" "os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
{{if .viper}} "github.com/spf13/viper"{{end}} {{if .viper}} "github.com/spf13/viper"{{end}}
) )
@ -158,7 +155,7 @@ to quickly create a Cobra application.` + "`" + `,
func Execute() { func Execute() {
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(-1) os.Exit(1)
} }
} }
@ -166,7 +163,7 @@ func init() {
{{if .viper}} cobra.OnInitialize(initConfig){{end}} {{if .viper}} cobra.OnInitialize(initConfig){{end}}
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application.{{ if .viper }} // will be global for your application.{{ if .viper }}
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }} RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }} // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}
@ -178,11 +175,20 @@ func init() {
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
func initConfig() { func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
viper.SetConfigName(".{{ .appName }}") // name of config file (without extension) // Find home directory.
viper.AddConfigPath(os.Getenv("HOME")) // adding home directory as first search path home, err := homedir.Dir()
if err != nil {
fmt.Println(home)
os.Exit(1)
}
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cobra")
} }
viper.AutomaticEnv() // read in environment variables that match viper.AutomaticEnv() // read in environment variables that match

View file

@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"os" "os"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -34,12 +35,13 @@ to quickly create a Cobra application.`,
func Execute() { func Execute() {
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(-1) os.Exit(1)
} }
} }
func init() { func init() {
initViper() initViper()
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)") RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")
RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/") RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/")
RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution") RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")
@ -50,13 +52,26 @@ func init() {
viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper")) viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))
viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>") viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")
viper.SetDefault("license", "apache") viper.SetDefault("license", "apache")
RootCmd.AddCommand(initCmd)
RootCmd.AddCommand(addCmd)
} }
func initViper() { func initViper() {
if cfgFile != "" { // enable ability to specify config file via flag if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile) viper.SetConfigFile(cfgFile)
} else { } else {
viper.AddConfigPath(os.Getenv("HOME")) // Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(home)
os.Exit(1)
}
// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".cobra") viper.SetConfigName(".cobra")
} }