forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
58 lines (52 loc) · 1.73 KB
/
repos.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
package main
import (
"fmt"
"github.com/forj-oss/forjj-modules/trace"
"log"
"strings"
)
// DefineDefaultUpstream will set a Defaultupstream if default is not set.
// In that case, if we have no upstream app or 2 or more upstream apps, an error will be returned.
func (a *Forj) DefineDefaultUpstream() error {
instances := []string{}
for instance, app := range a.f.Apps() {
if app.Type == "upstream" {
instances = append(instances, instance)
}
}
found_instances := len(instances)
switch {
case found_instances == 0 :
return fmt.Errorf("Unable to determine a default upstream instance. No upstream application found.")
case found_instances >1 :
return fmt.Errorf("Unable to determine one default upstream instance. " +
"Found '%s'. You must define one in Forjfile:/settings/default/upstream-instance.",
strings.Join(instances, "', '"))
default:
a.f.Set("forjj", "settings", "default-repo-apps", "upstream", instances[0])
log.Printf("Set default upstream application to '%s'", instances[0])
}
return nil
}
// GetReposRequestedFor Identify number of repository requested for an instance.
func (a *Forj) GetReposRequestedFor(instance, action string) (num int) {
if instance == "" || action == "" {
gotrace.Trace("Internal error: instance and action cannot be empty.")
return
}
for _, data := range a.cli.GetObjectValues("repo") {
if v, _, _ := data.Get("instance"); v == instance || (v == "" && instance == a.o.Defaults["instance"]) {
num++
}
}
return
}
func NumDisplay(num int, format, elements, element string) string {
if num > 1 {
return fmt.Sprintf(format, num, elements)
}
return fmt.Sprintf(format, num, element)
}
func NumReposDisplay(num int) string {
return NumDisplay(num, "%d repositor%s", "ies", "y")
}