-
Notifications
You must be signed in to change notification settings - Fork 875
/
parse.go
255 lines (228 loc) · 6.25 KB
/
parse.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
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"net/url"
"strings"
"github.com/pkg/errors"
)
// TypeLocal represents github
const TypeLocal = "local"
// TypeOss represent oss
const TypeOss = "oss"
// TypeGithub represents github
const TypeGithub = "github"
// TypeGitee represents gitee
const TypeGitee = "gitee"
// TypeGitlab represents gitlab
const TypeGitlab = "gitlab"
// TypeUnknown represents parse failed
const TypeUnknown = "unknown"
// Content contains different type of content needed when building Registry
type Content struct {
OssContent
GithubContent
GiteeContent
GitlabContent
LocalContent
}
// LocalContent for local registry
type LocalContent struct {
AbsDir string `json:"abs_dir"`
}
// OssContent for oss registry
type OssContent struct {
EndPoint string `json:"bucket_url"`
Bucket string `json:"bucket"`
}
// GithubContent for cap center
type GithubContent struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Path string `json:"path"`
Ref string `json:"ref"`
}
// GiteeContent for cap center
type GiteeContent struct {
Owner string `json:"gitee_owner"`
Repo string `json:"gitee_repo"`
Path string `json:"gitee_path"`
Ref string `json:"gitee_ref"`
}
// GitlabContent for cap center
type GitlabContent struct {
Host string `json:"gitlab_host"`
Owner string `json:"gitlab_owner"`
Repo string `json:"gitlab_repo"`
Path string `json:"gitlab_path"`
Ref string `json:"gitlab_ref"`
PId int `json:"gitlab_pid"`
}
// Parse will parse config from address
func Parse(addr string) (string, *Content, error) {
URL, err := url.Parse(addr)
if err != nil {
return "", nil, err
}
l := strings.Split(strings.TrimPrefix(URL.Path, "/"), "/")
switch URL.Scheme {
case "http", "https":
switch URL.Host {
case "github.com":
// We support two valid format:
// 1. https://github.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
// 2. https://github.com/<owner>/<repo>/<path-to-dir>
if len(l) < 3 {
return "", nil, errors.New("invalid format " + addr)
}
if l[2] == "tree" {
// https://github.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
if len(l) < 5 {
return "", nil, errors.New("invalid format " + addr)
}
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[4:], "/"),
Ref: l[3],
},
}, nil
}
// https://github.com/<owner>/<repo>/<path-to-dir>
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[2:], "/"),
Ref: "", // use default branch
},
},
nil
case "api.github.com":
if len(l) != 5 {
return "", nil, errors.New("invalid format " + addr)
}
//https://api.github.com/repos/<owner>/<repo>/contents/<path-to-dir>
return TypeGithub, &Content{
GithubContent: GithubContent{
Owner: l[1],
Repo: l[2],
Path: l[4],
Ref: URL.Query().Get("ref"),
},
},
nil
case "gitee.com":
// We support two valid format:
// 1. https://gitee.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
// 2. https://gitee.com/<owner>/<repo>/<path-to-dir>
if len(l) < 3 {
return "", nil, errors.New("invalid format " + addr)
}
switch l[2] {
case "tree":
// https://gitee.com/<owner>/<repo>/tree/<branch>/<path-to-dir>
if len(l) < 5 {
return "", nil, errors.New("invalid format " + addr)
}
return TypeGitee, &Content{
GiteeContent: GiteeContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[4:], "/"),
Ref: l[3],
},
}, nil
default:
// https://gitee.com/<owner>/<repo>/<path-to-dir>
return TypeGitee, &Content{
GiteeContent: GiteeContent{
Owner: l[0],
Repo: l[1],
Path: strings.Join(l[2:], "/"),
Ref: "", // use default branch
},
}, nil
}
default:
return "", nil, fmt.Errorf("git type repository only support github for now")
}
case "oss":
return TypeOss, &Content{
OssContent: OssContent{
EndPoint: URL.Host,
Bucket: URL.Path,
},
}, nil
case "file":
return TypeLocal, &Content{
LocalContent: LocalContent{
AbsDir: URL.Path,
},
}, nil
}
return TypeUnknown, nil, nil
}
// ByteCountIEC convert number of bytes into readable string
// borrowed from https://yourbasic.org/golang/formatting-byte-size-to-human-readable-format/
func ByteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
// ParseGitlab will parse gitlab config from address
func ParseGitlab(addr, repo string) (string, *Content, error) {
if !strings.Contains(addr, repo) {
return "", nil, errors.New("addon registry repo name invalid")
}
// We support two valid format:
// 1. https://example.gitlab.com/<owner>/<repo>
// 2. https://example.gitlab.com/<owner>/<repo>/tree/<branch>
URL, err := url.Parse(addr)
if err != nil {
return "", nil, err
}
arr := strings.Split(addr, repo)
owner := strings.Split(arr[0], URL.Host+"/")
if !strings.Contains(arr[1], "/") {
// https://example.gitlab.com/<owner>/<repo>
return TypeGitlab, &Content{
GitlabContent: GitlabContent{
Host: URL.Scheme + "://" + URL.Host,
Owner: owner[1][:len(owner[1])-1],
Repo: repo,
Ref: "", // use default branch
},
}, nil
}
// https://example.gitlab.com/<owner>/<repo>/tree/<branch>
l := strings.Split(arr[1], "/")
return TypeGitlab, &Content{
GitlabContent: GitlabContent{
Host: URL.Scheme + "://" + URL.Host,
Owner: owner[1][:len(owner[1])-1],
Repo: repo,
Ref: l[2],
},
}, nil
}