/
modfile.go
240 lines (188 loc) · 3.91 KB
/
modfile.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package common
import (
"bufio"
"bytes"
"fmt"
"strings"
)
type Module struct {
Name string
}
type GO struct {
Version string
}
type Toolchain struct {
Version string
}
type Require struct {
Module string
Version string
Indirect bool
}
type Replace struct {
Module string
Target string
}
type Exclude struct {
Module string
Version string
}
type Retract struct {
VersionLow string
VersionHigh string
Comment string
}
type Modfile struct {
Module Module
GO GO
Toolchain Toolchain
Requires []Require
Replaces []Replace
Excludes []Exclude
Retracts []Retract
}
func ReadModfile(ba []byte) (*Modfile, error) {
modfile := &Modfile{}
correct := false
inRequire := false
inReplace := false
inExclude := false
inRetract := false
var line string
readRequire := func() {
if strings.Contains(line, ")") {
inRequire = false
return
}
indirect := false
p := strings.Index(line, "// indirect")
if p != -1 {
line = strings.TrimSpace(line[:p])
indirect = true
}
splits := Split(line, " ")
modfile.Requires = append(modfile.Requires, Require{
Module: splits[0],
Version: splits[1],
Indirect: indirect,
})
}
readReplace := func() {
if strings.Contains(line, ")") {
inReplace = false
return
}
splits := Split(line, " ")
modfile.Replaces = append(modfile.Replaces, Replace{
Module: splits[1],
Target: splits[3],
})
}
readExclude := func() {
if strings.Contains(line, ")") {
inExclude = false
return
}
splits := Split(line, " ")
modfile.Excludes = append(modfile.Excludes, Exclude{
Module: splits[1],
Version: splits[2],
})
}
readRetract := func() {
if strings.Contains(line, ")") {
inRetract = false
return
}
comment := ""
p := strings.Index(line, "//")
if p != -1 {
comment = strings.TrimSpace(line[p+2:])
line = strings.TrimSpace(line[:p])
}
retract := Retract{
Comment: comment,
}
splits := Split(line, " ")
if strings.HasPrefix(splits[1], "[") {
splits[1] = strings.TrimPrefix(splits[1], "[")
splits[1] = strings.TrimSuffix(splits[1], "]")
}
values := Split(splits[1], ",")
if len(values) >= 1 {
retract.VersionLow = values[0]
}
if len(values) == 2 {
retract.VersionHigh = values[1]
}
modfile.Retracts = append(modfile.Retracts, retract)
}
scanner := bufio.NewScanner(bytes.NewReader(ba))
for scanner.Scan() {
line = strings.TrimSpace(scanner.Text())
switch {
case inRequire:
readRequire()
case inReplace:
readReplace()
case inExclude:
readExclude()
case inRetract:
readRetract()
case strings.Contains(line, "require "):
p := strings.Index(line, "(")
if p != -1 {
inRequire = true
} else {
line = strings.TrimSpace(line[p+1:])
readRequire()
}
case strings.Contains(line, "replace "):
p := strings.Index(line, "(")
if p != -1 {
inReplace = true
} else {
line = strings.TrimSpace(line[p+1:])
readReplace()
}
case strings.Contains(line, "exclude "):
p := strings.Index(line, "(")
if p != -1 {
inExclude = true
} else {
line = strings.TrimSpace(line[p+1:])
readExclude()
}
case strings.Contains(line, "retract "):
p := strings.Index(line, "(")
if p != -1 {
inRetract = true
} else {
line = strings.TrimSpace(line[p+1:])
readRetract()
}
case strings.Contains(line, "module "):
correct = true
splits := Split(line, " ")
modfile.Module.Name = splits[1]
case strings.Contains(line, "go "):
splits := Split(line, " ")
modfile.GO.Version = splits[1]
case strings.Contains(line, "toolchain "):
splits := Split(line, " ")
modfile.Toolchain.Version = splits[1]
}
}
if !correct {
return nil, fmt.Errorf("cannot read go.mod content")
}
return modfile, nil
}
func (mf *Modfile) Title() string {
title := mf.Module.Name
p := strings.LastIndex(title, "/")
if p != -1 {
title = title[p+1:]
}
return title
}