mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Added/Corrected golden file test for creating a project named testproject with viper active
This commit is contained in:
parent
ad974c59c7
commit
b1f355e936
1 changed files with 21 additions and 14 deletions
35
cobra/cmd/testdata/root.go.golden
vendored
35
cobra/cmd/testdata/root.go.golden
vendored
|
@ -18,16 +18,25 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
homedir "github.com/mitchellh/go-homedir"
|
"github.com/Fjolnir-Dvorak/environ"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"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
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "testproject",
|
Use: ApplicationName,
|
||||||
Short: "A brief description of your application",
|
Short: "A brief description of your application",
|
||||||
Long: `A longer description that spans multiple lines and likely contains
|
Long: `A longer description that spans multiple lines and likely contains
|
||||||
examples and usage of using your application. For example:
|
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)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
// 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.
|
// 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
|
// Cobra also supports local flags, which will only run
|
||||||
// when this action is called directly.
|
// when this action is called directly.
|
||||||
|
@ -68,16 +81,10 @@ func initConfig() {
|
||||||
// Use config file from the flag.
|
// Use config file from the flag.
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} 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).
|
// Search config in Environ.ConfigLocal() directory with name "ApplicationName" (without extension).
|
||||||
viper.AddConfigPath(home)
|
viper.AddConfigPath(Environ.ConfigLocal())
|
||||||
viper.SetConfigName(".testproject")
|
viper.SetConfigName(ApplicationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
Loading…
Add table
Reference in a new issue