Skip to content

Commit

Permalink
drop mitchellh/go-homedir (spf13#853), print to stderr in boilerplate (
Browse files Browse the repository at this point in the history
…spf13#774)

fix: root.go.golden and tpl/main.go
  • Loading branch information
umarcor committed Aug 29, 2020
1 parent e9c2d94 commit 467789a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -194,7 +194,6 @@ import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -245,7 +244,7 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
home, err := os.UserHomeDir()
if err != nil {
er(err)
}
Expand All @@ -254,7 +253,6 @@ func initConfig() {
viper.AddConfigPath(home)
viper.SetConfigName(".cobra")
}

viper.AutomaticEnv()

if err := viper.ReadInConfig(); err == nil {
Expand Down
4 changes: 2 additions & 2 deletions cobra/cmd/root.go
Expand Up @@ -15,8 +15,8 @@ package cmd

import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -62,7 +62,7 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
home, err := os.UserHomeDir()
if err != nil {
er(err)
}
Expand Down
13 changes: 6 additions & 7 deletions cobra/cmd/testdata/root.go.golden
Expand Up @@ -17,10 +17,9 @@ package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
"github.com/spf13/cobra"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)

Expand All @@ -38,14 +37,14 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
// Run: func(cmd *cobra.Command, args []string) { },
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand All @@ -71,9 +70,9 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

Expand All @@ -86,6 +85,6 @@ func initConfig() {

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
16 changes: 7 additions & 9 deletions cobra/tpl/main.go
Expand Up @@ -24,12 +24,10 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
"os"
"github.com/spf13/cobra"
{{ if .Viper }}
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
{{ end -}}
"github.com/spf13/viper"{{ end }}
)
{{ if .Viper -}}
Expand All @@ -48,14 +46,14 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.` + "`" + `,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
Expand Down Expand Up @@ -85,9 +83,9 @@ func initConfig() {
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
home, err := os.UserHomeDir()
if err != nil {
fmt.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand All @@ -100,7 +98,7 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
{{- end }}
Expand Down

0 comments on commit 467789a

Please sign in to comment.