File: provider.go

package info (click to toggle)
emd 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 260 kB
  • sloc: sh: 159; makefile: 4
file content (116 lines) | stat: -rw-r--r-- 2,264 bytes parent folder | download | duplicates (2)
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
package std

import (
	"regexp"
	"strings"
)

var re = regexp.MustCompile("^([^/]+)[.](com|org|net)/.*")

// Provider detects GH url
type Provider struct {
	URL         string
	providerURL string
	providerID  string
}

// New provider to work with url
func New() *Provider {
	return &Provider{}
}

// Is the current url about gh?
func (p *Provider) Is(url string) bool {
	x := cleanURL(url)
	return re.MatchString(x)
}

// SetURL of the provider
func (p *Provider) SetURL(url string) {
	p.URL = cleanURL(url)
	parts := strings.Split(p.URL, "/")
	p.providerURL = parts[0]
	parts2 := re.FindAllStringSubmatch(p.URL, -1)
	if len(parts2) > 0 {
		p.providerID = parts2[0][1]
	}
}

func cleanURL(url string) string {
	if url[:4] == "http" {
		url = url[4:]
	} else if url[:5] == "https" {
		url = url[5:]
	}
	if url[:3] == "://" {
		url = url[3:]
	}
	if url[:1] == "/" {
		url = url[1:]
	}
	return url
}

// GetUserName of the the current url.
func (p *Provider) GetUserName() string {
	ss := strings.Split(p.URL, "/")
	if len(ss) > 1 {
		return ss[1]
	}
	return ""
}

// GetProjectName of the the current url.
func (p *Provider) GetProjectName() string {
	ss := strings.Split(p.URL, "/")
	if len(ss) > 2 {
		return ss[2]
	}
	return ""
}

// GetProjectPath of the the current url.
func (p *Provider) GetProjectPath() string {
	ss := strings.Split(p.URL, "/")
	if len(ss) > 3 {
		return "/" + strings.Join(ss[3:], "/")
	}
	return ""
}

// GetProjectURL of the the current url.
func (p *Provider) GetProjectURL() string {
	ret := []string{}
	if x := p.GetProviderURL(); x != "" {
		if y := p.GetUserName(); y != "" {
			if z := p.GetProjectName(); z != "" {
				ret = append(ret, x)
				ret = append(ret, y)
				ret = append(ret, z)
			}
		}
	}
	return strings.Join(ret, "/")
}

// GetURL of the the current url.
func (p *Provider) GetURL() string {
	ret := []string{}
	if x := p.GetProjectURL(); x != "" {
		ret = append(ret, x)
		if y := p.GetProjectPath(); y != "" {
			ret = append(ret, y[1:]) //rm front /
		}
	}
	return strings.Join(ret, "/")
}

// GetProviderURL of the the current url.
func (p *Provider) GetProviderURL() string {
	return p.providerURL
}

// GetProviderID of the the current url.
func (p *Provider) GetProviderID() string {
	return p.providerID
}