-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
910 lines (755 loc) · 19.3 KB
/
main.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
package main
import (
"bufio"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
)
// TODO:
//
// check the directory is a git repo, and error if not
//
// option to run in any directory of git repo. Needs to figure out
// path from repo root.
//
// verbose option to print git commands being run
//
// popen should include command in errors
//
// Deal with git being fussy when a submodule is removed then re-added
//
// warn when it looks like a package ought to be present at the
// particular path, but it's not. E.g. when resolve an import of
// github.com/foo/bar/baz, we find that only github.com/foo exists.
//
// check that declared package names match dirs
//
// Support relative (aka local) imports
//
// Warn on diamond problem
type config struct {
rootDir string
projectName string
update bool
prune bool
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [ <project directory> ]\n",
os.Args[0])
flag.PrintDefaults()
}
var cf config
flag.StringVar(&cf.projectName, "n", "",
"base package name for the project, e.g. github.com/user/proj")
flag.BoolVar(&cf.update, "u", false,
"update dependency submodules from their remote repos")
flag.BoolVar(&cf.prune, "p", false,
"prune unused dependency submodules")
flag.Parse()
switch {
case flag.NArg() == 1:
cf.rootDir = flag.Arg(0)
case flag.NArg() > 1:
flag.Usage()
os.Exit(2)
}
if err := run(&cf); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type vendetta struct {
*config
goPath
goPaths map[string]*goPath
dirPackages map[string]*build.Package
submodules []submodule
}
// A goPath says where to search for packages (analogous to
// GOPATH). Different directories have different gopaths because there
// can be vendor directories anywhere, not just at the top level.
// These gopaths are produced by getGoPath and memoized in the goPaths
// map.
type goPath struct {
dir string
next *goPath
// prefixes is nil for goPaths corresponding to vendor
// directories. But there is also a goPath corresponding to
// the top-level project directory. When searching for a
// package in that top-level directory, we need to remove any
// prefix of the package name corresponding to the root name
// of the project (e.g. github.com/user/proj). prefixes is
// the set of such prefixes.
prefixes map[string]struct{}
}
type submodule struct {
dir string
used bool
}
func run(cf *config) error {
v := vendetta{
config: cf,
goPaths: make(map[string]*goPath),
dirPackages: make(map[string]*build.Package),
}
v.goPaths[""] = &goPath{dir: "vendor", next: &v.goPath}
v.prefixes = make(map[string]struct{})
rootPkgs, err := v.scanRootProject()
if err != nil {
return err
}
if cf.projectName != "" {
v.prefixes[cf.projectName] = struct{}{}
} else {
if err := v.inferProjectNameFromGoPath(); err != nil {
return err
}
if err := v.inferProjectNameFromGit(); err != nil {
return err
}
v.inferProjectNameFromImportComments(rootPkgs)
if !mainOnly(rootPkgs) && len(v.prefixes) == 0 {
return fmt.Errorf("Unable to infer project name; specify it explicitly with the '-n' option.")
}
}
if err := v.checkSubmodules(); err != nil {
return err
}
if err := v.populateSubmodules(); err != nil {
return err
}
if err := v.resolveRootProjectDeps(rootPkgs); err != nil {
return err
}
return v.pruneSubmodules()
}
// Attempt to infer the project name from GOPATH, by seeing if the
// project dir resides under any element of the GOPATH.
func (v *vendetta) inferProjectNameFromGoPath() error {
gp := os.Getenv("GOPATH")
if gp == "" {
return nil
}
gpparts := filepath.SplitList(gp)
gpfis := make([]os.FileInfo, len(gpparts))
for i, p := range gpparts {
var err error
gpfis[i], err = os.Stat(filepath.Join(p, "src"))
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
}
// Get the absolute path to the project dir
dir := v.rootDir
if dir == "" {
dir = "."
}
dir, err := filepath.Abs(dir)
if err != nil {
return err
}
// walk from the project dir upwards towards the filesystem root
var proj, projsep string
for {
var subdir string
dir, subdir = filepath.Split(dir)
proj = subdir + projsep + proj
projsep = "/"
fi, err := os.Stat(dir)
if err != nil {
return err
}
for i, gpfi := range gpfis {
if gpfi != nil && os.SameFile(fi, gpfi) {
v.inferredProjectName(proj, "GOPATH element",
gpparts[i])
return nil
}
}
if dir != "" && dir[len(dir)-1] == os.PathSeparator {
dir = dir[:len(dir)-1]
}
if dir == "" {
return nil
}
}
}
var remoteUrlRE = regexp.MustCompile(`^(?:https://github\.com/|git@github\.com:)(.*\.?)$`)
func (v *vendetta) inferProjectNameFromGit() error {
remotes, err := v.popen("git", "remote", "-v")
if err != nil {
return err
}
defer remotes.close()
for remotes.Scan() {
fields := splitWS(remotes.Text())
if len(fields) < 2 {
return fmt.Errorf("could not parse 'git remote' output")
}
m := remoteUrlRE.FindStringSubmatch(fields[1])
if m != nil {
name := m[1]
if strings.HasSuffix(name, ".git") {
name = name[:len(name)-4]
}
v.inferredProjectName("github.com/"+name,
"git remote", fields[0])
}
}
if err := remotes.close(); err != nil {
return err
}
return nil
}
func (v *vendetta) inferProjectNameFromImportComments(rootPkgs []rootPackage) {
for _, pkg := range rootPkgs {
ic := pkg.ImportComment
if ic == "" {
continue
}
// For an import comment to suggest a project name, it
// should have the path of the package within the
// project as a suffix.
if pkg.dir != "" {
suffix := pathToPackage(pkg.dir)
if len(ic) <= len(suffix) ||
ic[len(ic)-len(suffix):] != suffix &&
ic[len(ic)-len(suffix)-1] != '/' {
continue
}
ic = ic[:len(ic)-len(suffix)-1]
}
v.inferredProjectName(ic, "import comment in",
v.realDir(pkg.dir))
}
}
func (v *vendetta) inferredProjectName(proj string, source ...interface{}) {
if _, found := v.prefixes[proj]; !found {
fmt.Println(append([]interface{}{
"Inferred root package name", proj, "from",
}, source...)...)
v.prefixes[proj] = struct{}{}
}
}
// Check for submodules that seem to be missing in the working tree.
func (v *vendetta) checkSubmodules() error {
var err2 error
if err := v.querySubmodules(func(path string) bool {
err2 = v.checkSubmodule(path)
return err2 == nil
}, "--recursive"); err != nil {
return err
}
return err2
}
func (v *vendetta) checkSubmodule(dir string) error {
foundSomething := false
if err := readDir(v.realDir(dir), func(fi os.FileInfo) bool {
foundSomething = true
return false
}); err != nil && !os.IsNotExist(err) {
return err
}
if !foundSomething {
return fmt.Errorf("The submodule '%s' doesn't seem to the present in the working tree. Maybe you forgot to update with 'git submodule update --init --recursive'?", dir)
}
return nil
}
func (v *vendetta) querySubmodules(f func(string) bool, args ...string) error {
status, err := v.popen("git",
append([]string{"submodule", "status"}, args...)...)
if err != nil {
return err
}
defer status.close()
for status.Scan() {
fields := splitWS(strings.TrimSpace(status.Text()))
if len(fields) < 2 {
return fmt.Errorf("could not parse 'git submodule status' output")
}
path := fields[1]
if !f(path) {
return nil
}
}
return status.close()
}
func (v *vendetta) populateSubmodules() error {
var submodules []string
if err := v.querySubmodules(func(path string) bool {
submodules = append(submodules, path)
return true
}); err != nil {
return err
}
sort.Strings(submodules)
v.submodules = make([]submodule, 0, len(submodules))
for _, p := range submodules {
v.submodules = append(v.submodules, submodule{dir: p})
}
return nil
}
func (v *vendetta) pathInSubmodule(path string) *submodule {
i := sort.Search(len(v.submodules), func(i int) bool {
return v.submodules[i].dir >= path
})
if i < len(v.submodules) && v.submodules[i].dir == path {
return &v.submodules[i]
}
if i > 0 && isSubpath(path, v.submodules[i-1].dir) {
return &v.submodules[i-1]
}
return nil
}
func (v *vendetta) addSubmodule(dir string) {
i := sort.Search(len(v.submodules), func(i int) bool {
return v.submodules[i].dir >= dir
})
submodules := make([]submodule, len(v.submodules)+1)
copy(submodules, v.submodules[:i])
submodules[i] = submodule{dir: dir, used: true}
copy(submodules[i+1:], v.submodules[i:])
v.submodules = submodules
}
func isSubpath(path, dir string) bool {
return path == dir ||
(strings.HasPrefix(path, dir) && path[len(dir)] == os.PathSeparator)
}
func (v *vendetta) updateSubmodule(sm *submodule) error {
fmt.Fprintf(os.Stderr, "Updating submodule %s from remote\n", sm.dir)
if err := v.git("submodule", "update", "--remote", "--recursive", sm.dir); err != nil {
return err
}
// If we don't put the updated submodule into the index, a
// subsequent "git submodule update" will revert it, which can
// lead to surprises.
return v.git("add", sm.dir)
}
func (v *vendetta) pruneSubmodules() error {
for _, sm := range v.submodules {
if sm.used || !isSubpath(sm.dir, "vendor") {
continue
}
if v.prune {
fmt.Fprintf(os.Stderr, "Removing unused submodule %s\n",
sm.dir)
if err := v.git("rm", "-f", sm.dir); err != nil {
return err
}
if err := v.removeEmptyDirsAbove(sm.dir); err != nil {
return err
}
} else {
fmt.Fprintf(os.Stderr, "Unused submodule %s (use -p option to prune)\n", sm.dir)
}
}
return nil
}
func (v *vendetta) removeEmptyDirsAbove(dir string) error {
for {
dir = parentDir(dir)
if dir == "" {
return nil
}
empty := true
if err := readDir(v.realDir(dir), func(_ os.FileInfo) bool {
empty = false
return false
}); err != nil {
return err
}
if !empty {
return nil
}
if err := os.Remove(v.realDir(dir)); err != nil {
return err
}
}
}
// Get the directory name from a path. path.Dir doesn't
// do what we want in the case where there is no path
// separator:
func parentDir(path string) string {
slash := strings.LastIndexByte(path, os.PathSeparator)
dir := ""
if slash >= 0 {
dir = path[:slash]
}
return dir
}
var wsRE = regexp.MustCompile(`[ \t]+`)
func splitWS(s string) []string {
return wsRE.Split(s, -1)
}
func (v *vendetta) gitSubmoduleAdd(url, dir string) error {
fmt.Fprintf(os.Stderr, "Adding %s at %s\n", url, dir)
err := v.git("submodule", "add", url, dir)
if err != nil {
return err
}
v.addSubmodule(dir)
return nil
}
func (v *vendetta) git(args ...string) error {
return v.system("git", args...)
}
func (v *vendetta) system(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Dir = v.rootDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err == nil {
err = cmd.Wait()
if err == nil {
return nil
}
}
return fmt.Errorf("Command failed: %s %s (%s)",
name, strings.Join(args, " "), err)
}
type popenLines struct {
cmd *exec.Cmd
stdout io.ReadCloser
*bufio.Scanner
}
func (v *vendetta) popen(name string, args ...string) (popenLines, error) {
cmd := exec.Command(name, args...)
cmd.Dir = v.rootDir
stdout, err := cmd.StdoutPipe()
if err != nil {
return popenLines{}, err
}
cmd.Stderr = os.Stderr
p := popenLines{cmd: cmd, stdout: stdout}
if err := cmd.Start(); err != nil {
return popenLines{}, err
}
p.Scanner = bufio.NewScanner(stdout)
return p, nil
}
func (p popenLines) close() error {
res := p.Scanner.Err()
setRes := func(err error) {
if res == nil {
res = err
}
}
if p.stdout != nil {
_, err := io.Copy(ioutil.Discard, p.stdout)
p.stdout = nil
if err != nil {
setRes(err)
p.cmd.Process.Kill()
}
}
if p.cmd != nil {
setRes(p.cmd.Wait())
p.cmd = nil
}
return res
}
func (v *vendetta) realDir(dir string) string {
res := filepath.Join(v.rootDir, dir)
if res == "" {
res = "."
}
return res
}
type rootPackage struct {
dir string
*build.Package
}
func (v *vendetta) scanRootProject() ([]rootPackage, error) {
// Load each package in the root project without resolving
// dependencies, because we process packages in the root
// project slightly differently to dependency packages.
var pkgs []rootPackage
var err error
var traverseDir func(dir string, root bool)
traverseDir = func(dir string, root bool) {
var pkg *build.Package
pkg, err = v.loadPackage(dir, true)
if err != nil {
return
}
if pkg != nil {
pkgs = append(pkgs, rootPackage{dir, pkg})
}
err = readDir(v.realDir(dir), func(fi os.FileInfo) bool {
if !fi.IsDir() {
return true
}
switch fi.Name() {
case "vendor":
if root {
return true
}
case "testdata":
return true
}
traverseDir(filepath.Join(dir, fi.Name()), false)
return err == nil
})
}
traverseDir("", true)
if err != nil {
return nil, err
}
return pkgs, nil
}
func (v *vendetta) resolveRootProjectDeps(pkgs []rootPackage) error {
for _, pkg := range pkgs {
if err := v.resolveDependencies(pkg.dir, pkg.Imports); err != nil {
return err
}
if err := v.resolveDependencies(pkg.dir, pkg.TestImports); err != nil {
return err
}
if err := v.resolveDependencies(pkg.dir, pkg.XTestImports); err != nil {
return err
}
}
return nil
}
func mainOnly(pkgs []rootPackage) bool {
for _, pkg := range pkgs {
if pkg.Name != "main" {
return false
}
}
return true
}
func (v *vendetta) scanPackage(dir string) (*build.Package, error) {
if pkg := v.dirPackages[dir]; pkg != nil {
return pkg, nil
}
pkg, err := v.loadPackage(dir, false)
if err != nil {
return nil, err
}
if err = v.resolveDependencies(dir, pkg.Imports); err != nil {
return nil, err
}
return pkg, nil
}
func (v *vendetta) loadPackage(dir string, noGoOk bool) (*build.Package, error) {
pkg, err := build.Default.ImportDir(v.realDir(dir), build.ImportComment)
if err != nil {
if _, ok := err.(*build.NoGoError); ok && noGoOk {
return nil, nil
}
return nil, fmt.Errorf("gathering imports in %s: %s",
v.realDir(dir), err)
}
v.dirPackages[dir] = pkg
return pkg, nil
}
func (v *vendetta) resolveDependencies(dir string, deps []string) error {
for _, dep := range deps {
if err := v.resolveDependency(dir, dep); err != nil {
return err
}
}
return nil
}
func (v *vendetta) resolveDependency(dir string, pkg string) error {
found, pkgdir, err := v.searchGoPath(dir, pkg)
switch {
case err != nil:
return err
case found:
// Does the package fall within an existing submodule
// under vendor/ ?
if sm := v.pathInSubmodule(pkgdir); sm != nil && !sm.used {
sm.used = true
if v.update {
if err := v.updateSubmodule(sm); err != nil {
return err
}
}
}
default:
pkgdir, err = v.obtainPackage(pkg)
if err != nil || pkgdir == "" {
return err
}
}
pi, err := v.scanPackage(pkgdir)
if err != nil {
return err
}
if pi.ImportComment != "" && pkg != pi.ImportComment {
fmt.Printf("Warning: Package with import comment %s referred to as %s (from directory %s)\n",
pi.ImportComment, pkg, v.realDir(dir))
}
return nil
}
func (v *vendetta) obtainPackage(pkg string) (string, error) {
bits := strings.Split(pkg, "/")
// Exclude golang standard packages
if !strings.Contains(bits[0], ".") {
return "", nil
}
// Figure out how to obtain the package. Packages on
// github.com are treated as a special case, because that is
// most of them. Otherwise, we use the queryRepoRoot code
// borrowed from vcs.go to figure out how to obtain the
// package.
var basePkg, url string
if bits[0] == "github.com" {
if len(bits) < 3 {
return "", fmt.Errorf("github.com package name %s seems to be truncated", pkg)
}
basePkg = strings.Join(bits[:3], "/")
url = "https://" + basePkg
} else if bits[0] == "bitbucket.org" {
if len(bits) < 3 {
return "", fmt.Errorf("bitbucket.org package name %s seems to be truncated", pkg)
}
basePkg = strings.Join(bits[:3], "/")
url = "https://" + basePkg
// Probe to see if it is a git repo
if exec.Command("git", "ls-remote", url).Run() != nil {
return "", fmt.Errorf("Package %s does not seem to be git repo at %s; maybe it's an hg repo?", pkg, url)
}
} else if rr, err := queryRepoRoot(pkg, secure); err == nil {
if rr.vcs != "git" {
return "", fmt.Errorf("Package %s does not live in a git repo", pkg)
}
basePkg = rr.root
url = rr.repo
} else if strings.HasSuffix(err.Error(), "no go-import meta tags") && len(bits) >= 3 {
// When no go-import meta tag is found, guess the base
// package and repo URL, so that e.g. package names on
// gitlab work. The test above is gross, but it
// avoids changes to the borrowed reporoot code.
basePkg = strings.Join(bits[:3], "/")
url = fmt.Sprintf("https://%s.git", basePkg)
fmt.Printf("Warning: no go-import meta tags found for package '%s'. Guessing git repo URL '%s'\n", pkg, url)
} else {
return "", err
}
projDir := filepath.Join("vendor", packageToPath(basePkg))
if err := v.gitSubmoduleAdd(url, projDir); err != nil {
return "", err
}
return filepath.Join("vendor", packageToPath(pkg)), nil
}
// Search the gopath for the given dir to find an existing package
func (v *vendetta) searchGoPath(dir, pkg string) (bool, string, error) {
gp, err := v.getGoPath(dir)
if err != nil {
return false, "", err
}
for gp != nil {
found, pkgdir, err := gp.provides(pkg, v)
if err != nil {
return false, "", err
}
if found {
return found, pkgdir, nil
}
gp = gp.next
}
return false, "", nil
}
func (v *vendetta) getGoPath(dir string) (*goPath, error) {
gp := v.goPaths[dir]
if gp != nil {
return gp, nil
}
gp, err := v.getGoPath(parentDir(dir))
if err != nil {
return nil, err
}
// If there's a vendor/ dir here, we need to put it on the
// front of the gopath
vendorDir := filepath.Join(dir, "vendor")
fi, err := os.Stat(v.realDir(vendorDir))
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else if fi.IsDir() {
gp = &goPath{dir: vendorDir, next: gp}
}
v.goPaths[dir] = gp
return gp, nil
}
func (gp *goPath) provides(pkg string, v *vendetta) (bool, string, error) {
matched, pkg := gp.removePrefix(pkg)
if !matched {
return false, "", nil
}
foundGoSrc := false
pkgdir := filepath.Join(gp.dir, packageToPath(pkg))
if err := readDir(v.realDir(pkgdir), func(fi os.FileInfo) bool {
// Should check for symlinks here?
if fi.Mode().IsRegular() && strings.HasSuffix(fi.Name(), ".go") {
foundGoSrc = true
return false
}
return true
}); err != nil {
if os.IsNotExist(err) {
err = nil
}
return false, "", err
}
return foundGoSrc, pkgdir, nil
}
func (gp *goPath) removePrefix(pkg string) (bool, string) {
if gp.prefixes == nil {
return true, pkg
}
for prefix := range gp.prefixes {
if pkg == prefix {
return true, ""
} else if isSubpath(pkg, prefix) {
return true, pkg[len(prefix)+1:]
}
}
return false, ""
}
// Convert a package name to a filesystem path
func packageToPath(name string) string {
return filepath.FromSlash(name)
}
// Convert a filesystem path to a package name
func pathToPackage(path string) string {
return filepath.ToSlash(path)
}
func readDir(dir string, f func(os.FileInfo) bool) error {
dh, err := os.Open(dir)
if err != nil {
return err
}
defer dh.Close()
for {
fis, err := dh.Readdir(100)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
for _, fi := range fis {
if !f(fi) {
return nil
}
}
}
}