diff options
author | Michael Muré <batolettre@gmail.com> | 2018-09-21 18:23:46 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-09-21 18:53:44 +0200 |
commit | 921cd18cf98ecfc1f7fa82f57d64f1b1f9077e64 (patch) | |
tree | dfd7fea5be54c6020ed6773fbdefbaeb5cd2a78e /bridge/core/bridge.go | |
parent | 82eaceffc1d750832a2a66f206749d2dca968cce (diff) | |
download | git-bug-921cd18cf98ecfc1f7fa82f57d64f1b1f9077e64.tar.gz |
bridge: better interfaces, working github configurator
Diffstat (limited to 'bridge/core/bridge.go')
-rw-r--r-- | bridge/core/bridge.go | 155 |
1 files changed, 139 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) +} |