-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
144 lines (120 loc) · 3.17 KB
/
main.go
File metadata and controls
144 lines (120 loc) · 3.17 KB
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
"github.com/rrgmc/whymodwhy/pkg"
)
var (
printOnly = flag.Bool("p", false, "print only")
showLastVersion = flag.Bool("v", false, "show last version of returned packages")
)
func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func run() error {
graph, err := pkg.RunGoModGraph()
if err != nil {
return err
}
if *printOnly {
return runPrint(graph)
}
return runFind(graph)
}
func runFind(graph *pkg.Graph) error {
if len(flag.Args()) == 0 {
return errors.New("package name is required")
}
pkgs, ok := graph.Packages[flag.Arg(0)]
if !ok {
return fmt.Errorf("package '%s' not found in dependencies", flag.Arg(0))
}
return runFindPkg(graph, pkgs)
}
func runFindPkg(graph *pkg.Graph, p *pkg.Package) error {
// printPackage(graph, p)
// check if direct dependency
if checkDirectDependency(graph, p) {
fmt.Printf("'%s' is a direct dependency of the root module '%s', just run:\n", p.Name, graph.Root)
fmt.Printf("go get %s\n", p.Name)
return nil
}
pkgs, err := checkRootPackages(graph, p)
if err != nil {
return err
}
var lverr error
fmt.Printf("to upgrade '%s' these packages must be upgraded:\n", p.Name)
for _, fp := range pkgs {
lastversion := ""
if *showLastVersion {
lv, err := GetLatestPackageVersion(fp)
if err == nil {
lastversion = fmt.Sprintf(" (last version: %s)", lv.Version)
} else {
lverr = errors.Join(lverr, fmt.Errorf("error getting last version of '%s': %w", fp, err))
}
}
xp := graph.GetPackage(fp)
if xp == nil {
fmt.Printf("- %s%s\n", fp)
} else {
fmt.Printf("- %s (local version: %s)%s\n", fp, xp.LastVersion, lastversion)
}
}
if lverr != nil {
fmt.Printf("errors getting latest version of packages: %s", lverr)
}
return nil
}
func runPrint(graph *pkg.Graph) error {
if len(flag.Args()) > 0 {
pkgs, ok := graph.Packages[flag.Arg(0)]
if !ok {
return fmt.Errorf("unknown package %s", flag.Arg(0))
}
printPackage(graph, pkgs)
return nil
}
for _, pkgs := range graph.SortedPackages() {
printPackage(graph, pkgs)
}
return nil
}
func printPackage(graph *pkg.Graph, pkgs *pkg.Package) {
fmt.Printf("%s %s (%s) %s\n", strings.Repeat("=", 5), pkgs.Name, pkgs.LastVersion, strings.Repeat("=", 5))
for _, version := range pkgs.SortedVersions() {
versionextra := ""
if version.Version == pkgs.LastVersion {
versionextra = " (last)"
}
fmt.Printf("\tVersion: %s%s\n", version.Version, versionextra)
if len(version.Parents) > 0 {
fmt.Printf("\t\t%s Parents %s\n", strings.Repeat("-", 5), strings.Repeat("-", 5))
for parentPkg, parentVersion := range version.Parents {
if parentPkg == graph.Root {
pextra := ""
if graph.IsRootIndirectMod(pkgs.Name) {
pextra = " (indirect)"
}
fmt.Printf("\t\t%s%s\n", parentPkg, pextra)
} else {
fmt.Printf("\t\t%s (%s)\n", parentPkg, parentVersion)
}
}
}
if len(version.Deps) > 0 {
fmt.Printf("\t\t%s Deps %s\n", strings.Repeat("-", 5), strings.Repeat("-", 5))
for parentPkg, parentVersion := range version.Deps {
fmt.Printf("\t\t%s (%s)\n", parentPkg, parentVersion)
}
}
}
}