-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
70 lines (61 loc) · 1.58 KB
/
address.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
package git
import (
"net/url"
"regexp"
"strings"
"github.com/cardil/deviate/pkg/errors"
)
var (
// ErrInvalidAddress when an invalid address is provided to ParseAddress func.
ErrInvalidAddress = errors.New("invalid address")
// See: https://regex101.com/r/IXFQX6/6
gitAddressRe = regexp.MustCompile(`^(?:([^:]+)://)?(?:([^@]+)@)?([^:]+):(.+?)(?:\.([a-z0-9]+))?$`)
)
// ParseAddress of a GIT remote URL.
func ParseAddress(address string) (*Address, error) {
u, err := url.Parse(address)
if err == nil {
addr := &Address{
Type: AddressTypeHTTP,
Protocol: u.Scheme,
User: u.User.String(),
Host: u.Host,
Path: strings.TrimPrefix(u.Path, "/"),
}
const dot = "."
dotSplit := strings.Split(addr.Path, dot)
if len(dotSplit) > 1 {
addr.Ext = dotSplit[len(dotSplit)-1]
addr.Path = strings.Join(dotSplit[:len(dotSplit)-1], dot)
}
return addr, nil
}
if matches := gitAddressRe.FindStringSubmatch(address); matches != nil {
return &Address{
Type: AddressTypeGit,
Protocol: matches[1],
User: matches[2],
Host: matches[3],
Path: matches[4],
Ext: matches[5],
}, nil
}
return nil, errors.Wrap(err, ErrInvalidAddress)
}
// AddressType is a type of GIT remote address.
type AddressType int
const (
// AddressTypeGit is a type of GIT address.
AddressTypeGit AddressType = iota
// AddressTypeHTTP is a type of HTTP address.
AddressTypeHTTP
)
// Address represents a GIT remote address.
type Address struct {
Type AddressType
Protocol string
User string
Host string
Path string
Ext string
}