Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

config: check a remote exists when creating a new one

Nick Craig-Wood049ff1f26968
diff --git a/fs/config/config.go b/fs/config/config.go
index cfceeb2a0..7f8ac827c 100644
--- a/fs/config/config.go
+++ b/fs/config/config.go
@@ -1084,12 +1084,17 @@ func fsOption() *fs.Option {
return o
}
-// NewRemoteName asks the user for a name for a remote
+// NewRemoteName asks the user for a name for a new remote
func NewRemoteName() (name string) {
for {
fmt.Printf("name> ")
name = ReadLine()
- err := fspath.CheckConfigName(name)
+ _, err := getConfigData().GetSection(name)
+ if err == nil {
+ fmt.Printf("Remote %q already exists.\n", name)
+ continue
+ }
+ err = fspath.CheckConfigName(name)
switch {
case name == "":
fmt.Printf("Can't use empty name.\n")
diff --git a/fs/config/config_test.go b/fs/config/config_test.go
index c662501a9..c89b7f3b7 100644
--- a/fs/config/config_test.go
+++ b/fs/config/config_test.go
@@ -115,14 +115,6 @@ func TestCRUD(t *testing.T) {
assert.Equal(t, "true", FileGet("asdf", "bool"))
assert.Equal(t, "secret", obscure.MustReveal(FileGet("asdf", "pass")))
- // no-op rename, asdf → asdf
- RenameRemote("asdf")
-
- assert.Equal(t, []string{"asdf"}, configFile.GetSectionList())
- assert.Equal(t, "config_test_remote", FileGet("asdf", "type"))
- assert.Equal(t, "true", FileGet("asdf", "bool"))
- assert.Equal(t, "secret", obscure.MustReveal(FileGet("asdf", "pass")))
-
// delete remote
DeleteRemote("asdf")
assert.Equal(t, []string{}, configFile.GetSectionList())
@@ -163,6 +155,28 @@ func TestChooseOption(t *testing.T) {
assert.Equal(t, "", FileGet("test", "pass"))
}
+func TestNewRemoteName(t *testing.T) {
+ defer testConfigFile(t, "crud.conf")()
+
+ // script for creating remote
+ ReadLine = makeReadLine([]string{
+ "config_test_remote", // type
+ "true", // bool value
+ "n", // not required
+ "y", // looks good, save
+ })
+ NewRemote("test")
+
+ ReadLine = makeReadLine([]string{
+ "test", // already exists
+ "", // empty string not allowed
+ "bad@characters", // bad characters
+ "newname", // OK
+ })
+
+ assert.Equal(t, "newname", NewRemoteName())
+}
+
func TestCreateUpatePasswordRemote(t *testing.T) {
defer testConfigFile(t, "update.conf")()