cmd: Make detection of $HOME work on all systems
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Use github.com/mitchellh/go-homedir Fix #430
authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 6bcf163 · Browse files · 3 files·+34 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−14 removed
- other:+34 added−14 removed
Provenance
- Commit
84cba62- Tree
65b9929- Author
Albert Nigmatzianov <albertnigma@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
6bcf163
3 files changed+34 added−14 removed
modified:cobra/cmd/add.go-1 removed
23 unmodified lines24 )25 26 func init() {27 addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")28- RootCmd.AddCommand(addCmd)28 }29 30 var parentName string31 135 unmodified linesmodified:cobra/cmd/init.go+16 added-10 removed
21 unmodified lines22 "github.com/spf13/cobra"23 "github.com/spf13/viper"24 )25 26-func init() {27- RootCmd.AddCommand(initCmd)28-}29-26 var initCmd = &cobra.Command{27 Use: "init [name]",28 Aliases: []string{"initialize", "initialise", "create"},29 Short: "Initialize a Cobra Application",97 unmodified lines127 import (128 "fmt"129 "os"130 131+ homedir "github.com/mitchellh/go-homedir"132 "github.com/spf13/cobra"133 {{if .viper}} "github.com/spf13/viper"{{end}}134 )135 18 unmodified lines154 // This is called by main.main(). It only needs to happen once to the rootCmd.155 func Execute() {156 if err := RootCmd.Execute(); err != nil {157 fmt.Println(err)161- os.Exit(-1)158+ os.Exit(1)159 }160 }161 162 func init() {163 {{if .viper}} cobra.OnInitialize(initConfig){{end}}164 165 // Here you will define your flags and configuration settings.169- // Cobra supports Persistent Flags, which, if defined here,166+ // Cobra supports persistent flags, which, if defined here,167 // will be global for your application.{{ if .viper }}168 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ else }}169 // RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.{{ .appName }}.yaml)"){{ end }}170 3 unmodified lines174 }{{ if .viper }}175 176 // initConfig reads in config file and ENV variables if set.177 func initConfig() {181- if cfgFile != "" { // enable ability to specify config file via flag178+ if cfgFile != "" {179+ // Use config file from the flag.180 viper.SetConfigFile(cfgFile)181 } else {184- viper.SetConfigName(".{{ .appName }}") // name of config file (without extension)185- viper.AddConfigPath(os.Getenv("HOME")) // adding home directory as first search path182+ // Find home directory.183+ home, err := homedir.Dir()184+ if err != nil {185+ fmt.Println(home)186+ os.Exit(1)187+ }188+189+ // Search config in home directory with name ".cobra" (without extension).190+ viper.AddConfigPath(home)191+ viper.SetConfigName(".cobra")192 }193 188- viper.AutomaticEnv() // read in environment variables that match194+ viper.AutomaticEnv() // read in environment variables that match195 196 // If a config file is found, read it in.197 if err := viper.ReadInConfig(); err == nil {198 fmt.Println("Using config file:", viper.ConfigFileUsed())26 unmodified linesmodified:cobra/cmd/root.go+18 added-3 removed
15 unmodified lines16 import (17 "fmt"18 "os"19 20+ homedir "github.com/mitchellh/go-homedir"21 "github.com/spf13/cobra"22 "github.com/spf13/viper"23 )24 9 unmodified lines34 35 func Execute() {36 if err := RootCmd.Execute(); err != nil {37 fmt.Println(err)37- os.Exit(-1)38+ os.Exit(1)39 }40 }41 42 func init() {43 initViper()44+45 RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cobra.yaml)")46 RootCmd.PersistentFlags().StringVarP(&projectBase, "projectbase", "b", "", "base project directory, e.g. github.com/spf13/")47 RootCmd.PersistentFlags().StringP("author", "a", "YOUR NAME", "Author name for copyright attribution")48 RootCmd.PersistentFlags().StringVarP(&userLicense, "license", "l", "", "Name of license for the project (can provide `license` in config)")2 unmodified lines51 viper.BindPFlag("projectbase", RootCmd.PersistentFlags().Lookup("projectbase"))52 viper.BindPFlag("useViper", RootCmd.PersistentFlags().Lookup("viper"))53 viper.SetDefault("author", "NAME HERE <EMAIL ADDRESS>")54 viper.SetDefault("license", "apache")55+56+ RootCmd.AddCommand(initCmd)57+ RootCmd.AddCommand(addCmd)58+59 }60 61 func initViper() {56- if cfgFile != "" { // enable ability to specify config file via flag62+ if cfgFile != "" {63+ // Use config file from the flag.64 viper.SetConfigFile(cfgFile)65 } else {66+ // Find home directory.67+ home, err := homedir.Dir()68+ if err != nil {69+ fmt.Println(home)70+ os.Exit(1)71+ }72+73+ // Search config in home directory with name ".cobra" (without extension).59- viper.AddConfigPath(os.Getenv("HOME"))74+ viper.AddConfigPath(home)75 viper.SetConfigName(".cobra")76 }77 78 viper.AutomaticEnv()5 unmodified lines