-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrepository_slug.go
60 lines (53 loc) · 1.2 KB
/
repository_slug.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
package selfupdate
import (
"strings"
)
type RepositorySlug struct {
owner string
repo string
}
// Repository interface
var _ Repository = RepositorySlug{}
// ParseSlug is used to take a string "owner/repo" to make a RepositorySlug
func ParseSlug(slug string) RepositorySlug {
var owner, repo string
couple := strings.Split(slug, "/")
if len(couple) != 2 {
// give it another try
couple = strings.Split(slug, "%2F")
}
if len(couple) == 2 {
owner = couple[0]
repo = couple[1]
}
return RepositorySlug{
owner: owner,
repo: repo,
}
}
// NewRepositorySlug creates a RepositorySlug from owner and repo parameters
func NewRepositorySlug(owner, repo string) RepositorySlug {
return RepositorySlug{
owner: owner,
repo: repo,
}
}
func (r RepositorySlug) GetSlug() (string, string, error) {
if r.owner == "" && r.repo == "" {
return "", "", ErrInvalidSlug
}
if r.owner == "" {
return r.owner, r.repo, ErrIncorrectParameterOwner
}
if r.repo == "" {
return r.owner, r.repo, ErrIncorrectParameterRepo
}
return r.owner, r.repo, nil
}
func (r RepositorySlug) Get() (interface{}, error) {
_, _, err := r.GetSlug()
if err != nil {
return "", err
}
return r.owner + "/" + r.repo, nil
}