-
Notifications
You must be signed in to change notification settings - Fork 265
/
mod_ref.go
53 lines (46 loc) · 1.05 KB
/
mod_ref.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
package mod_installer
import (
"fmt"
"strings"
goVersion "github.com/hashicorp/go-version"
)
// ModRef is a struct to represent an unresolved mod reference
type ModRef struct {
// the Git URL of the mod repo
Name string
// the version constraint of the mod
versionConstraint *goVersion.Version
// the branch to use
branch string
// the local file location to use
filePath string
// raw reference
raw string
}
func NewModRef(modRef string) (*ModRef, error) {
split := strings.Split(modRef, "@")
if len(split) > 2 {
return nil, fmt.Errorf("invalid mod ref %s", modRef)
}
res := &ModRef{
raw: modRef,
Name: split[0],
}
if len(split) == 2 {
res.setVersion(split[1])
}
return res, nil
}
func (r *ModRef) setVersion(versionString string) {
if strings.HasPrefix(versionString, "file:") {
r.filePath = versionString
return
}
// does the verison parse as a semver version
if v, err := goVersion.NewVersion(versionString); err == nil {
r.versionConstraint = v
return
}
// otherwise assume it is a branch
r.branch = versionString
}