/
repo.go
640 lines (583 loc) · 15.2 KB
/
repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package forjfile
import (
"fmt"
"forjj/drivers"
"forjj/rules"
"forjj/sources_info"
"strings"
"github.com/forj-oss/forjj-modules/trace"
"github.com/forj-oss/goforjj"
)
type RepoStruct struct {
name string
is_infra bool
isDeploy bool
isCurrentDeploy bool
forge *ForgeYaml
owner string
driverOwner *drivers.Driver
deployment string // set to deploiment name where the repo is planned to be deployed.
Deployment string `yaml:"deploy-repo-of,omitempty"`
Upstream string `yaml:"upstream-app,omitempty"` // Name of the application upstream hosting this repository.
GitRemote string `yaml:"git-remote,omitempty"`
remote goforjj.PluginRepoRemoteUrl // Git remote string to use/set
Title string `yaml:",omitempty"`
RepoTemplate string `yaml:"repo-template,omitempty"`
Flow RepoFlow `yaml:",omitempty"`
More map[string]string `yaml:",inline"`
apps map[string]*AppStruct // List of applications connected to this repo. Defaults are added automatically.
Apps map[string]string `yaml:"in-relation-with,omitempty"` // key: <AppRelName>, value: <appName>
sources *sourcesinfo.Sources
}
const (
FieldRepoName = "name"
FieldRepoUpstream = "upstream"
FieldRepoApps = "apps"
FieldRepoGitRemote = "git-remote"
FieldRepoRemote = "remote"
FieldRepoRemoteURL = "remote-url"
FieldRepoTitle = "title"
FieldRepoFlow = "flow"
FieldRepoTemplate = "repo-template"
FieldRepoDeployName = "deployment-name"
FieldRepoDeployType = "deployment-type"
FieldRepoRole = "role"
FieldCurrentDeployRepo = "current-deployment-repo"
)
// Register will register the repository
func (r *RepoStruct) Register() {
if r == nil {
return
}
if r.forge == nil {
return
}
r.forge.ForjCore.Repos[r.name] = r
}
// Flags provide the list of existing keys in a repo object.
func (r *RepoStruct) Flags() (flags []string) {
if r == nil {
flags = make([]string, 0)
return
}
coreList := []string{
FieldRepoName,
FieldRepoUpstream,
FieldRepoGitRemote,
FieldRepoRemote,
FieldRepoRemoteURL,
FieldRepoTitle,
FieldRepoFlow,
FieldRepoTemplate,
FieldRepoDeployName,
FieldRepoRole,
FieldCurrentDeployRepo,
}
flags = make([]string, len(coreList), len(coreList)+len(r.More))
for index, name := range coreList {
flags[index] = name
}
for k := range r.More {
flags = append(flags, k)
}
return
}
func (r *RepoStruct) mergeFrom(from *RepoStruct) *RepoStruct {
if r == nil {
return from
}
for _, flag := range from.Flags() {
if v, found, source := from.Get(flag); found {
r.Set(source, flag, v.GetString())
}
}
return r
}
type RepoFlow struct {
Name string
objects map[string]map[string]string
}
func (r *RepoStruct) Model() RepoModel {
model := RepoModel{
Apps: make(map[string]RepoAppModel),
}
model.From(r)
if r == nil {
return model
}
for appRelName, app := range r.apps {
repoModel := RepoAppModel{
AppName: app.name,
}
if _, found := r.Apps[appRelName]; !found {
repoModel.Default = true
}
model.Apps[appRelName] = repoModel
}
return model
}
func (r *RepoStruct) Owner() string {
if r == nil {
return ""
}
return r.owner
}
func (r *RepoStruct) setFromInfra(infra *RepoStruct) {
if r == nil {
return
}
infra.is_infra = true
infra.isDeploy = false
*r = *infra
delete(r.More, "name")
}
func (r *RepoStruct) GetString(field string) (_ string, _ string) {
if r == nil {
return
}
if v, found, source := r.Get(field); found {
return v.GetString(), source
}
return
}
func (r *RepoStruct) RemoteUrl() string {
if r == nil {
return ""
}
return r.remote.Url
}
func (r *RepoStruct) RemoteGit() string {
if r == nil {
return ""
}
return r.remote.Ssh
}
// Get return the value for a field.
func (r *RepoStruct) Get(field string) (value *goforjj.ValueStruct, found bool, source string) {
if r == nil {
return
}
source = r.sources.Get(field)
switch fieldSel := strings.Split(field, ":"); fieldSel[0] {
case FieldRepoName:
value, found = value.SetIfFound(r.name, (r.name != ""))
case FieldRepoApps, FieldRepoUpstream:
if field == "upstream" {
gotrace.Warning("*RepoStruct.Get(): Field '%s' is obsolete. Change the code to use 'apps:upstream'.", field)
} else if len(fieldSel) > 1 {
field = fieldSel[1]
}
if v, found2 := r.apps[field]; found2 {
value, found = value.SetIfFound(v.name, v != nil && v.name != "")
return
}
if v, found2 := r.Apps[field]; found2 {
value, found = value.SetIfFound(v, found2 && (v != ""))
return
}
if field == "upstream" && r.Upstream != "" {
gotrace.Warning("the '%s' in /repositories/%s is obsolete. Define it as /repositories/%s/apps/%s", field,
r.name, r.name, field)
value, found = value.SetIfFound(r.Upstream, (r.Upstream != ""))
return
}
value, found = value.SetIfFound("", false)
case FieldRepoGitRemote:
value, found = value.SetIfFound(r.GitRemote, (r.GitRemote != ""))
case FieldRepoRemote:
value, found = value.SetIfFound(r.remote.Ssh, (r.remote.Ssh != ""))
case FieldRepoRemoteURL:
value, found = value.SetIfFound(r.remote.Url, (r.remote.Url != ""))
case FieldRepoTitle:
value, found = value.SetIfFound(r.Title, (r.Title != ""))
case FieldRepoFlow:
value, found = value.SetIfFound(r.Flow.Name, (r.Flow.Name != ""))
case FieldRepoTemplate:
value, found = value.SetIfFound(r.RepoTemplate, (r.RepoTemplate != ""))
case FieldRepoDeployName:
value, found = value.SetIfFound(r.deployment, (r.deployment != ""))
case FieldRepoDeployType:
if v, found2 := r.forge.Deployments[r.deployment]; found2 {
value, found = value.SetIfFound(v.Type, found2)
return
}
value, found = value.SetIfFound("", false)
case FieldRepoRole:
value, found = value.SetIfFound(r.Role(), true)
case FieldCurrentDeployRepo:
if r.isCurrentDeploy {
value, found = value.SetIfFound("true", true)
return
}
value, found = value.SetIfFound("false", true)
default:
v, f := r.More[field]
value, found = value.SetIfFound(v, f)
}
return
}
// SetHandler is the generic setter function.
func (r *RepoStruct) SetHandler(source string, from func(field string) (string, bool), keys ...string) {
if r == nil {
return
}
for _, key := range keys {
if v, found := from(key); found {
r.Set(source, key, v)
}
}
}
// SetInternalRelApp will set the appName connected to the repo
// But if the Forjfile has a setup, this one will forcefully used.
// return:
// updated bool : true if the app has been updated.
// error : set if error has been found. updated is then nil.
func (r *RepoStruct) SetInternalRelApp(appRelName, appName string) (updated *bool, _ error) {
if err := r.initApp(); err != nil {
return nil, err
}
if v, found := r.Apps[appRelName]; found {
appName = v // Set always declared one.
}
if app, err := r.forge.ForjCore.Apps.Found(appName); err != nil {
return nil, fmt.Errorf("Unable to set %s:%s. %s.", appRelName, appName, err)
} else if v, found := r.apps[appRelName]; !found || (found && v.name != appName) {
r.apps[appRelName] = app
updated = new(bool)
*updated = true
}
return
}
// SetApp Define the Forjfile application name to link with the repo.
//
// return:
// updated bool : true if the app has been updated.
// error : set if error has been found. updated is then nil.
func (r *RepoStruct) SetApp(appRelName, appName string) (updated *bool, _ error) {
if err := r.initApp(); err != nil {
return nil, err
}
updated = new(bool)
if v, found := r.Apps[appRelName]; !found || (found && v != appName) {
*updated = true
}
r.Apps[appRelName] = appName
if set, err := r.SetInternalRelApp(appRelName, appName); set == nil {
return set, err
}
return
}
func (r *RepoStruct) initApp() (_ error) {
if r == nil {
return fmt.Errorf("Internal: repo object is nil.")
}
if r.forge == nil {
return fmt.Errorf("Internal: forge ref not set.")
}
if r.apps == nil {
r.apps = make(map[string]*AppStruct)
}
if r.Apps == nil {
r.Apps = make(map[string]string)
}
return
}
func (r *RepoStruct) Set(source, field, value string) {
if r == nil {
return
}
switch field_sel := strings.Split(field, ":"); field_sel[0] {
case FieldRepoName:
if r.name != value {
r.name = value
}
case FieldRepoApps:
if len(field_sel) > 1 {
r.SetApp(field_sel[1], value)
r.forge.dirty()
}
case FieldRepoUpstream:
if v, found := r.Apps["upstream"]; !found || (found && v != value) {
r.SetApp("upstream", value)
r.forge.dirty()
}
case FieldRepoGitRemote:
if r.GitRemote != value {
r.GitRemote = value
r.forge.dirty()
}
case FieldRepoRemote:
if r.remote.Ssh != value {
r.remote.Ssh = value
}
case FieldRepoRemoteURL:
if r.remote.Url != value {
r.remote.Url = value
}
case FieldRepoTemplate:
if r.RepoTemplate != value {
r.RepoTemplate = value
r.forge.dirty()
}
case FieldRepoTitle:
if r.Title != value {
r.Title = value
r.forge.dirty()
}
case FieldRepoFlow:
if r.Flow.Name != value {
r.Flow.Name = value
r.forge.dirty()
}
case FieldRepoDeployName:
if r.deployment != value {
r.deployment = value
}
case FieldRepoRole:
switch value {
case "infra":
r.is_infra = true
r.isDeploy = false
r.isCurrentDeploy = false
case "deploy":
r.is_infra = false
r.isDeploy = true
case "code":
r.is_infra = false
r.isDeploy = false
r.isCurrentDeploy = false
}
case FieldCurrentDeployRepo:
r.SetCurrentDeploy()
default:
if r.More == nil {
r.More = make(map[string]string)
}
if v, found := r.More[field]; found && value == "" {
delete(r.More, field)
r.forge.dirty()
} else {
if v != value {
r.forge.dirty()
r.More[field] = value
}
}
}
r.sources = r.sources.Set(source, field, value)
}
func (r *RepoStruct) SetInstanceOwner(owner string) {
if r == nil {
return
}
r.owner = owner
}
func (r *RepoStruct) SetPluginOwner(d *drivers.Driver) {
if r == nil {
return
}
r.driverOwner = d
}
func (r *RepoStruct) RemoteType() string {
if r == nil {
return "git"
}
if r.driverOwner == nil {
return "git"
}
return r.driverOwner.Name
}
func (r *RepoStruct) UpstreamAPIUrl() string {
if r == nil {
return ""
}
if r.driverOwner == nil {
return ""
}
return r.driverOwner.DriverAPIUrl
}
func (r *RepoStruct) set_forge(f *ForgeYaml) {
if r == nil {
return
}
r.forge = f
}
// ruleCheck implements a simple rule checker.
// It checks the rule for a key and a value. The value is returned by a get function.
//
// a rule is a string formatted with following syntax and meaning:
// - '<key>:<value>' or '<key>=<value>' - True if key has value equal to <value>. '<key>:<value>' is kept for compatibility but is obsolete.
// - '<key>!=<value>' - True if key has a value NOT equal to <value>
// - '<key>=/<regexp>/' - True if key has value respecting <regexp>.
// - '<key>!=/<regexp>/' - True if key has a value NOT respecting <regexp>
func ruleCheck(get func(string) (string, bool), rule []string) (found bool, err error) {
return
}
// HasApps return a bool if rules are all true on at least one application.
//
// Supported rules syntax are defined by rules module
// a rule is a string formatted with following syntax and meaning:
// - '<key>:<value>' or '<key>=<value>' - True if key has value equal to <value>. '<key>:<value>' is kept for compatibility but is obsolete.
// - '<key>!=<value>' - True if key has a value NOT equal to <value>
// - '<key>=/<regexp>/' - True if key has value respecting <regexp>.
// - '<key>!=/<regexp>/' - True if key has a value NOT respecting <regexp>
//
// a rule is true on an application if it has the key value set to <value>
//
// If the rule is not well formatted, an error is returned.
// If the repo has no application, HasApps return false.
// If no rules are provided and at least one application exist, HasApps return true.
// If all rules are true, HasApps return true
//
// TODO: Write Unit test of HasApps
func (r *RepoStruct) HasApps(rulesList ...string) (found bool, err error) {
if r.apps == nil {
return
}
ruleChecker := rules.NewRuleChecker()
for appRelName, app := range r.apps {
found = true
for _, rule := range rulesList {
var key string
if key, _, _, err = ruleChecker.Validate(rule); err != nil {
return
}
var ruleOk bool
if key == "appRelName" {
ruleOk, err = ruleChecker.Check(func(string) (string, bool) {
return appRelName, true
})
if err != nil {
return
}
if ruleOk {
continue
}
found = false
break
}
ruleOk, err = ruleChecker.Check(func(key string) (value string, isFound bool) {
v, found, _ := app.Get(key)
if !found {
return
}
return v.GetString(), found
})
if err != nil {
return
}
if ruleOk {
continue
}
found = false
break
}
if found {
gotrace.Trace("Found an application which meets '%s'", rulesList)
return
}
}
gotrace.Trace("NO application found which meets '%s'", rulesList)
return
}
func (r *RepoStruct) GetApps(rules ...string) (apps map[string]*AppStruct, err error) {
if r.apps == nil {
return
}
apps = make(map[string]*AppStruct)
found := false
for app_name, app := range r.apps {
found = true
for _, rule := range rules {
ruleToCheck := strings.Split(rule, ":")
if len(ruleToCheck) != 2 {
err = fmt.Errorf("rule '%s' is invalid. Format supported is '<key>:<value>'.", rule)
return
}
if ruleToCheck[0] == "appRelName" {
if app_name == ruleToCheck[0] {
continue
}
found = false
break
}
v, found2, _ := app.Get(ruleToCheck[0])
if ruleToCheck[1] == "*" {
if found2 {
continue
}
found = false
break
} else if found2 && v.GetString() != ruleToCheck[1] {
found = false
break
}
}
if found {
apps[app_name] = app
}
}
return
}
func (r *RepoStruct) HasValues(rules ...string) (found bool, err error) {
found = true
for _, rule := range rules {
ruleToCheck := strings.Split(rule, ":")
if len(ruleToCheck) != 2 {
err = fmt.Errorf("rule '%s' is invalid. Format supported is '<key>:<value>'.", rule)
return
}
if v, found2, _ := r.Get(ruleToCheck[0]); found2 && v.GetString() != ruleToCheck[1] {
found = false
break
}
}
if found {
return
}
return
}
func (r *RepoStruct) IsInfra() bool {
if r == nil {
return false
}
return r.is_infra
}
func (r *RepoStruct) Role() string {
switch {
case r.is_infra:
return "infra"
case r.isDeploy:
return "deploy"
default:
return "code"
}
}
// Name return the internal reponame.
func (r *RepoStruct) Name() string {
if r == nil {
return ""
}
return r.name
}
// IsCurrentDeploy return True if this repo is the current deployment repository
func (r *RepoStruct) IsCurrentDeploy() (ret bool) {
if r == nil {
return
}
return r.isCurrentDeploy && r.isDeploy
}
// SetCurrentDeploy set True if this repo IS the current deployment repository
// No check made on other repositories.
// At a time, only one repository should be considered as the current deployment repository
func (r *RepoStruct) SetCurrentDeploy() {
if r == nil {
return
}
if !r.isDeploy {
return
}
r.isCurrentDeploy = true
}
func (r *RepoStruct) AttachedToDeployment() string {
return r.deployment
}