config: give config questions default values - fixes #3672

This commit is contained in:
Nick Craig-Wood
2019-11-05 11:53:44 +00:00
parent 0eaf5475ef
commit 3f7af64316
4 changed files with 37 additions and 17 deletions

View File

@@ -638,11 +638,16 @@ func ReadNonEmptyLine(prompt string) string {
return result
}
// Command - choose one
func Command(commands []string) byte {
// CommandDefault - choose one. If return is pressed then it will
// chose the defaultIndex if it is >= 0
func CommandDefault(commands []string, defaultIndex int) byte {
opts := []string{}
for _, text := range commands {
fmt.Printf("%c) %s\n", text[0], text[1:])
for i, text := range commands {
def := ""
if i == defaultIndex {
def = " (default)"
}
fmt.Printf("%c) %s%s\n", text[0], text[1:], def)
opts = append(opts, text[:1])
}
optString := strings.Join(opts, "")
@@ -650,6 +655,9 @@ func Command(commands []string) byte {
for {
fmt.Printf("%s> ", optHelp)
result := strings.ToLower(ReadLine())
if len(result) == 0 && defaultIndex >= 0 {
return optString[defaultIndex]
}
if len(result) != 1 {
continue
}
@@ -660,11 +668,20 @@ func Command(commands []string) byte {
}
}
// Command - choose one
func Command(commands []string) byte {
return CommandDefault(commands, -1)
}
// Confirm asks the user for Yes or No and returns true or false
//
// If AutoConfirm is set, it will return true
func Confirm() bool {
return Command([]string{"yYes", "nNo"}) == 'y'
// If the user presses enter then the Default will be used
func Confirm(Default bool) bool {
defaultIndex := 0
if !Default {
defaultIndex = 1
}
return CommandDefault([]string{"yYes", "nNo"}, defaultIndex) == 'y'
}
// ConfirmWithConfig asks the user for Yes or No and returns true or
@@ -691,7 +708,7 @@ func ConfirmWithConfig(m configmap.Getter, configName string, Default bool) bool
fmt.Printf("Auto confirm is set: answering %s, override by setting config parameter %s=%v\n", answer, configName, !Default)
return Default
}
return Confirm()
return Confirm(Default)
}
// Choose one of the defaults or type a new string if newOk is set
@@ -800,7 +817,7 @@ func ShowRemote(name string) {
// OkRemote prints the contents of the remote and ask if it is OK
func OkRemote(name string) bool {
ShowRemote(name)
switch i := Command([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}); i {
switch i := CommandDefault([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}, 0); i {
case 'y':
return true
case 'e':
@@ -870,12 +887,14 @@ func ChooseOption(o *fs.Option, name string) string {
fmt.Println(o.Help)
if o.IsPassword {
actions := []string{"yYes type in my own password", "gGenerate random password"}
defaultAction := -1
if !o.Required {
defaultAction = len(actions)
actions = append(actions, "nNo leave this optional password blank")
}
var password string
var err error
switch i := Command(actions); i {
switch i := CommandDefault(actions, defaultAction); i {
case 'y':
password = ChangePassword("the")
case 'g':
@@ -890,7 +909,7 @@ func ChooseOption(o *fs.Option, name string) string {
fmt.Printf("Use this password? Please note that an obscured version of this \npassword (and not the " +
"password itself) will be stored under your \nconfiguration file, so keep this generated password " +
"in a safe place.\n")
if Confirm() {
if Confirm(true) {
break
}
}
@@ -1095,7 +1114,7 @@ func editOptions(ri *fs.RegInfo, name string, isNew bool) {
break
}
fmt.Printf("Edit advanced config? (y/n)\n")
if !Confirm() {
if !Confirm(false) {
break
}
}
@@ -1110,7 +1129,7 @@ func editOptions(ri *fs.RegInfo, name string, isNew bool) {
if !isNew {
fmt.Printf("Value %q = %q\n", option.Name, FileGet(name, option.Name))
fmt.Printf("Edit? (y/n)>\n")
if !Confirm() {
if !Confirm(false) {
continue
}
}