From 3f7af643166e3d8ef4739fe8a18f64c14a057312 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 5 Nov 2019 11:53:44 +0000 Subject: [PATCH] config: give config questions default values - fixes #3672 --- backend/crypt/crypt.go | 1 + backend/drive/drive.go | 2 +- backend/jottacloud/jottacloud.go | 6 ++--- fs/config/config.go | 45 +++++++++++++++++++++++--------- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/backend/crypt/crypt.go b/backend/crypt/crypt.go index ae92e22ed..f7f0f7e46 100644 --- a/backend/crypt/crypt.go +++ b/backend/crypt/crypt.go @@ -63,6 +63,7 @@ func init() { Name: "password", Help: "Password or pass phrase for encryption.", IsPassword: true, + Required: true, }, { Name: "password2", Help: "Password or pass phrase for salt. Optional but recommended.\nShould be different to the previous password.", diff --git a/backend/drive/drive.go b/backend/drive/drive.go index ffdb874c3..5e55ba3a3 100644 --- a/backend/drive/drive.go +++ b/backend/drive/drive.go @@ -830,7 +830,7 @@ func configTeamDrive(ctx context.Context, opt *Options, m configmap.Mapper, name } else { fmt.Printf("Change current team drive ID %q?\n", opt.TeamDriveID) } - if !config.Confirm() { + if !config.Confirm(false) { return nil } client, err := createOAuthClient(opt, name, m) diff --git a/backend/jottacloud/jottacloud.go b/backend/jottacloud/jottacloud.go index 36e18ebfa..07aef1042 100644 --- a/backend/jottacloud/jottacloud.go +++ b/backend/jottacloud/jottacloud.go @@ -84,14 +84,14 @@ func init() { tokenString, ok := m.Get("token") if ok && tokenString != "" { fmt.Printf("Already have a token - refresh?\n") - if !config.Confirm() { + if !config.Confirm(false) { return } } srv := rest.NewClient(fshttp.NewClient(fs.Config)) fmt.Printf("\nDo you want to create a machine specific API key?\n\nRclone has it's own Jottacloud API KEY which works fine as long as one only uses rclone on a single machine. When you want to use rclone with this account on more than one machine it's recommended to create a machine specific API key. These keys can NOT be shared between machines.\n\n") - if config.Confirm() { + if config.Confirm(false) { deviceRegistration, err := registerDevice(ctx, srv) if err != nil { log.Fatalf("Failed to register device: %v", err) @@ -127,7 +127,7 @@ func init() { } fmt.Printf("\nDo you want to use a non standard device/mountpoint e.g. for accessing files uploaded using the official Jottacloud client?\n\n") - if config.Confirm() { + if config.Confirm(false) { oAuthClient, _, err := oauthutil.NewClient(name, m, oauthConfig) if err != nil { log.Fatalf("Failed to load oAuthClient: %s", err) diff --git a/fs/config/config.go b/fs/config/config.go index 2c2cd08f6..cfceeb2a0 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -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 } }