Skip to content

Commit

Permalink
bridge: detect when trying to configure a bridge with a name already …
Browse files Browse the repository at this point in the history
…taken
  • Loading branch information
MichaelMure committed Jul 7, 2019
1 parent 5b1a8cd commit dc28987
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
11 changes: 10 additions & 1 deletion bridge/core/bridge.go
Expand Up @@ -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]+`)
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions bridge/core/export.go
Expand Up @@ -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
Expand Down
32 changes: 20 additions & 12 deletions commands/bridge_configure.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit dc28987

Please sign in to comment.