aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-07-07 12:15:01 +0200
committerMichael Muré <batolettre@gmail.com>2019-07-07 12:15:01 +0200
commitdc289876c625ec1c606e84112a6b6c53310e19be (patch)
tree0455ee404252f1b682980ffa0e6df6200bef06da
parent5b1a8cdefa9516d215f99ee114d451c96997f34a (diff)
downloadgit-bug-dc289876c625ec1c606e84112a6b6c53310e19be.tar.gz
bridge: detect when trying to configure a bridge with a name already taken
-rw-r--r--bridge/core/bridge.go11
-rw-r--r--bridge/core/export.go3
-rw-r--r--commands/bridge_configure.go32
3 files changed, 33 insertions, 13 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index a8a0c033..abb57ed7 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -163,6 +163,15 @@ func ConfiguredBridges(repo repository.RepoCommon) ([]string, error) {
return result, nil
}
+// Check if a bridge exist
+func BridgeExist(repo repository.RepoCommon, name string) bool {
+ keyPrefix := fmt.Sprintf("git-bug.bridge.%s.", name)
+
+ conf, err := repo.ReadConfigs(keyPrefix)
+
+ return err == nil && len(conf) > 0
+}
+
// Remove a configured bridge
func RemoveBridge(repo repository.RepoCommon, name string) error {
re, err := regexp.Compile(`^[a-zA-Z0-9]+`)
@@ -219,7 +228,7 @@ func (b *Bridge) ensureConfig() error {
return nil
}
-func loadConfig(repo *cache.RepoCache, name string) (Configuration, error) {
+func loadConfig(repo repository.RepoCommon, name string) (Configuration, error) {
keyPrefix := fmt.Sprintf("git-bug.bridge.%s.", name)
pairs, err := repo.ReadConfigs(keyPrefix)
diff --git a/bridge/core/export.go b/bridge/core/export.go
index 080ced80..2bcf0087 100644
--- a/bridge/core/export.go
+++ b/bridge/core/export.go
@@ -15,6 +15,9 @@ const (
ExportEventNothing
)
+// ExportResult is an event that is emitted during the export process, to
+// allow calling code to report on what is happening, collect metrics or
+// display meaningful errors if something went wrong.
type ExportResult struct {
Err error
Event ExportEvent
diff --git a/commands/bridge_configure.go b/commands/bridge_configure.go
index 61d969d1..e3576ead 100644
--- a/commands/bridge_configure.go
+++ b/commands/bridge_configure.go
@@ -14,6 +14,7 @@ import (
"github.com/MichaelMure/git-bug/bridge"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/interrupt"
)
@@ -52,7 +53,7 @@ func runBridgeConfigure(cmd *cobra.Command, args []string) error {
}
if bridgeConfigureName == "" {
- bridgeConfigureName, err = promptName()
+ bridgeConfigureName, err = promptName(repo)
if err != nil {
return err
}
@@ -99,21 +100,28 @@ func promptTarget() (string, error) {
}
}
-func promptName() (string, error) {
- fmt.Printf("name [%s]: ", defaultName)
+func promptName(repo repository.RepoCommon) (string, error) {
+ for {
+ fmt.Printf("name [%s]: ", defaultName)
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", err
- }
+ line, err := bufio.NewReader(os.Stdin).ReadString('\n')
+ if err != nil {
+ return "", err
+ }
- line = strings.TrimRight(line, "\n")
+ line = strings.TrimRight(line, "\n")
- if line == "" {
- return defaultName, nil
- }
+ name := line
+ if name == "" {
+ name = defaultName
+ }
- return line, nil
+ if !core.BridgeExist(repo, name) {
+ return name, nil
+ }
+
+ fmt.Println("a bridge with the same name already exist")
+ }
}
var bridgeConfigureCmd = &cobra.Command{