-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
modulesInfo.go
48 lines (40 loc) · 1012 Bytes
/
modulesInfo.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
package velox
import (
"fmt"
"regexp"
"strconv"
"time"
m "golang.org/x/mod/module"
)
// ModulesInfo represents single go module
type ModulesInfo struct {
// Version - commit sha or tag
Version string
// PseudoVersion - Go pseudo version
PseudoVersion string
// module name - eg: github.com/roadrunner-server/logger/v2
ModuleName string
// Replace (for the local dev)
Replace string
}
var vr = regexp.MustCompile(`/v(\d+)$`)
// ParseModuleInfo here we accept a module name and return the version
// e.g.: github.com/roadrunner-server/logger/v2 => v2
func ParseModuleInfo(module string, t time.Time, rev string) string {
match := vr.FindStringSubmatch(module)
var version string
if len(match) > 1 {
if !IsDigit(match[1]) {
return m.PseudoVersion("", "", t, rev)
}
version = fmt.Sprintf("v%s", match[1])
}
return m.PseudoVersion(version, "", t, rev)
}
func IsDigit(num string) bool {
if num == "" {
return false
}
_, err := strconv.ParseInt(num, 10, 64)
return err == nil
}