-
Notifications
You must be signed in to change notification settings - Fork 22
/
repository.go
131 lines (109 loc) · 2.64 KB
/
repository.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
package repository
import (
"fmt"
"strings"
"github.com/megamsys/vertice/meta"
)
const (
//source flags
GITHUB = "github"
GITLAB = "gitlab"
DOCKERHUB = "dockerhub"
MYDOCKERHUB = "mydockerhub"
defaultManager = GITHUB
CIHOOK = "CI"
TYPE = "type"
TOKEN = "token"
USERNAME = "username"
STATUS = "unbound"
// IMAGE indicates that the repo is an image
IMAGE = "image"
// Git indicates that the repo is a GIT
GIT = "git"
// oneclick indicates that an oneclick image exists
ONECLICK = "oneclick"
)
var managers map[string]RepositoryManager
/* Repository represents a repository managed by the manager. */
type Repo struct {
Type string
Source string
OneClick bool
URL string
Hook *Hook
}
type Hook struct {
Enabled bool
Token string
UserName string
CartonId string
BoxId string
}
func (r Repo) GetType() string {
return r.Type
}
func (r Repo) GetSource() string {
return r.Source
}
func (r Repo) Gitr() string {
return r.URL
}
func (r Repo) IsEnabled() bool {
return r.Hook != nil && r.Hook.Enabled
}
func (r Repo) GetToken() string {
return r.Hook.Token
}
func (r Repo) GetUserName() string {
return r.Hook.UserName
}
//Check on CartonId, BoxId if it exists (r.Hook.BoxId)
func (r Repo) Trigger() string {
return meta.MC.Api + "/assembly/upgrade/" + r.Hook.CartonId
}
func (r Repo) GetShortName() (string, error) {
i := strings.LastIndex(r.Gitr(), "/")
if i < 0 {
return "", fmt.Errorf("unable to parse output of git")
}
return strings.TrimRight(r.Gitr()[i+1:], ".git"), nil
}
//This shall be under type Tosca {} and a global method
func ForImageName(fullTosca string, version string) string {
img := fullTosca[strings.LastIndex(fullTosca, ".")+1:]
if len(strings.TrimSpace(version)) > 1 {
return img + "_" + version
}
return img
}
type Repository interface {
IsEnabled() bool
GetSource() string
GetType() string
GetToken() string
GetUserName() string
Gitr() string
Trigger() string
GetShortName() (string, error)
}
// RepositoryManager represents a manager of application repositories.
type RepositoryManager interface {
CreateHook(r Repository) (string, error)
RemoveHook(r Repository) error
}
// Manager returns the current configured manager, as defined in the
// configuration file.
func Manager(managerName string) RepositoryManager {
if _, ok := managers[managerName]; !ok {
managerName = "nop"
}
return managers[managerName]
}
// Register registers a new repository manager, that can be later configured
// and used.
func Register(name string, manager RepositoryManager) {
if managers == nil {
managers = make(map[string]RepositoryManager)
}
managers[name] = manager
}