bridge.go

  1package core
  2
  3import (
  4	"fmt"
  5	"reflect"
  6	"regexp"
  7	"strings"
  8
  9	"github.com/MichaelMure/git-bug/cache"
 10	"github.com/MichaelMure/git-bug/repository"
 11	"github.com/pkg/errors"
 12)
 13
 14var ErrImportNorSupported = errors.New("import is not supported")
 15var ErrExportNorSupported = errors.New("export is not supported")
 16
 17var bridgeImpl map[string]reflect.Type
 18
 19// Bridge is a wrapper around a BridgeImpl that will bind low-level
 20// implementation with utility code to provide high-level functions.
 21type Bridge struct {
 22	Name string
 23	repo *cache.RepoCache
 24	impl BridgeImpl
 25	conf Configuration
 26}
 27
 28// Register will register a new BridgeImpl
 29func Register(impl BridgeImpl) {
 30	if bridgeImpl == nil {
 31		bridgeImpl = make(map[string]reflect.Type)
 32	}
 33	bridgeImpl[impl.Target()] = reflect.TypeOf(impl)
 34}
 35
 36// Targets return all known bridge implementation target
 37func Targets() []string {
 38	var result []string
 39
 40	for key := range bridgeImpl {
 41		result = append(result, key)
 42	}
 43
 44	return result
 45}
 46
 47func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, error) {
 48	implType, ok := bridgeImpl[target]
 49	if !ok {
 50		return nil, fmt.Errorf("unknown bridge target %v", target)
 51	}
 52
 53	impl := reflect.New(implType).Elem().Interface().(BridgeImpl)
 54
 55	bridge := &Bridge{
 56		Name: name,
 57		repo: repo,
 58		impl: impl,
 59	}
 60
 61	return bridge, nil
 62}
 63
 64func ConfiguredBridges(repo repository.RepoCommon) ([]string, error) {
 65	configs, err := repo.ReadConfigs("git-bug.")
 66	if err != nil {
 67		return nil, errors.Wrap(err, "can't read configured bridges")
 68	}
 69
 70	re, err := regexp.Compile(`git-bug.([^\.]+\.[^\.]+)`)
 71	if err != nil {
 72		return nil, err
 73	}
 74
 75	set := make(map[string]interface{})
 76
 77	for key := range configs {
 78		res := re.FindStringSubmatch(key)
 79
 80		if res == nil {
 81			continue
 82		}
 83
 84		set[res[1]] = nil
 85	}
 86
 87	result := make([]string, len(set))
 88
 89	i := 0
 90	for key := range set {
 91		result[i] = key
 92		i++
 93	}
 94
 95	return result, nil
 96}
 97
 98func (b *Bridge) String() string {
 99	var _type string
100	if b.impl.Importer() != nil && b.impl.Exporter() != nil {
101		_type = "import/export"
102	} else if b.impl.Importer() != nil {
103		_type = "import"
104	} else if b.impl.Exporter() != nil {
105		_type = "export"
106	} else {
107		panic("bad bridge impl, neither import nor export")
108	}
109
110	return fmt.Sprintf("%s.%s: %s", b.impl.Target(), b.Name, _type)
111}
112
113func (b *Bridge) Configure() error {
114	conf, err := b.impl.Configure(b.repo)
115	if err != nil {
116		return err
117	}
118
119	b.conf = conf
120
121	return b.storeConfig(conf)
122}
123
124func (b *Bridge) storeConfig(conf Configuration) error {
125	for key, val := range conf {
126		storeKey := fmt.Sprintf("git-bug.%s.%s.%s", b.impl.Target(), b.Name, key)
127
128		err := b.repo.StoreConfig(storeKey, val)
129		if err != nil {
130			return errors.Wrap(err, "error while storing bridge configuration")
131		}
132	}
133
134	return nil
135}
136
137func (b Bridge) getConfig() (Configuration, error) {
138	var err error
139	if b.conf == nil {
140		b.conf, err = b.loadConfig()
141		if err != nil {
142			return nil, err
143		}
144	}
145
146	return b.conf, nil
147}
148
149func (b Bridge) loadConfig() (Configuration, error) {
150	keyPrefix := fmt.Sprintf("git-bug.%s.%s.", b.impl.Target(), b.Name)
151
152	pairs, err := b.repo.ReadConfigs(keyPrefix)
153	if err != nil {
154		return nil, errors.Wrap(err, "error while reading bridge configuration")
155	}
156
157	result := make(Configuration, len(pairs))
158	for key, value := range pairs {
159		key := strings.TrimPrefix(key, keyPrefix)
160		result[key] = value
161	}
162
163	return result, nil
164}
165
166func (b Bridge) ImportAll() error {
167	importer := b.impl.Importer()
168	if importer == nil {
169		return ErrImportNorSupported
170	}
171
172	conf, err := b.getConfig()
173	if err != nil {
174		return err
175	}
176
177	return b.impl.Importer().ImportAll(b.repo, conf)
178}
179
180func (b Bridge) Import(id string) error {
181	importer := b.impl.Importer()
182	if importer == nil {
183		return ErrImportNorSupported
184	}
185
186	conf, err := b.getConfig()
187	if err != nil {
188		return err
189	}
190
191	return b.impl.Importer().Import(b.repo, conf, id)
192}
193
194func (b Bridge) ExportAll() error {
195	exporter := b.impl.Exporter()
196	if exporter == nil {
197		return ErrExportNorSupported
198	}
199
200	conf, err := b.getConfig()
201	if err != nil {
202		return err
203	}
204
205	return b.impl.Exporter().ExportAll(b.repo, conf)
206}
207
208func (b Bridge) Export(id string) error {
209	exporter := b.impl.Exporter()
210	if exporter == nil {
211		return ErrExportNorSupported
212	}
213
214	conf, err := b.getConfig()
215	if err != nil {
216		return err
217	}
218
219	return b.impl.Exporter().Export(b.repo, conf, id)
220}