1// Package core contains the target-agnostic code to define and run a bridge
2package core
3
4import (
5 "fmt"
6 "reflect"
7 "regexp"
8 "sort"
9 "strings"
10 "time"
11
12 "github.com/MichaelMure/git-bug/cache"
13 "github.com/MichaelMure/git-bug/repository"
14 "github.com/pkg/errors"
15)
16
17var ErrImportNotSupported = errors.New("import is not supported")
18var ErrExportNotSupported = errors.New("export is not supported")
19
20const bridgeConfigKeyPrefix = "git-bug.bridge"
21
22var bridgeImpl map[string]reflect.Type
23
24// Bridge is a wrapper around a BridgeImpl that will bind low-level
25// implementation with utility code to provide high-level functions.
26type Bridge struct {
27 Name string
28 repo *cache.RepoCache
29 impl BridgeImpl
30 importer Importer
31 exporter Exporter
32 conf Configuration
33 initDone bool
34}
35
36// Register will register a new BridgeImpl
37func Register(impl BridgeImpl) {
38 if bridgeImpl == nil {
39 bridgeImpl = make(map[string]reflect.Type)
40 }
41 bridgeImpl[impl.Target()] = reflect.TypeOf(impl)
42}
43
44// Targets return all known bridge implementation target
45func Targets() []string {
46 var result []string
47
48 for key := range bridgeImpl {
49 result = append(result, key)
50 }
51
52 sort.Strings(result)
53
54 return result
55}
56
57// Instantiate a new Bridge for a repo, from the given target and name
58func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, error) {
59 implType, ok := bridgeImpl[target]
60 if !ok {
61 return nil, fmt.Errorf("unknown bridge target %v", target)
62 }
63
64 impl := reflect.New(implType).Elem().Interface().(BridgeImpl)
65
66 bridge := &Bridge{
67 Name: name,
68 repo: repo,
69 impl: impl,
70 }
71
72 return bridge, nil
73}
74
75// Instantiate a new bridge for a repo, from the combined target and name contained
76// in the full name
77func NewBridgeFromFullName(repo *cache.RepoCache, fullName string) (*Bridge, error) {
78 target, name, err := splitFullName(fullName)
79 if err != nil {
80 return nil, err
81 }
82
83 return NewBridge(repo, target, name)
84}
85
86// Attempt to retrieve a default bridge for the given repo. If zero or multiple
87// bridge exist, it fails.
88func DefaultBridge(repo *cache.RepoCache) (*Bridge, error) {
89 bridges, err := ConfiguredBridges(repo)
90 if err != nil {
91 return nil, err
92 }
93
94 if len(bridges) == 0 {
95 return nil, fmt.Errorf("no configured bridge")
96 }
97
98 if len(bridges) > 1 {
99 return nil, fmt.Errorf("multiple bridge are configured, you need to select one explicitely")
100 }
101
102 target, name, err := splitFullName(bridges[0])
103 if err != nil {
104 return nil, err
105 }
106
107 return NewBridge(repo, target, name)
108}
109
110func splitFullName(fullName string) (string, string, error) {
111 split := strings.Split(fullName, ".")
112
113 if len(split) != 2 {
114 return "", "", fmt.Errorf("bad bridge fullname: %s", fullName)
115 }
116
117 return split[0], split[1], nil
118}
119
120// ConfiguredBridges return the list of bridge that are configured for the given
121// repo
122func ConfiguredBridges(repo repository.RepoCommon) ([]string, error) {
123 configs, err := repo.ReadConfigs(bridgeConfigKeyPrefix + ".")
124 if err != nil {
125 return nil, errors.Wrap(err, "can't read configured bridges")
126 }
127
128 re, err := regexp.Compile(bridgeConfigKeyPrefix + `.([^.]+\.[^.]+)`)
129 if err != nil {
130 panic(err)
131 }
132
133 set := make(map[string]interface{})
134
135 for key := range configs {
136 res := re.FindStringSubmatch(key)
137
138 if res == nil {
139 continue
140 }
141
142 set[res[1]] = nil
143 }
144
145 result := make([]string, len(set))
146
147 i := 0
148 for key := range set {
149 result[i] = key
150 i++
151 }
152
153 return result, nil
154}
155
156// Remove a configured bridge
157func RemoveBridge(repo repository.RepoCommon, fullName string) error {
158 re, err := regexp.Compile(`^[^.]+\.[^.]+$`)
159 if err != nil {
160 panic(err)
161 }
162
163 if !re.MatchString(fullName) {
164 return fmt.Errorf("bad bridge fullname: %s", fullName)
165 }
166
167 keyPrefix := fmt.Sprintf("git-bug.bridge.%s", fullName)
168 return repo.RmConfigs(keyPrefix)
169}
170
171// Configure run the target specific configuration process
172func (b *Bridge) Configure() error {
173 conf, err := b.impl.Configure(b.repo)
174 if err != nil {
175 return err
176 }
177
178 b.conf = conf
179
180 return b.storeConfig(conf)
181}
182
183func (b *Bridge) storeConfig(conf Configuration) error {
184 for key, val := range conf {
185 storeKey := fmt.Sprintf("git-bug.bridge.%s.%s.%s", b.impl.Target(), b.Name, key)
186
187 err := b.repo.StoreConfig(storeKey, val)
188 if err != nil {
189 return errors.Wrap(err, "error while storing bridge configuration")
190 }
191 }
192
193 return nil
194}
195
196func (b *Bridge) ensureConfig() error {
197 if b.conf == nil {
198 conf, err := b.loadConfig()
199 if err != nil {
200 return err
201 }
202 b.conf = conf
203 }
204
205 return nil
206}
207
208func (b *Bridge) loadConfig() (Configuration, error) {
209 keyPrefix := fmt.Sprintf("git-bug.bridge.%s.%s.", b.impl.Target(), b.Name)
210
211 pairs, err := b.repo.ReadConfigs(keyPrefix)
212 if err != nil {
213 return nil, errors.Wrap(err, "error while reading bridge configuration")
214 }
215
216 result := make(Configuration, len(pairs))
217 for key, value := range pairs {
218 key := strings.TrimPrefix(key, keyPrefix)
219 result[key] = value
220 }
221
222 err = b.impl.ValidateConfig(result)
223 if err != nil {
224 return nil, errors.Wrap(err, "invalid configuration")
225 }
226
227 return result, nil
228}
229
230func (b *Bridge) getImporter() Importer {
231 if b.importer == nil {
232 b.importer = b.impl.NewImporter()
233 }
234
235 return b.importer
236}
237
238func (b *Bridge) getExporter() Exporter {
239 if b.exporter == nil {
240 b.exporter = b.impl.NewExporter()
241 }
242
243 return b.exporter
244}
245
246func (b *Bridge) ensureInit() error {
247 if b.initDone {
248 return nil
249 }
250
251 importer := b.getImporter()
252 if importer != nil {
253 err := importer.Init(b.conf)
254 if err != nil {
255 return err
256 }
257 }
258
259 exporter := b.getExporter()
260 if exporter != nil {
261 err := exporter.Init(b.conf)
262 if err != nil {
263 return err
264 }
265 }
266
267 b.initDone = true
268
269 return nil
270}
271
272func (b *Bridge) ImportAll(since time.Time) error {
273 importer := b.getImporter()
274 if importer == nil {
275 return ErrImportNotSupported
276 }
277
278 err := b.ensureConfig()
279 if err != nil {
280 return err
281 }
282
283 err = b.ensureInit()
284 if err != nil {
285 return err
286 }
287
288 return importer.ImportAll(b.repo, since)
289}
290
291func (b *Bridge) ExportAll(since time.Time) error {
292 exporter := b.getExporter()
293 if exporter == nil {
294 return ErrExportNotSupported
295 }
296
297 err := b.ensureConfig()
298 if err != nil {
299 return err
300 }
301
302 err = b.ensureInit()
303 if err != nil {
304 return err
305 }
306
307 return exporter.ExportAll(b.repo, since)
308}