-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (121 loc) · 3.57 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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/mitchellh/colorstring"
"github.com/radeksimko/go-mod-diff/diff"
"github.com/radeksimko/go-mod-diff/github"
"github.com/radeksimko/go-mod-diff/gomod"
"github.com/radeksimko/go-mod-diff/govendor"
)
func main() {
// Setup GitHub connection
gh := github.NewGitHub()
if os.Getenv("GITHUB_TOKEN") != "" {
gh = github.NewGitHubWithToken(os.Getenv("GITHUB_TOKEN"))
}
// Parse go modules file
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
goModFile, err := gomod.ParseFile(filepath.Join(cwd, "go.mod"))
// Parse govendor file
govendorFile, err := govendor.ParseFile(os.Args[1])
if err != nil {
log.Fatal(err)
}
// Compare both and print out differences
d, err := diff.CompareGoModWithGovendor(goModFile, govendorFile, gh)
if err != nil {
log.Fatal(err)
}
printDifference(d, gomod.GetVersionForModule(goModFile))
total := len(goModFile.Require) - len(d.Matched)
colorstring.Printf("\n\nMatched package revisions: [bold][green]%d[reset] of %d.\n"+
"[bold]%d[reset] to check ([bold][red]%d[reset] not found and [bold][yellow]%d[reset] different revs).\n",
len(d.Matched), len(goModFile.Require), total, len(d.NotFound), len(d.Different))
}
func printDifference(d *diff.Diff, vlF gomod.VersionLookupFunc) {
for _, entry := range d.Errored {
printDiffEntry(entry, vlF)
}
for _, entry := range d.NotFound {
printDiffEntry(entry, vlF)
}
for _, entry := range d.Different {
printDiffEntry(entry, vlF)
}
for _, entry := range d.Matched {
colorstring.Printf("\n[bold]%s[reset] [bold][green]✓[reset]", entry.ModulePath)
}
}
func printDiffEntry(de *diff.DiffEntry, vlF gomod.VersionLookupFunc) {
colorstring.Printf("\n[bold]%s[reset]\n", de.ModulePath)
colorstring.Printf(" - go modules: %s\n", de.GoModVersion.String())
if de.Error != nil {
colorstring.Printf(" - [bold][red]Error:[reset] [red]%s[reset]\n", de.Error.Error())
}
repo, err := github.ParseRepositoryURL(de.ModulePath)
if err == nil {
ref, err := gomod.ParseRefFromVersion(de.GoModVersion.Version)
if err == nil {
fmt.Printf(" - GitHub: %s\n", github.TreeURL(repo, ref.String()))
}
}
if de.GithubVersion != nil {
colorstring.Printf(" - GitHub rev: %s\n", de.GithubVersion.String())
}
fmt.Print(" - govendor: ")
if len(de.GoVendorVersions) > 0 {
fmt.Printf("[\n")
for _, gvv := range de.GoVendorVersions {
if gvv.IsEqual(de.GoModVersion) || gvv.IsEqual(de.GithubVersion) {
colorstring.Printf(" [green]%s\n", gvv.String())
} else {
fmt.Printf(" %s\n", gvv.String())
}
}
fmt.Print(" ]\n")
} else {
colorstring.Print("[red]not found\n")
}
printGoModWhy(de.ModulePath, vlF)
}
func printGoModWhy(path string, vlF gomod.VersionLookupFunc) {
fmt.Printf(" - go mod why: ")
mts, stderr, err := gomod.GoModWhy(path)
if err != nil {
colorstring.Printf("[bold][red]Failed to check (%s)[reset][red]\n%s", err, stderr)
return
}
if len(mts) > 0 {
fmt.Printf("[")
} else {
colorstring.Printf("[bold][red]Package not needed (try `go mod tidy`)\n")
}
for _, mt := range mts {
for _, t := range mt {
versionSuffix := ""
githubURL := ""
version := vlF(t)
if version != "" {
versionSuffix = " @ " + version
repo, err := github.ParseRepositoryURL(t)
if err == nil {
ref, err := gomod.ParseRefFromVersion(version)
if err == nil {
githubURL = fmt.Sprintf(" (%s)", github.TreeURL(repo, ref.String()))
}
}
}
fmt.Printf("\n %s%s%s", t, versionSuffix, githubURL)
}
fmt.Println("")
}
if len(mts) > 0 {
fmt.Printf(" ]\n")
}
}