/
init.go
163 lines (130 loc) · 3.61 KB
/
init.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
package true_git
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/Masterminds/semver"
)
const (
MinGitVersionConstraintValue = "1.9"
MinGitVersionWithSubmodulesConstraintValue = "2.14"
)
var (
ForbiddenGitVersionsConstraintValues = []string{"2.22.0"}
)
var (
gitVersion *semver.Version
minGitVersionErrorMsg = fmt.Sprintf("Git version >= %s required", MinGitVersionConstraintValue)
forbiddenGitVersionErrorMsg = fmt.Sprintf("Forbidden git versions: %s", strings.Join(ForbiddenGitVersionsConstraintValues, ", "))
submodulesVersionErrorMsg = fmt.Sprintf("To use git submodules install git >= %s", MinGitVersionWithSubmodulesConstraintValue)
outStream, errStream io.Writer
liveGitOutput bool
)
type Options struct {
Out, Err io.Writer
LiveGitOutput bool
}
func Init(opts Options) error {
outStream = os.Stdout
errStream = os.Stderr
liveGitOutput = opts.LiveGitOutput
if opts.Out != nil {
outStream = opts.Out
}
if opts.Err != nil {
errStream = opts.Err
}
var err error
v, err := getGitCliVersion()
if err != nil {
return err
}
vObj, err := semver.NewVersion(v)
if err != nil {
errMsg := strings.Join([]string{
fmt.Sprintf("Unexpected git version spec %s", v),
minGitVersionErrorMsg,
forbiddenGitVersionErrorMsg,
submodulesVersionErrorMsg,
}, ".\n")
return errors.New(errMsg)
}
gitVersion = vObj
if err := checkVersionConstraints(); err != nil {
return err
}
return nil
}
func getGitCliVersion() (string, error) {
cmd := exec.Command("git", "version")
out := bytes.Buffer{}
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
errMsg := strings.Join([]string{
fmt.Sprintf("Git version command failed: %s", err),
minGitVersionErrorMsg,
forbiddenGitVersionErrorMsg,
submodulesVersionErrorMsg,
}, ".\n")
return "", errors.New(errMsg)
}
strippedOut := strings.TrimSpace(out.String())
rightPart := strings.TrimLeft(strippedOut, "git version ")
fullVersion := strings.Split(rightPart, " ")[0]
fullVersionParts := strings.Split(fullVersion, ".")
lowestVersionPartInd := 3
if len(fullVersionParts) < lowestVersionPartInd {
lowestVersionPartInd = len(fullVersionParts)
}
version := strings.Join(fullVersionParts[0:lowestVersionPartInd], ".")
return version, nil
}
func checkVersionConstraints() error {
constraints := []*semver.Constraints{}
if os.Getenv("WERF_DISABLE_GIT_MIN_VERSION_CONSTRAINT") != "1" {
minVersionConstraints, err := semver.NewConstraint(fmt.Sprintf(">= %s", MinGitVersionConstraintValue))
if err != nil {
panic(err)
}
constraints = append(constraints, minVersionConstraints)
}
for _, cv := range ForbiddenGitVersionsConstraintValues {
forbiddenVersionsConstraints, err := semver.NewConstraint(fmt.Sprintf("!= %s", cv))
if err != nil {
panic(err)
}
constraints = append(constraints, forbiddenVersionsConstraints)
}
for i := range constraints {
if !constraints[i].Check(gitVersion) {
errMsg := strings.Join([]string{
minGitVersionErrorMsg,
forbiddenGitVersionErrorMsg,
submodulesVersionErrorMsg,
fmt.Sprintf("Your git version is %s", gitVersion.String()),
}, ".\n")
return errors.New(errMsg)
}
}
return nil
}
func checkSubmoduleConstraint() error {
constraint, err := semver.NewConstraint(fmt.Sprintf(">= %s", MinGitVersionWithSubmodulesConstraintValue))
if err != nil {
panic(err)
}
if !constraint.Check(gitVersion) {
errMsg := strings.Join([]string{
strings.ToLower(submodulesVersionErrorMsg),
fmt.Sprintf("Your git version is %s", gitVersion.String()),
}, ".\n")
return errors.New(errMsg)
}
return nil
}