Skip to content
Myr

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

3 files+34 added−14 removed

modified:cobra/cmd/add.go-1 removed
23 unmodified lines
24)2526func init() {27 addCmd.Flags().StringVarP(&parentName, "parent", "p", "RootCmd", "name of parent command for this command")28 RootCmd.AddCommand(addCmd)28}2930var parentName string31
135 unmodified lines
modified:cobra/cmd/init.go+16 added-10 removed
21 unmodified lines
22 "github.com/spf13/cobra"23 "github.com/spf13/viper"24)2526func init() {27 RootCmd.AddCommand(initCmd)28}2926var initCmd = &cobra.Command{27 Use: "init [name]",28 Aliases: []string{"initialize", "initialise", "create"},29 Short: "Initialize a Cobra Application",
97 unmodified lines
127import (128 "fmt"129 "os"130131 homedir "github.com/mitchellh/go-homedir"132 "github.com/spf13/cobra"133{{if .viper}} "github.com/spf13/viper"{{end}}134)135
18 unmodified lines
154// This is called by main.main(). It only needs to happen once to the rootCmd.155func Execute() {156 if err := RootCmd.Execute(); err != nil {157 fmt.Println(err)161 os.Exit(-1)158 os.Exit(1)159 }160}161162func init() {163{{if .viper}} cobra.OnInitialize(initConfig){{end}}164165 // 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 lines
174}{{ if .viper }}175176// initConfig reads in config file and ENV variables if set.177func 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 }188189 // Search config in home directory with name ".cobra" (without extension).190 viper.AddConfigPath(home)191 viper.SetConfigName(".cobra")192 }193188 viper.AutomaticEnv() // read in environment variables that match194 viper.AutomaticEnv() // read in environment variables that match195196 // 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 lines
modified:cobra/cmd/root.go+18 added-3 removed
15 unmodified lines
16import (17 "fmt"18 "os"1920 homedir "github.com/mitchellh/go-homedir"21 "github.com/spf13/cobra"22 "github.com/spf13/viper"23)24
9 unmodified lines
3435func Execute() {36 if err := RootCmd.Execute(); err != nil {37 fmt.Println(err)37 os.Exit(-1)38 os.Exit(1)39 }40}4142func init() {43 initViper()4445 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 lines
51 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")5556 RootCmd.AddCommand(initCmd)57 RootCmd.AddCommand(addCmd)5859}6061func 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 }7273 // 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 }7778 viper.AutomaticEnv()
5 unmodified lines