From 4e773f9787d4cc4d6090ab264db0cafc064a651e Mon Sep 17 00:00:00 2001 From: Raphael Tiersch Date: Fri, 7 Jul 2017 22:22:22 +0200 Subject: [PATCH 1/4] Changed the default config location from to the system-directory for config --- cobra/cmd/init.go | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) mode change 100644 => 100755 cobra/cmd/init.go diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go old mode 100644 new mode 100755 index 149aabe1..eaa3932b --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -142,17 +142,27 @@ package cmd import ( "fmt" "os" -{{if .viper}} - homedir "github.com/mitchellh/go-homedir"{{end}} + + "github.com/Fjolnir-Dvorak/environ"{{if .viper}} "github.com/spf13/cobra"{{if .viper}} - "github.com/spf13/viper"{{end}} + "github.com/spf13/viper" + "path/filepath"{{end}} ){{if .viper}} -var cfgFile string{{end}} +const ( + VendorName = "" + ApplicationName = "{{.appName}}" + DefaultConfType = "yaml" +) +{{end}} +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 +184,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 +210,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 From c36ef206d18175ffda420da034618fed690267eb Mon Sep 17 00:00:00 2001 From: Raphael Tiersch Date: Mon, 31 Jul 2017 16:36:49 +0200 Subject: [PATCH 2/4] There was an error where I messed up with {{ if .viper}}. --- cobra/cmd/init.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index eaa3932b..ceb50b3f 100755 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -143,18 +143,17 @@ import ( "fmt" "os" - "github.com/Fjolnir-Dvorak/environ"{{if .viper}} + "github.com/Fjolnir-Dvorak/environ" "github.com/spf13/cobra"{{if .viper}} "github.com/spf13/viper" "path/filepath"{{end}} -){{if .viper}} +) const ( VendorName = "" ApplicationName = "{{.appName}}" DefaultConfType = "yaml" ) -{{end}} var ({{if .viper}} cfgFile string{{end}} Environ environ.Environ From b1f355e9360958445002eabba4a3ad12e7f6c689 Mon Sep 17 00:00:00 2001 From: Raphael Tiersch Date: Mon, 25 Dec 2017 14:24:22 +0100 Subject: [PATCH 3/4] Added/Corrected golden file test for creating a project named testproject with viper active --- cobra/cmd/testdata/root.go.golden | 35 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cobra/cmd/testdata/root.go.golden b/cobra/cmd/testdata/root.go.golden index a76a7672..7bcfd676 100644 --- a/cobra/cmd/testdata/root.go.golden +++ b/cobra/cmd/testdata/root.go.golden @@ -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,17 @@ 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 +81,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 From 00605601416db92aa8b48f5372977c84ebd666a5 Mon Sep 17 00:00:00 2001 From: Raphael Tiersch Date: Mon, 25 Dec 2017 14:33:02 +0100 Subject: [PATCH 4/4] I had a whitespace to much in init.go:init() which shouldn't be there. Fixes failing CirleCI test --- cobra/cmd/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cobra/cmd/init.go b/cobra/cmd/init.go index db34bbb7..ec21090f 100755 --- a/cobra/cmd/init.go +++ b/cobra/cmd/init.go @@ -185,7 +185,7 @@ func Execute() { func init() { Environ = environ.New(VendorName, ApplicationName) - {{if .viper}} +{{if .viper}} cobra.OnInitialize(initConfig) {{end}} // Here you will define your flags and configuration settings.