-
Notifications
You must be signed in to change notification settings - Fork 199
/
init.go
137 lines (107 loc) · 2.79 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
package true_git
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/Masterminds/semver"
)
const (
MinGitVersionConstraintValue = "1.9"
MinGitVersionWithSubmodulesConstraintValue = "2.14"
)
var (
gitVersion *semver.Version
minGitVersionErrorMsg = fmt.Sprintf("Git version >= %s required", MinGitVersionConstraintValue)
submodulesVersionErrorMsg = fmt.Sprintf("To use git submodules install git >= %s", MinGitVersionWithSubmodulesConstraintValue)
outStream, errStream io.Writer
)
type Options struct {
Out, Err io.Writer
}
func Init(opts Options) error {
outStream = os.Stdout
errStream = os.Stderr
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,
submodulesVersionErrorMsg,
}, ".\n")
return errors.New(errMsg)
}
gitVersion = vObj
if err := checkMinVersionConstraint(); 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,
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 checkMinVersionConstraint() error {
constraint, err := semver.NewConstraint(fmt.Sprintf(">= %s", MinGitVersionConstraintValue))
if err != nil {
panic(err)
}
if !constraint.Check(gitVersion) {
errMsg := strings.Join([]string{
strings.ToLower(minGitVersionErrorMsg),
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
}