-
Notifications
You must be signed in to change notification settings - Fork 12
/
init.go
65 lines (56 loc) · 1.78 KB
/
init.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
package giturl
import (
"fmt"
giturl "github.com/chainguard-dev/git-urls"
azureparserv1 "github.com/kubescape/go-git-url/azureparser/v1"
bitbucketparserv1 "github.com/kubescape/go-git-url/bitbucketparser/v1"
githubparserv1 "github.com/kubescape/go-git-url/githubparser/v1"
gitlabparserv1 "github.com/kubescape/go-git-url/gitlabparser/v1"
)
// NewGitURL get instance of git parser
func NewGitURL(fullURL string) (IGitURL, error) {
hostUrl, err := getHost(fullURL)
if err != nil {
return nil, err
}
if githubparserv1.IsHostGitHub(hostUrl) {
return githubparserv1.NewGitHubParserWithURL(fullURL)
}
if azureparserv1.IsHostAzure(hostUrl) {
return azureparserv1.NewAzureParserWithURL(fullURL)
}
if bitbucketparserv1.IsHostBitBucket(hostUrl) {
return bitbucketparserv1.NewBitBucketParserWithURL(fullURL)
}
if gitlabparserv1.IsHostGitLab(hostUrl) {
return gitlabparserv1.NewGitLabParserWithURL(hostUrl, fullURL)
}
return nil, fmt.Errorf("repository host '%s' not supported", hostUrl)
}
// NewGitAPI get instance of git api
func NewGitAPI(fullURL string) (IGitAPI, error) {
hostUrl, err := getHost(fullURL)
if err != nil {
return nil, err
}
if githubparserv1.IsHostGitHub(hostUrl) {
return githubparserv1.NewGitHubParserWithURL(fullURL)
}
if gitlabparserv1.IsHostGitLab(hostUrl) {
return gitlabparserv1.NewGitLabParserWithURL(hostUrl, fullURL)
}
if azureparserv1.IsHostAzure(hostUrl) {
return azureparserv1.NewAzureParserWithURL(fullURL)
}
if bitbucketparserv1.IsHostBitBucket(hostUrl) {
return bitbucketparserv1.NewBitBucketParserWithURL(fullURL)
}
return nil, fmt.Errorf("repository host '%s' not supported", hostUrl)
}
func getHost(fullURL string) (string, error) {
parsedURL, err := giturl.Parse(fullURL)
if err != nil {
return "", err
}
return parsedURL.Host, nil
}