-
Notifications
You must be signed in to change notification settings - Fork 19
/
repo.go
209 lines (184 loc) · 5.41 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package versionutils
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/solo-io/go-utils/versionutils/dep"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
const (
gopkgToml = "Gopkg.toml"
constraint = "constraint"
override = "override"
nameConst = "name"
versionConst = "version"
revisionConst = "revision"
branchConst = "branch"
)
var (
UnableToFindVersionInTomlError = func(pkgName string) error {
return fmt.Errorf("unable to find version for %s in toml", pkgName)
}
FailedCommandError = func(err error, args []string, output string) error {
return errors.Wrapf(err, "%v failed: %s", args, output)
}
)
// Deprecated: use git.PinDependencyVersion
// This function prepends a "v" to the semver and then tries to 'git checkout' the resulting tag in the given directory.
func PinGitVersion(relativeRepoDir string, semVerVersion string) error {
tag := GetTag(semVerVersion)
cmd := exec.Command("git", "checkout", tag)
cmd.Dir = relativeRepoDir
buf := &bytes.Buffer{}
out := io.MultiWriter(buf, os.Stdout)
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return FailedCommandError(err, cmd.Args, buf.String())
}
return nil
}
// Deprecated: use git.GetGitRefInfo
func GetGitVersion(relativeRepoDir string) (string, error) {
cmd := exec.Command("git", "describe", "--tags", "--dirty")
cmd.Dir = relativeRepoDir
output, err := cmd.Output()
if err != nil {
return "", FailedCommandError(err, cmd.Args, string(output))
}
return strings.TrimSpace(string(output)), nil
}
// Deprecated: use git.AppendTagPrefix
func GetTag(version string) string {
if strings.HasPrefix(version, "v") {
return version
}
return "v" + version
}
func GetVersionFromTag(shouldBeAVersion string) (string, error) {
definiteTag := GetTag(shouldBeAVersion)
version, err := ParseVersion(definiteTag)
if err != nil {
return "", err
}
versionString := version.String()
return versionString[1:], nil
}
// Deprecated: Use GetTomlVersion instead
func GetVersion(pkgName string, tomlTree []*toml.Tree) (string, error) {
for _, v := range tomlTree {
if version, found := getVersionInfoFromTree(v, pkgName); found {
return version.Version, nil
}
}
return "", UnableToFindVersionInTomlError(pkgName)
}
// Deprecated: use GetDependencyVersionInfo
func GetTomlVersion(pkgName string, toml *TomlWrapper) (string, error) {
for _, v := range toml.Overrides {
if version, found := getVersionInfoFromTree(v, pkgName); found {
return version.Version, nil
}
}
for _, v := range toml.Constraints {
if version, found := getVersionInfoFromTree(v, pkgName); found {
return version.Version, nil
}
}
return "", UnableToFindVersionInTomlError(pkgName)
}
// Returns the version of the given package together with the type of version identifier, i.e. revision, version, branch.
func GetDependencyVersionInfo(pkgName string, toml *TomlWrapper) (*dep.VersionInfo, error) {
for _, v := range toml.Overrides {
if version, found := getVersionInfoFromTree(v, pkgName); found {
return version, nil
}
}
for _, v := range toml.Constraints {
if version, found := getVersionInfoFromTree(v, pkgName); found {
return version, nil
}
}
return nil, UnableToFindVersionInTomlError(pkgName)
}
// Deprecated: Use ParseFullToml instead
func ParseToml() ([]*toml.Tree, error) {
return ParseTomlFromDir("")
}
// Deprecated: Use ParseFullTomlFromDir instead
func ParseTomlFromDir(relativeDir string) ([]*toml.Tree, error) {
return parseTomlFromDir(relativeDir, constraint)
}
// Deprecated: Use ParseFullToml instead
func ParseTomlOverrides() ([]*toml.Tree, error) {
return ParseTomlOverridesFromDir("")
}
// Deprecated: Use ParseFullTomlFromDir instead
func ParseTomlOverridesFromDir(relativeDir string) ([]*toml.Tree, error) {
return parseTomlFromDir(relativeDir, override)
}
func parseTomlFromDir(relativeDir, configType string) ([]*toml.Tree, error) {
config, err := toml.LoadFile(filepath.Join(relativeDir, gopkgToml))
if err != nil {
return nil, err
}
tomlTree := config.Get(configType)
switch typedTree := tomlTree.(type) {
case []*toml.Tree:
return typedTree, nil
default:
return nil, fmt.Errorf("unable to parse toml tree")
}
}
type TomlWrapper struct {
Overrides []*toml.Tree
Constraints []*toml.Tree
}
func ParseFullTomlFromDir(relativeDir string) (*TomlWrapper, error) {
overrides, err := ParseTomlOverridesFromDir(relativeDir)
if err != nil {
return nil, err
}
constraints, err := ParseTomlFromDir(relativeDir)
if err != nil {
return nil, err
}
return &TomlWrapper{
Constraints: constraints,
Overrides: overrides,
}, nil
}
func ParseFullToml() (*TomlWrapper, error) {
return ParseFullTomlFromDir("")
}
func getVersionInfoFromTree(tomlTree *toml.Tree, pkgName string) (info *dep.VersionInfo, found bool) {
isEmpty := func(node *toml.Tree, key string) bool {
return node.Get(key) == nil || node.Get(key) == ""
}
if tomlTree.Get(nameConst) != pkgName {
return nil, false
}
switch {
case !isEmpty(tomlTree, versionConst):
return &dep.VersionInfo{
Version: tomlTree.Get(versionConst).(string),
Type: dep.Version,
}, true
case !isEmpty(tomlTree, revisionConst):
return &dep.VersionInfo{
Version: tomlTree.Get(revisionConst).(string),
Type: dep.Revision,
}, true
case !isEmpty(tomlTree, branchConst):
return &dep.VersionInfo{
Version: tomlTree.Get(branchConst).(string),
Type: dep.Branch,
}, true
}
return nil, false
}