This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
/
repo.go
157 lines (121 loc) · 3.84 KB
/
repo.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package repo
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2021 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"errors"
"regexp"
"strings"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Info contains basic information about repository
type Info struct {
User string
Name string
Path string
Target string
}
// ////////////////////////////////////////////////////////////////////////////////// //
var userValidationRegExp = regexp.MustCompile(`[A-Za-z0-9_-]{2,}`)
var nameValidationRegExp = regexp.MustCompile(`[A-Za-z0-9_.-]{2,}`)
var pathValidationRegExp = regexp.MustCompile(`[A-Za-z0-9_.-/]{0,}`)
// ////////////////////////////////////////////////////////////////////////////////// //
// ParsePath parses given path to repo struct
func ParsePath(path string) (*Info, error) {
if strings.Contains(path, ".git") {
path = strings.Replace(path, ".git", "", -1)
}
pathSlice := strings.Split(path, "/")
if len(pathSlice) <= 1 {
return nil, errors.New("Unsupported URL pattern")
}
var repoUser, repoName, repoTarget, repoPath string
// Check short notation (pkg.re/mgo or pkg.re/mgo.v1)
if strings.Contains(pathSlice[1], ".") || len(pathSlice) == 2 {
repoUser = ""
repoName = pathSlice[1]
if len(pathSlice) > 2 {
repoPath = strings.Join(pathSlice[2:], "/")
}
} else {
repoUser = pathSlice[1]
repoName = pathSlice[2]
if len(pathSlice) > 3 {
repoPath = strings.Join(pathSlice[3:], "/")
}
}
dotIndex := strings.Index(repoName, ".")
if dotIndex != -1 {
repoTarget = repoName[dotIndex+1:]
repoName = repoName[:dotIndex]
}
if repoUser != "" {
if !userValidationRegExp.MatchString(repoUser) {
return nil, errors.New("Repo username is not valid")
}
}
if repoName != "" {
if !nameValidationRegExp.MatchString(repoName) {
return nil, errors.New("Repo name is not valid")
}
}
if repoPath != "" {
if !pathValidationRegExp.MatchString(repoPath) {
return nil, errors.New("Repo sub-path is not valid")
}
}
return &Info{
User: repoUser,
Name: repoName,
Path: repoPath,
Target: repoTarget,
}, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// GitHubRoot returns GitHub root path e.g. github.com/user/project
func (i *Info) GitHubRoot() string {
if i.User == "" {
return "github.com/go-" + i.Name + "/" + i.Name
}
return "github.com/" + i.User + "/" + i.Name
}
// GitHubURL returns URL of repository on github
func (i *Info) GitHubURL(branchOrName string) string {
url := "https://" + i.GitHubRoot()
if branchOrName != "" {
url += "/tree/" + branchOrName
}
if i.Path != "" {
url += "/" + i.Path
}
return url
}
// GoDevURL returns URL of pkg.go.dev page with package documentation
func (i *Info) GoDevURL(path, branchOrName string) string {
url := "https://pkg.go.dev/pkg.re/" + path + "@" + branchOrName
if !strings.HasPrefix(branchOrName, "v1.") && !strings.HasPrefix(branchOrName, "v0.") {
url += "+incompatible"
}
return url
}
// Root returns root path for some repo e.g. user/project.target
func (i *Info) Root() string {
var target = ""
if i.Target != "" {
target = "." + i.Target
}
if i.User == "" {
return i.Name + target
}
return i.User + "/" + i.Name + target
}
// FullPath returns full path e.g. user/project.target/some/part
func (i *Info) FullPath() string {
if i.Path != "" {
return i.Root() + "/" + i.Path
}
return i.Root()
}