Use UserConfigDir instead of $HOME

go-homedir???! I be using go 1.13 now
This commit is contained in:
comp500 2019-09-19 01:22:59 +01:00
parent 44cd63923e
commit 79a62c735e
2 changed files with 10 additions and 8 deletions

View File

@ -3,10 +3,9 @@ package cmd
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -42,7 +41,13 @@ func init() {
rootCmd.PersistentFlags().StringVar(&modsFolder, "mods-folder", "mods", "The default folder to store mod metadata files in") rootCmd.PersistentFlags().StringVar(&modsFolder, "mods-folder", "mods", "The default folder to store mod metadata files in")
viper.BindPFlag("mods-folder", rootCmd.PersistentFlags().Lookup("mods-folder")) viper.BindPFlag("mods-folder", rootCmd.PersistentFlags().Lookup("mods-folder"))
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.packwiz.toml)") file, err := os.UserConfigDir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
file = filepath.Join(file, "packwiz", ".packwiz.toml")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "The config file to use (default \""+file+"\")")
} }
// initConfig reads in config file and ENV variables if set. // initConfig reads in config file and ENV variables if set.
@ -51,15 +56,13 @@ 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. dir, err := os.UserConfigDir()
home, err := homedir.Dir()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
// Search config in home directory with name ".packwiz" (without extension). viper.AddConfigPath(filepath.Join(dir, "packwiz"))
viper.AddConfigPath(home)
viper.SetConfigName(".packwiz") viper.SetConfigName(".packwiz")
} }

1
go.mod
View File

@ -12,7 +12,6 @@ require (
github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e // indirect github.com/golangplus/testing v0.0.0-20180327235837-af21d9c3145e // indirect
github.com/igorsobreira/titlecase v0.0.0-20140109233139-4156b5b858ac github.com/igorsobreira/titlecase v0.0.0-20140109233139-4156b5b858ac
github.com/mattn/go-isatty v0.0.4 // indirect github.com/mattn/go-isatty v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2 github.com/mitchellh/mapstructure v1.1.2
github.com/skratchdot/open-golang v0.0.0-20190402232053-79abb63cd66e github.com/skratchdot/open-golang v0.0.0-20190402232053-79abb63cd66e
github.com/spf13/cobra v0.0.5 github.com/spf13/cobra v0.0.5