From 049ff1f269680178481ab7618a4491ced9bacac8 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 5 Nov 2019 12:39:33 +0000 Subject: [PATCH] config: check a remote exists when creating a new one --- fs/config/config.go | 9 +++++++-- fs/config/config_test.go | 30 ++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) 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")()