/
govers.go
352 lines (317 loc) · 9.53 KB
/
govers.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
The govers command searches all Go packages under the current
directory for imports with a prefix matching a particular pattern, and
changes them to another specified prefix. As with gofmt and gofix, there is
no backup - you are expected to be using a version control system.
It prints the names of any packages that are modified.
Usage:
govers [-d] [-m regexp] [-n] new-package-path
It accepts the following flags:
-d
Suppress dependency checking
-m regexp
Search for and change imports which have the
given pattern as a prefix (see below for the default).
-n
Don't make any changes; just perform checks.
If the pattern is not specified with the -m flag, it is derived from
new-package-path and matches any prefix that is the same in all but
version. A version is defined to be an element within a package path
that matches the regular expression "(/|\.)v[0-9.]+".
The govers command will also check (unless the -d flag is given)
that no (recursive) dependencies would be changed if the same govers
command was run on them. If they would, govers will fail and do nothing.
For example, say a new version of the tomb package is released.
The old import path was gopkg.in/tomb.v2, and we want
to use the new verson, gopkg.in/tomb.v3. In the root of the
source tree we want to change, we run:
govers gopkg.in/tomb.v3
This will change all gopkg.in/tomb.v2 imports to use v3.
It will also check that all external packages that we're
using are also using v3, making sure that our program
is consistently using the same version throughout.
BUG: Vendored imports are not dealt with correctly - they won't
be changed. It's not yet clear how this command should work then.
*/
package main
import (
"bufio"
"flag"
"fmt"
"go/build"
"go/parser"
"go/printer"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
const help = `
The govers command searches all Go packages under the current
directory for imports with a prefix matching a particular pattern, and
changes them to another specified prefix. As with gofmt and gofix, there is
no backup - you are expected to be using a version control system.
It prints the names of any packages that are modified.
Usage:
govers [-d] [-m regexp] [-n] new-package-path
It accepts the following flags:
-d
Suppress dependency checking
-m regexp
Search for and change imports which have the
given pattern as a prefix (see below for the default).
-n
Don't make any changes; just perform checks.
If the pattern is not specified with the -m flag, it is derived from
new-package-path and matches any prefix that is the same in all but
version. A version is defined to be an element within a package path
that matches the regular expression "(/|\.)v[0-9.]+(-unstable)?".
The govers command will also check (unless the -d flag is given)
that no (recursive) dependencies would be changed if the same govers
command was run on them. If they would, govers will fail and do nothing.
For example, say a new version of the tomb package is released.
The old import path was gopkg.in/tomb.v2, and we want
to use the new verson, gopkg.in/tomb.v3. In the root of the
source tree we want to change, we run:
govers gopkg.in/tomb.v3
This will change all gopkg.in/tomb.v2 imports to use v3.
It will also check that all external packages that we're
using are also using v3, making sure that our program
is consistently using the same version throughout.
`
var (
match = flag.String("m", "", "change imports with a matching prefix")
noEdit = flag.Bool("n", false, "don't make any changes; perform checks only")
noDependencies = flag.Bool("d", false, "suppress dependency checking")
)
var cwd, _ = os.Getwd()
func main() {
flag.Usage = func() {
fmt.Printf("%s", help[1:])
os.Exit(2)
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
}
newPackage := flag.Arg(0)
cwd, err := os.Getwd()
if err != nil {
fatalf("cannot get working directory: %v", err)
}
var oldPackagePat *regexp.Regexp
if *match != "" {
oldPackagePat, err = regexp.Compile("^(" + *match + ")")
if err != nil {
fatalf("invalid match pattern: %v", err)
}
} else {
oldPackagePat = pathVersionPat(newPackage)
}
buildCtxt := build.Default
// BUG we ignore files that are ignored by the current build context
// if we don't set this flag, but if we do set it, the import fails.
// The solution is to avoid using build.Import but it's convenient
// at the moment.
// buildCtxt.UseAllFiles = true
ctxt := &context{
cwd: cwd,
newPackage: newPackage,
oldPackagePat: oldPackagePat,
buildCtxt: &buildCtxt,
checked: make(map[string]bool),
editPkgs: make(map[string]*editPkg),
}
ctxt.walkDir(cwd)
for path := range ctxt.editPkgs {
ctxt.checkPackage(path, cwd)
}
if ctxt.failed {
os.Exit(1)
}
for path, ep := range ctxt.editPkgs {
if !ep.needsEdit {
continue
}
changed := false
for _, file := range ep.goFiles {
changed = ctxt.changeVersion(file) || changed
}
if changed {
fmt.Printf("%s\n", path)
}
}
if ctxt.failed {
os.Exit(1)
}
}
type editPkg struct {
goFiles []string
needsEdit bool
}
type context struct {
cwd string
failed bool
newPackage string
oldPackagePat *regexp.Regexp
buildCtxt *build.Context
checked map[string]bool
editPkgs map[string]*editPkg
}
// walkDir walks all directories below path and
// adds any packages to ctxt.editPkgs.
func (ctxt *context) walkDir(path string) {
entries, err := ioutil.ReadDir(path)
if err != nil {
logf("cannot read directory %q: %v", path, err)
return
}
var ep editPkg
for _, entry := range entries {
if entry.IsDir() {
if !strings.HasPrefix(entry.Name(), ".") {
ctxt.walkDir(filepath.Join(path, entry.Name()))
}
} else {
if strings.HasSuffix(entry.Name(), ".go") {
ep.goFiles = append(ep.goFiles, filepath.Join(path, entry.Name()))
}
}
}
pkg, err := ctxt.buildCtxt.Import(".", path, build.FindOnly)
if err != nil {
// ignore directories that don't correspond to packages.
return
}
ctxt.editPkgs[pkg.ImportPath] = &ep
}
// checkPackage checks all go files in the given
// package, and all their dependencies.
func (ctxt *context) checkPackage(path, fromDir string) {
if path == "C" {
return
}
if ctxt.checked[path] {
// The package has already been, is or being, checked
return
}
pkg, err := ctxt.buildCtxt.Import(path, fromDir, 0)
ctxt.checked[pkg.ImportPath] = true
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
logf("cannot import %q from %q: %v", path, fromDir, err)
}
return
}
ep := ctxt.editPkgs[path]
// N.B. is it worth eliminating duplicates here?
var allImports []string
allImports = append(allImports, pkg.Imports...)
if ctxt.editPkgs[path] != nil {
// The package is in our set of root packages so
// add testing imports too.
allImports = append(allImports, pkg.TestImports...)
allImports = append(allImports, pkg.XTestImports...)
}
for _, impPath := range allImports {
// Import the package to find out its absolute path
// including vendor directories before applying the
// rewrite.
impPkg, _ := ctxt.buildCtxt.Import(impPath, pkg.Dir, 0)
if err != nil {
continue
}
if p := ctxt.fixPath(impPkg.ImportPath); p != impPkg.ImportPath {
if ep == nil {
logf("package %q is using inconsistent path %q", pkg.ImportPath, impPkg.ImportPath)
ctxt.failed = true
continue
}
ep.needsEdit = true
impPath = p
}
if !*noDependencies {
ctxt.checkPackage(impPath, impPkg.Dir)
}
}
}
var printConfig = printer.Config{
Mode: printer.TabIndent | printer.UseSpaces,
Tabwidth: 8,
}
// changeVersion changes the named go file to
// import the new version.
func (ctxt *context) changeVersion(path string) bool {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
if err != nil {
logf("cannot parse %q: %v", path, err)
}
changed := false
for _, ispec := range f.Imports {
impPath, err := strconv.Unquote(ispec.Path.Value)
if err != nil {
panic(err)
}
if p := ctxt.fixPath(impPath); p != impPath {
ispec.Path.Value = strconv.Quote(p)
changed = true
}
}
if !changed || *noEdit {
return changed
}
out, err := os.Create(path)
if err != nil {
logf("cannot create file: %v", err)
}
defer out.Close()
w := bufio.NewWriter(out)
if err := printConfig.Fprint(w, fset, f); err != nil {
logf("cannot write file: %v", err)
}
if err := w.Flush(); err != nil {
logf("cannot write file: %v", err)
}
return true
}
func (ctxt *context) fixPath(p string) string {
loc := ctxt.oldPackagePat.FindStringSubmatchIndex(p)
if loc == nil {
return p
}
i := loc[3]
if p[0:i] != ctxt.newPackage {
p = ctxt.newPackage + p[i:]
}
return p
}
const versPat = `[/|\.]v[0-9]+(-unstable)?`
// pathVersionPat returns a pattern that will match any
// package path that's the same except possibly
// the version number.
func pathVersionPat(p string) *regexp.Regexp {
versRe := regexp.MustCompile(versPat + "(/|$)")
if !versRe.MatchString(p) {
fatalf("%q is not versioned", p)
}
// Use an intermediate step so that we can use QuoteMeta after
// matching against versPat. (versPat won't match quoted
// metacharacters).
// Note that '#' is an invalid character in an import path
p = versRe.ReplaceAllString(p, "#")
p = regexp.QuoteMeta(p)
// BUG doesn't match "foo/v0/v1/bar", but do we care?
p = "^(" + strings.Replace(p, "#", versPat, -1) + ")(/|$)"
return regexp.MustCompile(p)
}
func logf(f string, a ...interface{}) {
fmt.Fprintf(os.Stderr, "govers: %s\n", fmt.Sprintf(f, a...))
}
func fatalf(f string, a ...interface{}) {
logf(f, a...)
os.Exit(2)
}