/
source.go
99 lines (85 loc) · 2.5 KB
/
source.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
package cmd
import (
"fmt"
"net/url"
"strings"
"github.com/creativeprojects/go-selfupdate"
)
// SplitDomainSlug tries to make sense of the repository string
// and returns a domain name (if present) and a slug.
//
// Example of valid entries:
//
// - "owner/name"
// - "github.com/owner/name"
// - "http://github.com/owner/name"
func SplitDomainSlug(repo string) (domain, slug string, err error) {
// simple case first => only a slug
parts := strings.Split(repo, "/")
if len(parts) == 2 {
if parts[0] == "" || parts[1] == "" {
return "", "", fmt.Errorf("invalid slug or URL %q", repo)
}
return "", repo, nil
}
// trim trailing /
repo = strings.TrimSuffix(repo, "/")
if !strings.HasPrefix(repo, "http") && !strings.Contains(repo, "://") && !strings.HasPrefix(repo, "/") {
// add missing scheme
repo = "https://" + repo
}
repoURL, err := url.Parse(repo)
if err != nil {
return "", "", err
}
// make sure hostname looks like a real domain name
if !strings.Contains(repoURL.Hostname(), ".") {
return "", "", fmt.Errorf("invalid domain name %q", repoURL.Hostname())
}
domain = repoURL.Scheme + "://" + repoURL.Host
slug = strings.TrimPrefix(repoURL.Path, "/")
if slug == "" {
return "", "", fmt.Errorf("invalid URL %q", repo)
}
return domain, slug, nil
}
func GetSource(cvsType, domain string) (selfupdate.Source, error) {
if cvsType != "auto" && cvsType != "" {
source, err := getSourceFromName(cvsType, domain)
if err != nil {
return nil, err
}
return source, nil
}
source, err := getSourceFromURL(domain)
if err != nil {
return nil, err
}
return source, nil
}
func getSourceFromName(name, domain string) (selfupdate.Source, error) {
switch name {
case "gitea":
return selfupdate.NewGiteaSource(selfupdate.GiteaConfig{BaseURL: domain})
case "gitlab":
return selfupdate.NewGitLabSource(selfupdate.GitLabConfig{BaseURL: domain})
default:
return newGitHubSource(domain)
}
}
func getSourceFromURL(domain string) (selfupdate.Source, error) {
if strings.Contains(domain, "gitea") {
return selfupdate.NewGiteaSource(selfupdate.GiteaConfig{BaseURL: domain})
}
if strings.Contains(domain, "gitlab") {
return selfupdate.NewGitLabSource(selfupdate.GitLabConfig{BaseURL: domain})
}
return newGitHubSource(domain)
}
func newGitHubSource(domain string) (*selfupdate.GitHubSource, error) {
config := selfupdate.GitHubConfig{}
if domain != "" && !strings.HasSuffix(domain, "://github.com") {
config.EnterpriseBaseURL = domain
}
return selfupdate.NewGitHubSource(config)
}