rclone config: allow rename and copy of remotes - fixes #641

This commit is contained in:
Nick Craig-Wood 2017-01-29 15:37:44 +00:00
parent 94947f2523
commit db0b93c0ad

View file

@ -883,6 +883,25 @@ func fsOption() *Option {
return o return o
} }
// NewRemoteName asks the user for a name for a remote
func NewRemoteName() (name string) {
for {
fmt.Printf("name> ")
name = ReadLine()
parts := matcher.FindStringSubmatch(name + ":")
switch {
case name == "":
fmt.Printf("Can't use empty name.\n")
case isDriveLetter(name):
fmt.Printf("Can't use %q as it can be confused a drive letter.\n", name)
case parts == nil:
fmt.Printf("Can't use %q as it has invalid characters in it.\n", name)
default:
return name
}
}
}
// NewRemote make a new remote from its name // NewRemote make a new remote from its name
func NewRemote(name string) { func NewRemote(name string) {
newType := ChooseOption(fsOption()) newType := ChooseOption(fsOption())
@ -928,11 +947,37 @@ func DeleteRemote(name string) {
SaveConfig() SaveConfig()
} }
// copyRemote asks the user for a new remote name and copies name into
// it
func copyRemote(name string) {
newName := NewRemoteName()
// Copy the keys
for _, key := range configData.GetKeyList(name) {
value := configData.MustValue(name, key, "")
configData.SetValue(newName, key, value)
}
}
// RenameRemote renames a config section
func RenameRemote(name string) {
fmt.Printf("Enter new name for %q remote.\n", name)
copyRemote(name)
configData.DeleteSection(name)
SaveConfig()
}
// CopyRemote copies a config section
func CopyRemote(name string) {
fmt.Printf("Enter name for copy of %q remote.\n", name)
copyRemote(name)
SaveConfig()
}
// EditConfig edits the config file interactively // EditConfig edits the config file interactively
func EditConfig() { func EditConfig() {
for { for {
haveRemotes := len(configData.GetSectionList()) != 0 haveRemotes := len(configData.GetSectionList()) != 0
what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "sSet configuration password", "qQuit config"} what := []string{"eEdit existing remote", "nNew remote", "dDelete remote", "rRename remote", "cCopy remote", "sSet configuration password", "qQuit config"}
if haveRemotes { if haveRemotes {
fmt.Printf("Current remotes:\n\n") fmt.Printf("Current remotes:\n\n")
ShowRemotes() ShowRemotes()
@ -947,26 +992,14 @@ func EditConfig() {
fs := MustFindByName(name) fs := MustFindByName(name)
EditRemote(fs, name) EditRemote(fs, name)
case 'n': case 'n':
nameLoop: NewRemote(NewRemoteName())
for {
fmt.Printf("name> ")
name := ReadLine()
parts := matcher.FindStringSubmatch(name + ":")
switch {
case name == "":
fmt.Printf("Can't use empty name\n")
case isDriveLetter(name):
fmt.Printf("Can't use %q as it can be confused a drive letter\n", name)
case len(parts) != 3 || parts[2] != "":
fmt.Printf("Can't use %q as it has invalid characters in it %v\n", name, parts)
default:
NewRemote(name)
break nameLoop
}
}
case 'd': case 'd':
name := ChooseRemote() name := ChooseRemote()
DeleteRemote(name) DeleteRemote(name)
case 'r':
RenameRemote(ChooseRemote())
case 'c':
CopyRemote(ChooseRemote())
case 's': case 's':
SetPassword() SetPassword()
case 'q': case 'q':