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

@ -63,6 +63,7 @@ func init() {
Name: "password", Name: "password",
Help: "Password or pass phrase for encryption.", Help: "Password or pass phrase for encryption.",
IsPassword: true, IsPassword: true,
Required: true,
}, { }, {
Name: "password2", Name: "password2",
Help: "Password or pass phrase for salt. Optional but recommended.\nShould be different to the previous password.", Help: "Password or pass phrase for salt. Optional but recommended.\nShould be different to the previous password.",

View File

@ -830,7 +830,7 @@ func configTeamDrive(ctx context.Context, opt *Options, m configmap.Mapper, name
} else { } else {
fmt.Printf("Change current team drive ID %q?\n", opt.TeamDriveID) fmt.Printf("Change current team drive ID %q?\n", opt.TeamDriveID)
} }
if !config.Confirm() { if !config.Confirm(false) {
return nil return nil
} }
client, err := createOAuthClient(opt, name, m) client, err := createOAuthClient(opt, name, m)

View File

@ -84,14 +84,14 @@ func init() {
tokenString, ok := m.Get("token") tokenString, ok := m.Get("token")
if ok && tokenString != "" { if ok && tokenString != "" {
fmt.Printf("Already have a token - refresh?\n") fmt.Printf("Already have a token - refresh?\n")
if !config.Confirm() { if !config.Confirm(false) {
return return
} }
} }
srv := rest.NewClient(fshttp.NewClient(fs.Config)) 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") 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) deviceRegistration, err := registerDevice(ctx, srv)
if err != nil { if err != nil {
log.Fatalf("Failed to register device: %v", err) 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") 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) oAuthClient, _, err := oauthutil.NewClient(name, m, oauthConfig)
if err != nil { if err != nil {
log.Fatalf("Failed to load oAuthClient: %s", err) log.Fatalf("Failed to load oAuthClient: %s", err)

View File

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