aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
authorAmine Hilaly <hilalyamine@gmail.com>2019-06-15 02:33:06 +0200
committerAmine Hilaly <hilalyamine@gmail.com>2019-06-15 02:33:06 +0200
commitf70e775dbd7c53ebb6e3cd2ec1af8d73d13449b0 (patch)
tree92ac9d1c0b837e0adf2e4728cadd430cc5393a0b /bridge/core
parentd69dcce806d280ddbd6a4fb17700153bc03da90b (diff)
downloadgit-bug-f70e775dbd7c53ebb6e3cd2ec1af8d73d13449b0.tar.gz
Store bridge type alongside the other params in the config
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/bridge.go81
1 files changed, 44 insertions, 37 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 3513b790..2b2db0a2 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -9,15 +9,19 @@ import (
"strings"
"time"
+ "github.com/pkg/errors"
+
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
- "github.com/pkg/errors"
)
var ErrImportNotSupported = errors.New("import is not supported")
var ErrExportNotSupported = errors.New("export is not supported")
-const bridgeConfigKeyPrefix = "git-bug.bridge"
+const (
+ keyTarget = "target"
+ bridgeConfigKeyPrefix = "git-bug.bridge"
+)
var bridgeImpl map[string]reflect.Type
@@ -81,15 +85,33 @@ func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, erro
return bridge, nil
}
-// Instantiate a new bridge for a repo, from the combined target and name contained
-// in the full name
-func NewBridgeFromFullName(repo *cache.RepoCache, fullName string) (*Bridge, error) {
- target, name, err := splitFullName(fullName)
+// LoadBridge instantiate a new bridge from a repo configuration
+func LoadBridge(repo *cache.RepoCache, name string) (*Bridge, error) {
+ bridge := &Bridge{
+ Name: name,
+ repo: repo,
+ }
+
+ conf, err := bridge.loadConfig()
if err != nil {
return nil, err
}
+ bridge.conf = conf
+
+ target := bridge.conf[keyTarget]
+ implType, ok := bridgeImpl[target]
+ if !ok {
+ return nil, fmt.Errorf("unknown bridge target %v", target)
+ }
+
+ bridge.impl = reflect.New(implType).Elem().Interface().(BridgeImpl)
+
+ err = bridge.impl.ValidateConfig(bridge.conf)
+ if err != nil {
+ return nil, errors.Wrap(err, "invalid configuration")
+ }
- return NewBridge(repo, target, name)
+ return bridge, nil
}
// Attempt to retrieve a default bridge for the given repo. If zero or multiple
@@ -108,22 +130,7 @@ func DefaultBridge(repo *cache.RepoCache) (*Bridge, error) {
return nil, fmt.Errorf("multiple bridge are configured, you need to select one explicitely")
}
- target, name, err := splitFullName(bridges[0])
- if err != nil {
- return nil, err
- }
-
- return NewBridge(repo, target, name)
-}
-
-func splitFullName(fullName string) (string, string, error) {
- split := strings.Split(fullName, ".")
-
- if len(split) != 2 {
- return "", "", fmt.Errorf("bad bridge fullname: %s", fullName)
- }
-
- return split[0], split[1], nil
+ return LoadBridge(repo, bridges[0])
}
// ConfiguredBridges return the list of bridge that are configured for the given
@@ -134,7 +141,7 @@ func ConfiguredBridges(repo repository.RepoCommon) ([]string, error) {
return nil, errors.Wrap(err, "can't read configured bridges")
}
- re, err := regexp.Compile(bridgeConfigKeyPrefix + `.([^.]+\.[^.]+)`)
+ re, err := regexp.Compile(bridgeConfigKeyPrefix + `.([^.]+)`)
if err != nil {
panic(err)
}
@@ -163,17 +170,17 @@ func ConfiguredBridges(repo repository.RepoCommon) ([]string, error) {
}
// Remove a configured bridge
-func RemoveBridge(repo repository.RepoCommon, fullName string) error {
- re, err := regexp.Compile(`^[^.]+\.[^.]+$`)
+func RemoveBridge(repo repository.RepoCommon, name string) error {
+ re, err := regexp.Compile(`^[a-zA-Z0-9]+`)
if err != nil {
panic(err)
}
- if !re.MatchString(fullName) {
- return fmt.Errorf("bad bridge fullname: %s", fullName)
+ if !re.MatchString(name) {
+ return fmt.Errorf("bad bridge fullname: %s", name)
}
- keyPrefix := fmt.Sprintf("git-bug.bridge.%s", fullName)
+ keyPrefix := fmt.Sprintf("git-bug.bridge.%s", name)
return repo.RmConfigs(keyPrefix)
}
@@ -184,14 +191,18 @@ func (b *Bridge) Configure(params BridgeParams) error {
return err
}
- b.conf = conf
+ err = b.impl.ValidateConfig(conf)
+ if err != nil {
+ return fmt.Errorf("invalid configuration: %v", err)
+ }
+ b.conf = conf
return b.storeConfig(conf)
}
func (b *Bridge) storeConfig(conf Configuration) error {
for key, val := range conf {
- storeKey := fmt.Sprintf("git-bug.bridge.%s.%s.%s", b.impl.Target(), b.Name, key)
+ storeKey := fmt.Sprintf("git-bug.bridge.%s.%s", b.Name, key)
err := b.repo.StoreConfig(storeKey, val)
if err != nil {
@@ -215,7 +226,7 @@ func (b *Bridge) ensureConfig() error {
}
func (b *Bridge) loadConfig() (Configuration, error) {
- keyPrefix := fmt.Sprintf("git-bug.bridge.%s.%s.", b.impl.Target(), b.Name)
+ keyPrefix := fmt.Sprintf("git-bug.bridge.%s.", b.Name)
pairs, err := b.repo.ReadConfigs(keyPrefix)
if err != nil {
@@ -225,14 +236,10 @@ func (b *Bridge) loadConfig() (Configuration, error) {
result := make(Configuration, len(pairs))
for key, value := range pairs {
key := strings.TrimPrefix(key, keyPrefix)
+ fmt.Println(key, value)
result[key] = value
}
- err = b.impl.ValidateConfig(result)
- if err != nil {
- return nil, errors.Wrap(err, "invalid configuration")
- }
-
return result, nil
}