aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/bridge.go155
-rw-r--r--bridge/core/interfaces.go29
2 files changed, 168 insertions, 16 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 3ff8404b..3e3b935e 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -1,26 +1,149 @@
package core
-import "github.com/MichaelMure/git-bug/cache"
+import (
+ "fmt"
+ "os/exec"
+ "strings"
-type Common interface {
- // Configure handle the user interaction and return a key/value configuration
- // for future use
- Configure() (map[string]string, error)
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/repository"
+ "github.com/pkg/errors"
+)
+
+var ErrImportNorSupported = errors.New("import is not supported")
+var ErrExportNorSupported = errors.New("export is not supported")
+
+// Bridge is a wrapper around a BridgeImpl that will bind low-level
+// implementation with utility code to provide high-level functions.
+type Bridge struct {
+ impl BridgeImpl
+ conf Configuration
}
-type Importer interface {
- Common
- ImportAll(repo *cache.RepoCache) error
- Import(repo *cache.RepoCache, id string) error
+func NewBridge(impl BridgeImpl) *Bridge {
+ return &Bridge{
+ impl: impl,
+ }
}
-type Exporter interface {
- Common
- ExportAll(repo *cache.RepoCache) error
- Export(repo *cache.RepoCache, id string) error
+func (b *Bridge) Configure(repo repository.RepoCommon) error {
+ conf, err := b.impl.Configure(repo)
+ if err != nil {
+ return err
+ }
+
+ return b.storeConfig(repo, conf)
}
-type NotSupportedImporter struct{}
-type NotSupportedExporter struct{}
+func (b *Bridge) storeConfig(repo repository.RepoCommon, conf Configuration) error {
+ for key, val := range conf {
+ storeKey := fmt.Sprintf("git-bug.%s.%s", b.impl.Name(), key)
+
+ cmd := exec.Command("git", "config", "--replace-all", storeKey, val)
+ cmd.Dir = repo.GetPath()
+
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error while storing bridge configuration: %s", out)
+ }
+ }
-// persist
+ return nil
+}
+
+func (b Bridge) getConfig(repo repository.RepoCommon) (Configuration, error) {
+ var err error
+ if b.conf == nil {
+ b.conf, err = b.loadConfig(repo)
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ return b.conf, nil
+}
+
+func (b Bridge) loadConfig(repo repository.RepoCommon) (Configuration, error) {
+ key := fmt.Sprintf("git-bug.%s", b.impl.Name())
+ cmd := exec.Command("git", "config", "--get-regexp", key)
+ cmd.Dir = repo.GetPath()
+
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ return nil, fmt.Errorf("error while reading bridge configuration: %s", out)
+ }
+
+ lines := strings.Split(string(out), "\n")
+
+ result := make(Configuration, len(lines))
+ for _, line := range lines {
+ if strings.TrimSpace(line) == "" {
+ continue
+ }
+
+ parts := strings.Fields(line)
+ if len(parts) != 2 {
+ return nil, fmt.Errorf("bad bridge configuration: %s", line)
+ }
+
+ result[parts[0]] = parts[1]
+ }
+
+ return result, nil
+}
+
+func (b Bridge) ImportAll(repo *cache.RepoCache) error {
+ importer := b.impl.Importer()
+ if importer == nil {
+ return ErrImportNorSupported
+ }
+
+ conf, err := b.getConfig(repo)
+ if err != nil {
+ return err
+ }
+
+ return b.impl.Importer().ImportAll(repo, conf)
+}
+
+func (b Bridge) Import(repo *cache.RepoCache, id string) error {
+ importer := b.impl.Importer()
+ if importer == nil {
+ return ErrImportNorSupported
+ }
+
+ conf, err := b.getConfig(repo)
+ if err != nil {
+ return err
+ }
+
+ return b.impl.Importer().Import(repo, conf, id)
+}
+
+func (b Bridge) ExportAll(repo *cache.RepoCache) error {
+ exporter := b.impl.Exporter()
+ if exporter == nil {
+ return ErrExportNorSupported
+ }
+
+ conf, err := b.getConfig(repo)
+ if err != nil {
+ return err
+ }
+
+ return b.impl.Exporter().ExportAll(repo, conf)
+}
+
+func (b Bridge) Export(repo *cache.RepoCache, id string) error {
+ exporter := b.impl.Exporter()
+ if exporter == nil {
+ return ErrExportNorSupported
+ }
+
+ conf, err := b.getConfig(repo)
+ if err != nil {
+ return err
+ }
+
+ return b.impl.Exporter().Export(repo, conf, id)
+}
diff --git a/bridge/core/interfaces.go b/bridge/core/interfaces.go
new file mode 100644
index 00000000..5336e7a8
--- /dev/null
+++ b/bridge/core/interfaces.go
@@ -0,0 +1,29 @@
+package core
+
+import (
+ "github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/repository"
+)
+
+type Configuration map[string]string
+
+type Importer interface {
+ ImportAll(repo *cache.RepoCache, conf Configuration) error
+ Import(repo *cache.RepoCache, conf Configuration, id string) error
+}
+
+type Exporter interface {
+ ExportAll(repo *cache.RepoCache, conf Configuration) error
+ Export(repo *cache.RepoCache, conf Configuration, id string) error
+}
+
+type BridgeImpl interface {
+ Name() string
+
+ // Configure handle the user interaction and return a key/value configuration
+ // for future use
+ Configure(repo repository.RepoCommon) (Configuration, error)
+
+ Importer() Importer
+ Exporter() Exporter
+}