cmd/config: improve option prompt

See #5538
This commit is contained in:
albertony 2021-08-16 14:43:12 +02:00
parent e2f47ecdeb
commit 8b65c55711

View file

@ -96,7 +96,7 @@ func Choose(what string, defaults, help []string, newOk bool) string {
if newOk { if newOk {
valueDescription = "your own" valueDescription = "your own"
} }
fmt.Printf("Choose a number from below, or type in %s value\n", valueDescription) fmt.Printf("Choose a number from below, or type in %s value.\n", valueDescription)
attributes := []string{terminal.HiRedFg, terminal.HiGreenFg} attributes := []string{terminal.HiRedFg, terminal.HiGreenFg}
for i, text := range defaults { for i, text := range defaults {
var lines []string var lines []string
@ -277,7 +277,7 @@ func backendConfig(ctx context.Context, name string, m configmap.Mapper, ri *fs.
fmt.Println(out.Option.Help) fmt.Println(out.Option.Help)
in.Result = fmt.Sprint(Confirm(Default)) in.Result = fmt.Sprint(Confirm(Default))
} else { } else {
value := ChooseOption(out.Option, "") value := ChooseOption(out.Option)
if value != "" { if value != "" {
err := out.Option.Set(value) err := out.Option.Set(value)
if err != nil { if err != nil {
@ -316,15 +316,23 @@ func RemoteConfig(ctx context.Context, name string) error {
} }
// ChooseOption asks the user to choose an option // ChooseOption asks the user to choose an option
func ChooseOption(o *fs.Option, name string) string { func ChooseOption(o *fs.Option) string {
fmt.Println(o.Help) fmt.Printf("Option %s.\n", o.Name)
if o.Help != "" {
// Show help string without empty lines.
help := strings.Replace(strings.TrimSpace(o.Help), "\n\n", "\n", -1)
fmt.Println(help)
}
if o.IsPassword { if o.IsPassword {
fmt.Printf("Choose an alternative below.")
actions := []string{"yYes type in my own password", "gGenerate random password"} actions := []string{"yYes type in my own password", "gGenerate random password"}
defaultAction := -1 defaultAction := -1
if !o.Required { if !o.Required {
defaultAction = len(actions) defaultAction = len(actions)
actions = append(actions, "nNo leave this optional password blank") actions = append(actions, "nNo leave this optional password blank")
fmt.Printf(" Press Enter for the default (%s).", string(actions[defaultAction][0]))
} }
fmt.Println()
var password string var password string
var err error var err error
switch i := CommandDefault(actions, defaultAction); i { switch i := CommandDefault(actions, defaultAction); i {
@ -432,7 +440,7 @@ func NewRemote(ctx context.Context, name string) error {
// Set the type first // Set the type first
for { for {
newType = ChooseOption(fsOption(), name) newType = ChooseOption(fsOption())
ri, err = fs.Find(newType) ri, err = fs.Find(newType)
if err != nil { if err != nil {
fmt.Printf("Bad remote %q: %v\n", newType, err) fmt.Printf("Bad remote %q: %v\n", newType, err)