Merge remote-tracking branch 'me/master' into environPR

# Conflicts:
#	cobra/cmd/init.go
#	cobra/cmd/testdata/root.go.golden
This commit is contained in:
Raphael Tiersch 2018-09-04 21:48:36 +02:00
commit c9cb21db45
2 changed files with 47 additions and 33 deletions

47
cobra/cmd/init.go Normal file → Executable file
View file

@ -142,17 +142,26 @@ package cmd
import (
"fmt"
"os"
{{if .viper}}
homedir "github.com/mitchellh/go-homedir"{{end}}
"github.com/spf13/cobra"{{if .viper}}
"github.com/spf13/viper"{{end}}
){{if .viper}}
var cfgFile string{{end}}
"github.com/Fjolnir-Dvorak/environ"
"github.com/spf13/cobra"{{if .viper}}
"github.com/spf13/viper"
"path/filepath"{{end}}
)
const (
VendorName = ""
ApplicationName = "{{.appName}}"
DefaultConfType = "yaml"
)
var ({{if .viper}}
cfgFile string{{end}}
Environ environ.Environ
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "{{.appName}}",
Use: ApplicationName,
Short: "A brief description of your application",
Long: ` + "`" + `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
@ -174,14 +183,20 @@ func Execute() {
}
}
func init() { {{- if .viper}}
func init() {
Environ = environ.New(VendorName, ApplicationName)
{{- if .viper}}
cobra.OnInitialize(initConfig)
{{end}}
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// 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)"){{ end }}
configFile := filepath.Join(Environ.VarConfigLocal(), ApplicationName + "." + DefaultConfType)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
"config file (default is " + configFile + ")"){{ else }}
// configFile := filepath.Join(Environ.VarConfigLocal(), ApplicationName + "." + DefaultConfType)
// RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
// "config file (default is " + configFile + ")"){{ end }}
// Cobra also supports local flags, which will only run
// when this action is called directly.
@ -194,16 +209,10 @@ func initConfig() {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".{{ .appName }}" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".{{ .appName }}")
// Search config in Environ.ConfigLocal() directory with name "ApplicationName" (without extension).
viper.AddConfigPath(Environ.ConfigLocal())
viper.SetConfigName(ApplicationName)
}
viper.AutomaticEnv() // read in environment variables that match

View file

@ -18,16 +18,25 @@ import (
"fmt"
"os"
homedir "github.com/mitchellh/go-homedir"
"github.com/Fjolnir-Dvorak/environ"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"path/filepath"
)
var cfgFile string
const (
VendorName = ""
ApplicationName = "testproject"
DefaultConfType = "yaml"
)
var (
cfgFile string
Environ environ.Environ
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "testproject",
Use: ApplicationName,
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
@ -49,13 +58,15 @@ func Execute() {
}
}
func init() {
func init() {Environ = environ.New(VendorName, ApplicationName)
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.testproject.yaml)")
configFile := filepath.Join(Environ.VarConfigLocal(), ApplicationName + "." + DefaultConfType)
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "",
"config file (default is " + configFile + ")")
// Cobra also supports local flags, which will only run
// when this action is called directly.
@ -68,16 +79,10 @@ func initConfig() {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".testproject" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".testproject")
// Search config in Environ.ConfigLocal() directory with name "ApplicationName" (without extension).
viper.AddConfigPath(Environ.ConfigLocal())
viper.SetConfigName(ApplicationName)
}
viper.AutomaticEnv() // read in environment variables that match