2021-03-11 17:20:31 +01:00
|
|
|
// Textual user interface parts of the config system
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2021-03-11 17:20:31 +01:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fs/config/configstruct"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/driveletter"
|
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
|
|
|
"golang.org/x/text/unicode/norm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadLine reads some input
|
|
|
|
var ReadLine = func() string {
|
|
|
|
buf := bufio.NewReader(os.Stdin)
|
|
|
|
line, err := buf.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read line: %v", err)
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(line)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadNonEmptyLine prints prompt and calls Readline until non empty
|
|
|
|
func ReadNonEmptyLine(prompt string) string {
|
|
|
|
result := ""
|
|
|
|
for result == "" {
|
|
|
|
fmt.Print(prompt)
|
|
|
|
result = strings.TrimSpace(ReadLine())
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommandDefault - choose one. If return is pressed then it will
|
|
|
|
// chose the defaultIndex if it is >= 0
|
2023-03-03 15:17:02 +01:00
|
|
|
//
|
|
|
|
// Must not call fs.Log anything from here to avoid deadlock in
|
|
|
|
// --interactive --progress
|
2021-03-11 17:20:31 +01:00
|
|
|
func CommandDefault(commands []string, defaultIndex int) byte {
|
|
|
|
opts := []string{}
|
|
|
|
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, "")
|
|
|
|
optHelp := strings.Join(opts, "/")
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", optHelp)
|
|
|
|
result := strings.ToLower(ReadLine())
|
2021-08-22 22:43:51 +02:00
|
|
|
if len(result) == 0 {
|
|
|
|
if defaultIndex >= 0 {
|
|
|
|
return optString[defaultIndex]
|
|
|
|
}
|
|
|
|
fmt.Printf("This value is required and it has no default.\n")
|
|
|
|
} else if len(result) == 1 {
|
|
|
|
i := strings.Index(optString, string(result[0]))
|
|
|
|
if i >= 0 {
|
|
|
|
return result[0]
|
|
|
|
}
|
|
|
|
fmt.Printf("This value must be one of the following characters: %s.\n", strings.Join(opts, ", "))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("This value must be a single character, one of the following: %s.\n", strings.Join(opts, ", "))
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 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'
|
|
|
|
}
|
|
|
|
|
2021-08-22 22:43:51 +02:00
|
|
|
// Choose one of the choices, or default, or type a new string if newOk is set
|
|
|
|
func Choose(what string, kind string, choices, help []string, defaultValue string, required bool, newOk bool) string {
|
2021-03-11 17:20:31 +01:00
|
|
|
valueDescription := "an existing"
|
|
|
|
if newOk {
|
|
|
|
valueDescription = "your own"
|
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
fmt.Printf("Choose a number from below, or type in %s %s.\n", valueDescription, kind)
|
2021-09-11 21:10:05 +02:00
|
|
|
// Empty input is allowed if not required is set, or if
|
|
|
|
// required is set but there is a default value to use.
|
|
|
|
if defaultValue != "" {
|
|
|
|
fmt.Printf("Press Enter for the default (%s).\n", defaultValue)
|
|
|
|
} else if !required {
|
|
|
|
fmt.Printf("Press Enter to leave empty.\n")
|
2021-08-22 22:43:51 +02:00
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
attributes := []string{terminal.HiRedFg, terminal.HiGreenFg}
|
2021-08-22 22:43:51 +02:00
|
|
|
for i, text := range choices {
|
2021-03-11 17:20:31 +01:00
|
|
|
var lines []string
|
2022-01-18 18:29:29 +01:00
|
|
|
if help != nil && help[i] != "" {
|
2021-03-11 17:20:31 +01:00
|
|
|
parts := strings.Split(help[i], "\n")
|
|
|
|
lines = append(lines, parts...)
|
2022-01-18 18:29:29 +01:00
|
|
|
lines = append(lines, fmt.Sprintf("(%s)", text))
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
pos := i + 1
|
|
|
|
terminal.WriteString(attributes[i%len(attributes)])
|
2022-01-18 18:29:29 +01:00
|
|
|
if len(lines) == 0 {
|
2021-03-11 17:20:31 +01:00
|
|
|
fmt.Printf("%2d > %s\n", pos, text)
|
|
|
|
} else {
|
|
|
|
mid := (len(lines) - 1) / 2
|
|
|
|
for i, line := range lines {
|
|
|
|
var sep rune
|
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
sep = '/'
|
|
|
|
case len(lines) - 1:
|
|
|
|
sep = '\\'
|
|
|
|
default:
|
|
|
|
sep = '|'
|
|
|
|
}
|
|
|
|
number := " "
|
|
|
|
if i == mid {
|
|
|
|
number = fmt.Sprintf("%2d", pos)
|
|
|
|
}
|
|
|
|
fmt.Printf("%s %c %s\n", number, sep, line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
terminal.WriteString(terminal.Reset)
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
2021-08-22 22:43:51 +02:00
|
|
|
for _, v := range choices {
|
2021-03-11 17:20:31 +01:00
|
|
|
if result == v {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
if result == "" {
|
|
|
|
// If empty string is in the predefined list of choices it has already been returned above.
|
|
|
|
// If parameter required is not set, then empty string is always a valid value.
|
|
|
|
if !required {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
// If parameter required is set, but there is a default, then empty input means default.
|
|
|
|
if defaultValue != "" {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
// If parameter required is set, and there is no default, then an input value is required.
|
|
|
|
fmt.Printf("This value is required and it has no default.\n")
|
|
|
|
} else if newOk {
|
|
|
|
// If legal input is not restricted to defined choices, then any nonzero input string is accepted.
|
|
|
|
return result
|
|
|
|
} else {
|
|
|
|
// A nonzero input string was specified but it did not match any of the strictly defined choices.
|
|
|
|
fmt.Printf("This value must match %s value.\n", valueDescription)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if i >= 1 && i <= len(choices) {
|
|
|
|
return choices[i-1]
|
|
|
|
}
|
|
|
|
fmt.Printf("No choices with this number.\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter prompts for an input value of a specified type
|
|
|
|
func Enter(what string, kind string, defaultValue string, required bool) string {
|
2021-09-11 21:10:05 +02:00
|
|
|
// Empty input is allowed if not required is set, or if
|
|
|
|
// required is set but there is a default value to use.
|
|
|
|
fmt.Printf("Enter a %s.", kind)
|
|
|
|
if defaultValue != "" {
|
|
|
|
fmt.Printf(" Press Enter for the default (%s).\n", defaultValue)
|
|
|
|
} else if !required {
|
|
|
|
fmt.Println(" Press Enter to leave empty.")
|
2021-08-22 22:43:51 +02:00
|
|
|
} else {
|
2021-09-11 21:10:05 +02:00
|
|
|
fmt.Println()
|
2021-08-22 22:43:51 +02:00
|
|
|
}
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
if !required || result != "" {
|
|
|
|
return result
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
if defaultValue != "" {
|
|
|
|
return defaultValue
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
fmt.Printf("This value is required and it has no default.\n")
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-22 22:43:51 +02:00
|
|
|
// ChoosePassword asks the user for a password
|
2021-09-11 13:46:37 +02:00
|
|
|
func ChoosePassword(defaultValue string, required bool) string {
|
2021-08-22 22:43:51 +02:00
|
|
|
fmt.Printf("Choose an alternative below.")
|
2021-09-11 13:46:37 +02:00
|
|
|
actions := []string{"yYes, type in my own password", "gGenerate random password"}
|
2021-08-22 22:43:51 +02:00
|
|
|
defaultAction := -1
|
2021-09-11 13:46:37 +02:00
|
|
|
if defaultValue != "" {
|
2021-08-22 22:43:51 +02:00
|
|
|
defaultAction = len(actions)
|
2021-09-11 13:46:37 +02:00
|
|
|
actions = append(actions, "nNo, keep existing")
|
|
|
|
fmt.Printf(" Press Enter for the default (%s).", string(actions[defaultAction][0]))
|
|
|
|
} else if !required {
|
|
|
|
defaultAction = len(actions)
|
|
|
|
actions = append(actions, "nNo, leave this optional password blank")
|
2021-08-22 22:43:51 +02:00
|
|
|
fmt.Printf(" Press Enter for the default (%s).", string(actions[defaultAction][0]))
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
var password string
|
|
|
|
var err error
|
|
|
|
switch i := CommandDefault(actions, defaultAction); i {
|
|
|
|
case 'y':
|
|
|
|
password = ChangePassword("the")
|
|
|
|
case 'g':
|
|
|
|
for {
|
|
|
|
fmt.Printf("Password strength in bits.\n64 is just about memorable\n128 is secure\n1024 is the maximum\n")
|
|
|
|
bits := ChooseNumber("Bits", 64, 1024)
|
|
|
|
password, err = Password(bits)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to make password: %v", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("Your password is: %s\n", password)
|
|
|
|
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(true) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'n':
|
2021-09-11 13:46:37 +02:00
|
|
|
return defaultValue
|
2021-08-22 22:43:51 +02:00
|
|
|
default:
|
|
|
|
fs.Errorf(nil, "Bad choice %c", i)
|
|
|
|
}
|
|
|
|
return obscure.MustObscure(password)
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
// ChooseNumber asks the user to enter a number between min and max
|
|
|
|
// inclusive prompting them with what.
|
|
|
|
func ChooseNumber(what string, min, max int) int {
|
|
|
|
for {
|
|
|
|
fmt.Printf("%s> ", what)
|
|
|
|
result := ReadLine()
|
|
|
|
i, err := strconv.Atoi(result)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Bad number: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if i < min || i > max {
|
|
|
|
fmt.Printf("Out of range - %d to %d inclusive\n", min, max)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowRemotes shows an overview of the config file
|
|
|
|
func ShowRemotes() {
|
2021-04-26 23:37:49 +02:00
|
|
|
remotes := LoadedData().GetSectionList()
|
2021-03-11 17:20:31 +01:00
|
|
|
if len(remotes) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sort.Strings(remotes)
|
|
|
|
fmt.Printf("%-20s %s\n", "Name", "Type")
|
|
|
|
fmt.Printf("%-20s %s\n", "====", "====")
|
|
|
|
for _, remote := range remotes {
|
|
|
|
fmt.Printf("%-20s %s\n", remote, FileGet(remote, "type"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseRemote chooses a remote name
|
|
|
|
func ChooseRemote() string {
|
2021-04-26 23:37:49 +02:00
|
|
|
remotes := LoadedData().GetSectionList()
|
2021-03-11 17:20:31 +01:00
|
|
|
sort.Strings(remotes)
|
2022-06-01 21:50:20 +02:00
|
|
|
fmt.Println("Select remote.")
|
2021-08-22 22:43:51 +02:00
|
|
|
return Choose("remote", "value", remotes, nil, "", true, false)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mustFindByName finds the RegInfo for the remote name passed in or
|
|
|
|
// exits with a fatal error.
|
|
|
|
func mustFindByName(name string) *fs.RegInfo {
|
|
|
|
fsType := FileGet(name, "type")
|
|
|
|
if fsType == "" {
|
|
|
|
log.Fatalf("Couldn't find type of fs for %q", name)
|
|
|
|
}
|
|
|
|
return fs.MustFind(fsType)
|
|
|
|
}
|
|
|
|
|
2023-07-06 18:55:53 +02:00
|
|
|
// findByName finds the RegInfo for the remote name passed in or
|
|
|
|
// returns an error
|
|
|
|
func findByName(name string) (*fs.RegInfo, error) {
|
|
|
|
fsType := FileGet(name, "type")
|
|
|
|
if fsType == "" {
|
|
|
|
return nil, fmt.Errorf("couldn't find type of fs for %q", name)
|
|
|
|
}
|
|
|
|
return fs.Find(fsType)
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:53:47 +02:00
|
|
|
// printRemoteOptions prints the options of the remote
|
2023-07-06 18:55:53 +02:00
|
|
|
func printRemoteOptions(name string, prefix string, sep string, redacted bool) {
|
|
|
|
fsInfo, err := findByName(name)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("# %v\n", err)
|
|
|
|
fsInfo = nil
|
|
|
|
}
|
2021-04-26 23:37:49 +02:00
|
|
|
for _, key := range LoadedData().GetKeyList(name) {
|
2021-03-11 17:20:31 +01:00
|
|
|
isPassword := false
|
2023-07-06 18:55:53 +02:00
|
|
|
isSensitive := false
|
|
|
|
if fsInfo != nil {
|
|
|
|
for _, option := range fsInfo.Options {
|
|
|
|
if option.Name == key {
|
|
|
|
if option.IsPassword {
|
|
|
|
isPassword = true
|
|
|
|
} else if option.Sensitive {
|
|
|
|
isSensitive = true
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
value := FileGet(name, key)
|
2023-07-06 18:55:53 +02:00
|
|
|
if redacted && (isSensitive || isPassword) && value != "" {
|
|
|
|
fmt.Printf("%s%s%sXXX\n", prefix, key, sep)
|
|
|
|
} else if isPassword && value != "" {
|
2022-05-28 15:53:47 +02:00
|
|
|
fmt.Printf("%s%s%s*** ENCRYPTED ***\n", prefix, key, sep)
|
2021-03-11 17:20:31 +01:00
|
|
|
} else {
|
2022-05-28 15:53:47 +02:00
|
|
|
fmt.Printf("%s%s%s%s\n", prefix, key, sep, value)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-28 15:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// listRemoteOptions lists the options of the remote
|
|
|
|
func listRemoteOptions(name string) {
|
2023-07-06 18:55:53 +02:00
|
|
|
printRemoteOptions(name, "- ", ": ", false)
|
2022-05-28 15:53:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ShowRemote shows the contents of the remote in config file format
|
|
|
|
func ShowRemote(name string) {
|
|
|
|
fmt.Printf("[%s]\n", name)
|
2023-07-06 18:55:53 +02:00
|
|
|
printRemoteOptions(name, "", " = ", false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowRedactedRemote shows the contents of the remote in config file format
|
|
|
|
func ShowRedactedRemote(name string) {
|
|
|
|
fmt.Printf("[%s]\n", name)
|
|
|
|
printRemoteOptions(name, "", " = ", true)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// OkRemote prints the contents of the remote and ask if it is OK
|
|
|
|
func OkRemote(name string) bool {
|
2022-06-01 21:50:20 +02:00
|
|
|
fmt.Println("Configuration complete.")
|
|
|
|
fmt.Println("Options:")
|
2022-05-28 15:53:47 +02:00
|
|
|
listRemoteOptions(name)
|
2022-06-01 21:50:20 +02:00
|
|
|
fmt.Printf("Keep this %q remote?\n", name)
|
2021-03-11 17:20:31 +01:00
|
|
|
switch i := CommandDefault([]string{"yYes this is OK", "eEdit this remote", "dDelete this remote"}, 0); i {
|
|
|
|
case 'y':
|
|
|
|
return true
|
|
|
|
case 'e':
|
|
|
|
return false
|
|
|
|
case 'd':
|
2021-04-26 23:37:49 +02:00
|
|
|
LoadedData().DeleteSection(name)
|
2021-03-11 17:20:31 +01:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
fs.Errorf(nil, "Bad choice %c", i)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:52:48 +02:00
|
|
|
// newSection prints an empty line to separate sections
|
|
|
|
func newSection() {
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
|
2021-05-09 17:03:18 +02:00
|
|
|
// backendConfig configures the backend starting from the state passed in
|
2021-04-29 10:28:18 +02:00
|
|
|
//
|
|
|
|
// The is the user interface loop that drives the post configuration backend config.
|
2021-05-09 17:03:18 +02:00
|
|
|
func backendConfig(ctx context.Context, name string, m configmap.Mapper, ri *fs.RegInfo, choices configmap.Getter, startState string) error {
|
2021-04-29 10:28:18 +02:00
|
|
|
in := fs.ConfigIn{
|
2021-05-09 17:03:18 +02:00
|
|
|
State: startState,
|
2021-04-29 10:28:18 +02:00
|
|
|
}
|
|
|
|
for {
|
2021-05-09 17:03:18 +02:00
|
|
|
out, err := fs.BackendConfig(ctx, name, m, ri, choices, in)
|
2021-04-29 10:28:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if out.Error != "" {
|
|
|
|
fmt.Println(out.Error)
|
|
|
|
}
|
|
|
|
in.State = out.State
|
|
|
|
in.Result = out.Result
|
|
|
|
if out.Option != nil {
|
2021-05-05 12:45:30 +02:00
|
|
|
fs.Debugf(name, "config: reading config parameter %q", out.Option.Name)
|
2021-04-29 10:28:18 +02:00
|
|
|
if out.Option.Default == nil {
|
|
|
|
out.Option.Default = ""
|
|
|
|
}
|
|
|
|
if Default, isBool := out.Option.Default.(bool); isBool &&
|
|
|
|
len(out.Option.Examples) == 2 &&
|
|
|
|
out.Option.Examples[0].Help == "Yes" &&
|
|
|
|
out.Option.Examples[0].Value == "true" &&
|
|
|
|
out.Option.Examples[1].Help == "No" &&
|
|
|
|
out.Option.Examples[1].Value == "false" &&
|
|
|
|
out.Option.Exclusive {
|
|
|
|
// Use Confirm for Yes/No questions as it has a nicer interface=
|
|
|
|
fmt.Println(out.Option.Help)
|
|
|
|
in.Result = fmt.Sprint(Confirm(Default))
|
|
|
|
} else {
|
2021-08-22 22:43:51 +02:00
|
|
|
value := ChooseOption(out.Option, name)
|
2021-04-29 10:28:18 +02:00
|
|
|
if value != "" {
|
|
|
|
err := out.Option.Set(value)
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("failed to set option: %w", err)
|
2021-04-29 10:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
in.Result = out.Option.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out.State == "" {
|
|
|
|
break
|
|
|
|
}
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-04-29 10:28:18 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-09 17:03:18 +02:00
|
|
|
// PostConfig configures the backend after the main config has been done
|
|
|
|
//
|
|
|
|
// The is the user interface loop that drives the post configuration backend config.
|
|
|
|
func PostConfig(ctx context.Context, name string, m configmap.Mapper, ri *fs.RegInfo) error {
|
|
|
|
if ri.Config == nil {
|
|
|
|
return errors.New("backend doesn't support reconnect or authorize")
|
|
|
|
}
|
|
|
|
return backendConfig(ctx, name, m, ri, configmap.Simple{}, "")
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
// RemoteConfig runs the config helper for the remote if needed
|
2021-04-06 22:27:34 +02:00
|
|
|
func RemoteConfig(ctx context.Context, name string) error {
|
2021-03-11 17:20:31 +01:00
|
|
|
fmt.Printf("Remote config\n")
|
2021-04-29 10:28:18 +02:00
|
|
|
ri := mustFindByName(name)
|
2024-07-01 19:06:49 +02:00
|
|
|
m := fs.ConfigMap(ri.Prefix, ri.Options, name, nil)
|
2021-04-29 10:28:18 +02:00
|
|
|
if ri.Config == nil {
|
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-04-29 10:28:18 +02:00
|
|
|
return PostConfig(ctx, name, m, ri)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChooseOption asks the user to choose an option
|
2021-08-22 22:43:51 +02:00
|
|
|
func ChooseOption(o *fs.Option, name string) string {
|
2021-08-16 14:43:12 +02:00
|
|
|
fmt.Printf("Option %s.\n", o.Name)
|
|
|
|
if o.Help != "" {
|
|
|
|
// Show help string without empty lines.
|
2022-05-16 18:11:45 +02:00
|
|
|
help := strings.ReplaceAll(strings.TrimSpace(o.Help), "\n\n", "\n")
|
2021-08-16 14:43:12 +02:00
|
|
|
fmt.Println(help)
|
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
|
2021-09-11 13:46:37 +02:00
|
|
|
var defaultValue string
|
|
|
|
if o.Default == nil {
|
|
|
|
defaultValue = ""
|
|
|
|
} else {
|
|
|
|
defaultValue = fmt.Sprint(o.Default)
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
if o.IsPassword {
|
2021-09-11 13:46:37 +02:00
|
|
|
return ChoosePassword(defaultValue, o.Required)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
|
2021-09-11 21:10:05 +02:00
|
|
|
what := "value"
|
|
|
|
if o.Default != "" {
|
|
|
|
switch o.Default.(type) {
|
|
|
|
case bool:
|
|
|
|
what = "boolean value (true or false)"
|
|
|
|
case fs.SizeSuffix:
|
|
|
|
what = "size with suffix K,M,G,T"
|
|
|
|
case fs.Duration:
|
|
|
|
what = "duration s,m,h,d,w,M,y"
|
|
|
|
case int, int8, int16, int32, int64:
|
|
|
|
what = "signed integer"
|
|
|
|
case uint, byte, uint16, uint32, uint64:
|
|
|
|
what = "unsigned integer"
|
|
|
|
default:
|
2024-03-29 16:23:42 +01:00
|
|
|
what = fmt.Sprintf("value of type %s", o.Type())
|
2021-09-11 21:10:05 +02:00
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
var in string
|
|
|
|
for {
|
|
|
|
if len(o.Examples) > 0 {
|
|
|
|
var values []string
|
|
|
|
var help []string
|
|
|
|
for _, example := range o.Examples {
|
2021-05-09 17:03:18 +02:00
|
|
|
values = append(values, example.Value)
|
|
|
|
help = append(help, example.Help)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
in = Choose(o.Name, what, values, help, defaultValue, o.Required, !o.Exclusive)
|
2021-03-11 17:20:31 +01:00
|
|
|
} else {
|
2021-08-22 22:43:51 +02:00
|
|
|
in = Enter(o.Name, what, defaultValue, o.Required)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
if in != "" {
|
|
|
|
newIn, err := configstruct.StringToInterface(o.Default, in)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse %q: %v\n", in, err)
|
2021-03-11 17:20:31 +01:00
|
|
|
continue
|
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
in = fmt.Sprint(newIn) // canonicalise
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2021-08-22 22:43:51 +02:00
|
|
|
return in
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemoteName asks the user for a name for a new remote
|
|
|
|
func NewRemoteName() (name string) {
|
|
|
|
for {
|
2022-06-01 21:50:20 +02:00
|
|
|
fmt.Println("Enter name for new remote.")
|
2021-03-11 17:20:31 +01:00
|
|
|
fmt.Printf("name> ")
|
|
|
|
name = ReadLine()
|
2021-04-26 23:37:49 +02:00
|
|
|
if LoadedData().HasSection(name) {
|
2021-03-11 17:20:31 +01:00
|
|
|
fmt.Printf("Remote %q already exists.\n", name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := fspath.CheckConfigName(name)
|
|
|
|
switch {
|
|
|
|
case name == "":
|
|
|
|
fmt.Printf("Can't use empty name.\n")
|
|
|
|
case driveletter.IsDriveLetter(name):
|
|
|
|
fmt.Printf("Can't use %q as it can be confused with a drive letter.\n", name)
|
|
|
|
case err != nil:
|
|
|
|
fmt.Printf("Can't use %q as %v.\n", name, err)
|
|
|
|
default:
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRemote make a new remote from its name
|
2021-04-06 22:27:34 +02:00
|
|
|
func NewRemote(ctx context.Context, name string) error {
|
2021-03-11 17:20:31 +01:00
|
|
|
var (
|
|
|
|
newType string
|
|
|
|
ri *fs.RegInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Set the type first
|
|
|
|
for {
|
2021-08-22 22:43:51 +02:00
|
|
|
newType = ChooseOption(fsOption(), name)
|
2021-03-11 17:20:31 +01:00
|
|
|
ri, err = fs.Find(newType)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Bad remote %q: %v\n", newType, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2021-04-26 23:37:49 +02:00
|
|
|
LoadedData().SetValue(name, "type", newType)
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-05-11 15:57:37 +02:00
|
|
|
_, err = CreateRemote(ctx, name, newType, nil, UpdateRemoteOpt{
|
|
|
|
All: true,
|
|
|
|
})
|
2021-04-06 22:27:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
if OkRemote(name) {
|
|
|
|
SaveConfig()
|
2021-04-06 22:27:34 +02:00
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-04-06 22:27:34 +02:00
|
|
|
return EditRemote(ctx, ri, name)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditRemote gets the user to edit a remote
|
2021-04-06 22:27:34 +02:00
|
|
|
func EditRemote(ctx context.Context, ri *fs.RegInfo, name string) error {
|
2022-06-01 21:50:20 +02:00
|
|
|
fmt.Printf("Editing existing %q remote with options:\n", name)
|
2022-05-28 15:53:47 +02:00
|
|
|
listRemoteOptions(name)
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
for {
|
2021-05-11 15:57:37 +02:00
|
|
|
_, err := UpdateRemote(ctx, name, nil, UpdateRemoteOpt{
|
|
|
|
All: true,
|
|
|
|
})
|
2021-05-09 17:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
if OkRemote(name) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SaveConfig()
|
2021-05-09 17:03:18 +02:00
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRemote gets the user to delete a remote
|
|
|
|
func DeleteRemote(name string) {
|
2021-04-26 23:37:49 +02:00
|
|
|
LoadedData().DeleteSection(name)
|
2021-03-11 17:20:31 +01:00
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyRemote asks the user for a new remote name and copies name into
|
|
|
|
// it. Returns the new name.
|
|
|
|
func copyRemote(name string) string {
|
|
|
|
newName := NewRemoteName()
|
|
|
|
// Copy the keys
|
2021-04-26 23:37:49 +02:00
|
|
|
for _, key := range LoadedData().GetKeyList(name) {
|
2021-03-11 17:20:31 +01:00
|
|
|
value := getWithDefault(name, key, "")
|
2021-04-26 23:37:49 +02:00
|
|
|
LoadedData().SetValue(newName, key, value)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
return newName
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenameRemote renames a config section
|
|
|
|
func RenameRemote(name string) {
|
|
|
|
fmt.Printf("Enter new name for %q remote.\n", name)
|
|
|
|
newName := copyRemote(name)
|
|
|
|
if name != newName {
|
2021-04-26 23:37:49 +02:00
|
|
|
LoadedData().DeleteSection(name)
|
2021-03-11 17:20:31 +01:00
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyRemote copies a config section
|
|
|
|
func CopyRemote(name string) {
|
|
|
|
fmt.Printf("Enter name for copy of %q remote.\n", name)
|
|
|
|
copyRemote(name)
|
|
|
|
SaveConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowConfigLocation prints the location of the config file in use
|
|
|
|
func ShowConfigLocation() {
|
2021-04-08 17:49:47 +02:00
|
|
|
if configPath := GetConfigPath(); configPath == "" {
|
|
|
|
fmt.Println("Configuration is in memory only")
|
2021-03-11 17:20:31 +01:00
|
|
|
} else {
|
2021-04-08 17:49:47 +02:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
fmt.Println("Configuration file doesn't exist, but rclone will use this path:")
|
|
|
|
} else {
|
|
|
|
fmt.Println("Configuration file is stored at:")
|
|
|
|
}
|
|
|
|
fmt.Printf("%s\n", configPath)
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShowConfig prints the (unencrypted) config options
|
|
|
|
func ShowConfig() {
|
2021-04-26 23:37:49 +02:00
|
|
|
str, err := LoadedData().Serialize()
|
2021-03-11 17:20:31 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to serialize config: %v", err)
|
|
|
|
}
|
|
|
|
if str == "" {
|
|
|
|
str = "; empty config\n"
|
|
|
|
}
|
|
|
|
fmt.Printf("%s", str)
|
|
|
|
}
|
|
|
|
|
2023-07-06 18:55:53 +02:00
|
|
|
// ShowRedactedConfig prints the redacted (unencrypted) config options
|
|
|
|
func ShowRedactedConfig() {
|
|
|
|
remotes := LoadedData().GetSectionList()
|
|
|
|
if len(remotes) == 0 {
|
|
|
|
fmt.Println("; empty config")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sort.Strings(remotes)
|
|
|
|
for i, remote := range remotes {
|
|
|
|
if i != 0 {
|
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
ShowRedactedRemote(remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 17:20:31 +01:00
|
|
|
// EditConfig edits the config file interactively
|
2021-04-06 22:27:34 +02:00
|
|
|
func EditConfig(ctx context.Context) (err error) {
|
2021-03-11 17:20:31 +01:00
|
|
|
for {
|
2021-04-26 23:37:49 +02:00
|
|
|
haveRemotes := len(LoadedData().GetSectionList()) != 0
|
2021-03-11 17:20:31 +01:00
|
|
|
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "rRename remote", "cCopy remote", "sSet configuration password", "qQuit config"}
|
|
|
|
if haveRemotes {
|
|
|
|
fmt.Printf("Current remotes:\n\n")
|
|
|
|
ShowRemotes()
|
|
|
|
fmt.Printf("\n")
|
|
|
|
} else {
|
2021-11-01 21:34:46 +01:00
|
|
|
fmt.Printf("No remotes found, make a new one?\n")
|
2021-03-11 17:20:31 +01:00
|
|
|
// take 2nd item and last 2 items of menu list
|
|
|
|
what = append(what[1:2], what[len(what)-2:]...)
|
|
|
|
}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'e':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
name := ChooseRemote()
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
fs := mustFindByName(name)
|
2021-04-06 22:27:34 +02:00
|
|
|
err = EditRemote(ctx, fs, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
case 'n':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
|
|
|
name := NewRemoteName()
|
|
|
|
newSection()
|
|
|
|
err = NewRemote(ctx, name)
|
2021-04-06 22:27:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-11 17:20:31 +01:00
|
|
|
case 'd':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
name := ChooseRemote()
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
DeleteRemote(name)
|
|
|
|
case 'r':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
|
|
|
name := ChooseRemote()
|
|
|
|
newSection()
|
|
|
|
RenameRemote(name)
|
2021-03-11 17:20:31 +01:00
|
|
|
case 'c':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
|
|
|
name := ChooseRemote()
|
|
|
|
newSection()
|
|
|
|
CopyRemote(name)
|
2021-03-11 17:20:31 +01:00
|
|
|
case 's':
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
SetPassword()
|
|
|
|
case 'q':
|
2021-04-06 22:27:34 +02:00
|
|
|
return nil
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
2022-05-28 13:52:48 +02:00
|
|
|
newSection()
|
2021-03-11 17:20:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Suppress the confirm prompts by altering the context config
|
|
|
|
func suppressConfirm(ctx context.Context) context.Context {
|
|
|
|
newCtx, ci := fs.AddConfig(ctx)
|
|
|
|
ci.AutoConfirm = true
|
|
|
|
return newCtx
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkPassword normalises and validates the password
|
|
|
|
func checkPassword(password string) (string, error) {
|
|
|
|
if !utf8.ValidString(password) {
|
|
|
|
return "", errors.New("password contains invalid utf8 characters")
|
|
|
|
}
|
|
|
|
// Check for leading/trailing whitespace
|
|
|
|
trimmedPassword := strings.TrimSpace(password)
|
|
|
|
// Warn user if password has leading+trailing whitespace
|
|
|
|
if len(password) != len(trimmedPassword) {
|
|
|
|
_, _ = fmt.Fprintln(os.Stderr, "Your password contains leading/trailing whitespace - in previous versions of rclone this was stripped")
|
|
|
|
}
|
|
|
|
// Normalize to reduce weird variations.
|
|
|
|
password = norm.NFKC.String(password)
|
|
|
|
if len(password) == 0 || len(trimmedPassword) == 0 {
|
|
|
|
return "", errors.New("no characters in password")
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPassword asks the user for a password with the prompt given.
|
|
|
|
func GetPassword(prompt string) string {
|
|
|
|
_, _ = fmt.Fprintln(PasswordPromptOutput, prompt)
|
|
|
|
for {
|
|
|
|
_, _ = fmt.Fprint(PasswordPromptOutput, "password:")
|
|
|
|
password := ReadPassword()
|
|
|
|
password, err := checkPassword(password)
|
|
|
|
if err == nil {
|
|
|
|
return password
|
|
|
|
}
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Bad password: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangePassword will query the user twice for the named password. If
|
|
|
|
// the same password is entered it is returned.
|
|
|
|
func ChangePassword(name string) string {
|
|
|
|
for {
|
|
|
|
a := GetPassword(fmt.Sprintf("Enter %s password:", name))
|
|
|
|
b := GetPassword(fmt.Sprintf("Confirm %s password:", name))
|
|
|
|
if a == b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
fmt.Println("Passwords do not match!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPassword will allow the user to modify the current
|
|
|
|
// configuration encryption settings.
|
|
|
|
func SetPassword() {
|
|
|
|
for {
|
|
|
|
if len(configKey) > 0 {
|
|
|
|
fmt.Println("Your configuration is encrypted.")
|
|
|
|
what := []string{"cChange Password", "uUnencrypt configuration", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'c':
|
|
|
|
changeConfigPassword()
|
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password changed")
|
|
|
|
continue
|
|
|
|
case 'u':
|
|
|
|
configKey = nil
|
|
|
|
SaveConfig()
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
fmt.Println("Your configuration is not encrypted.")
|
|
|
|
fmt.Println("If you add a password, you will protect your login information to cloud services.")
|
|
|
|
what := []string{"aAdd Password", "qQuit to main menu"}
|
|
|
|
switch i := Command(what); i {
|
|
|
|
case 'a':
|
|
|
|
changeConfigPassword()
|
|
|
|
SaveConfig()
|
|
|
|
fmt.Println("Password set")
|
|
|
|
continue
|
|
|
|
case 'q':
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|