fix config home dir help text (#907)

## Type of change

- [x] 🐛 Bugfix

## Issue(s)

* #718

## Test Plan

- [x] 💪 Manual
This commit is contained in:
Keepers 2022-09-20 12:19:25 -06:00 committed by GitHub
parent acf1837e77
commit e8473e3c78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 15 deletions

View File

@ -30,9 +30,11 @@ const (
)
var (
configFilePath string
configDir string
defaultDir string
configFilePath string
configFilePathFlag string
configDir string
defaultDir string
displayDefaultFP = filepath.Join("$HOME", ".corso.toml")
)
// Attempts to set the default dir and config file path.
@ -64,13 +66,11 @@ func init() {
// adds the persistent flag --config-file to the provided command.
func AddConfigFlags(cmd *cobra.Command) {
fs := cmd.PersistentFlags()
def := configFilePath
if len(def) == 0 {
filepath.Join(defaultDir, ".corso.toml")
}
fs.StringVar(&configFilePath, "config-file", def, "config file location (default is "+defaultDir+".corso.toml)")
fs.StringVar(
&configFilePathFlag,
"config-file",
displayDefaultFP,
"config file location (default is $HOME/.corso.toml)")
}
// ---------------------------------------------------------------------------------------------------------
@ -81,7 +81,12 @@ func AddConfigFlags(cmd *cobra.Command) {
// verifies that the configuration was able to read a file.
func InitFunc() func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
err := initWithViper(GetViper(cmd.Context()), configFilePath)
fp := configFilePathFlag
if len(fp) == 0 || fp == displayDefaultFP {
fp = configFilePath
}
err := initWithViper(GetViper(cmd.Context()), fp)
if err != nil {
return err
}
@ -94,7 +99,7 @@ func InitFunc() func(*cobra.Command, []string) error {
// struct for testing.
func initWithViper(vpr *viper.Viper, configFP string) error {
// Configure default config file location
if configFP == "" {
if configFP == "" || configFP == displayDefaultFP {
// Find home directory.
_, err := os.Stat(configDir)
if err != nil {

View File

@ -170,8 +170,8 @@ func printFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
return
}
buf.WriteString("|Flag|Short|Default|Help\n")
buf.WriteString("|:----|:-----|:-------|:----\n")
buf.WriteString("|Flag|Short|Default|Help|\n")
buf.WriteString("|:----|:-----|:-------|:----|\n")
flags.VisitAll(func(flag *pflag.Flag) {
if flag.Hidden {
@ -194,6 +194,6 @@ func printFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
buf.WriteString("|")
buf.WriteString(flag.Usage)
buf.WriteString("\n")
buf.WriteString("|\n")
})
}