-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (154 loc) · 4.87 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
package main
import (
"context"
_ "embed"
"flag"
"fmt"
"io"
"os"
"os/signal"
"strings"
"syscall"
"github.com/celerway/openwrt-versions/openwrt"
"github.com/celerway/openwrt-versions/pkgidx"
)
//go:embed .version
var embeddedVersion string
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
if err := run(ctx, os.Args[1:]); err != nil {
fmt.Println("error: ", err)
}
os.Exit(0)
}
func run(ctx context.Context, args []string) error {
fmt.Printf("OpenWRT package version comparison tool version %s\n", embeddedVersion)
fs := flag.NewFlagSet("", flag.ContinueOnError)
version := fs.String("version", "", "Version for upstream")
verbose := fs.Bool("verbose", false, "Verbose output")
err := fs.Parse(args)
if err != nil {
return fmt.Errorf("parse flags: %w", err)
}
// check that the version is not empty
if *version == "" {
return fmt.Errorf("version cannot be empty")
}
baseUrl := openwrt.GetBasePackageUrl(*version)
addonUrl := openwrt.GetAddOnPackageUrl(*version)
luciUrl := openwrt.GetLuciPackageUrl(*version)
routingUrl := openwrt.GetRoutingPackageUrl(*version)
telephonyUrl := openwrt.GetTelephonyPackageUrl(*version)
UpstreamPackages := make(map[string]string)
packages, err := pkgidx.LoadFromURL(ctx, baseUrl)
if err != nil {
return fmt.Errorf("base packages: %w", err)
}
for name, pkg := range packages {
UpstreamPackages[name] = pkg
}
packages, err = pkgidx.LoadFromURL(ctx, addonUrl)
if err != nil {
return fmt.Errorf("addon packages: %w", err)
}
for name, pkg := range packages {
UpstreamPackages[name] = pkg
}
packages, err = pkgidx.LoadFromURL(ctx, luciUrl)
if err != nil {
return fmt.Errorf("luci packages: %w", err)
}
for name, pkg := range packages {
UpstreamPackages[name] = pkg
}
packages, err = pkgidx.LoadFromURL(ctx, routingUrl)
if err != nil {
return fmt.Errorf("routing packages: %w", err)
}
for name, pkg := range packages {
UpstreamPackages[name] = pkg
}
packages, err = pkgidx.LoadFromURL(ctx, telephonyUrl)
if err != nil {
return fmt.Errorf("telephony packages: %w", err)
}
for name, pkg := range packages {
UpstreamPackages[name] = pkg
}
// Load the downstream packages, see if there is an argument on the command line
// to read from a file instead of stdin
var input io.Reader
if len(fs.Args()) > 0 {
input, err = os.Open(fs.Arg(0))
if err != nil {
return fmt.Errorf("open file: %w", err)
}
} else {
input = os.Stdin
}
downstreamPackages, err := pkgidx.LoadFromManifest(input)
if err != nil {
return fmt.Errorf("downstream packages: %w", err)
}
// Compare the two package lists
differences, cwyOnly := downstreamPackages.Equals(UpstreamPackages)
if *verbose {
fmt.Println("OpenWRT version used (x86_64): ", *version)
fmt.Println("base url: ", baseUrl)
fmt.Println("addon url: ", addonUrl)
fmt.Println("Upstream packages count: ", len(UpstreamPackages))
fmt.Println("Downstream package count: ", len(downstreamPackages))
fmt.Println("Differences count: ", len(differences))
}
nameWidth := 0
upstreamWidth := 0
downstreamWidth := 0
for _, pkg := range differences {
if len(pkg.Name) > nameWidth {
nameWidth = len(pkg.Name)
}
if len(pkg.UpstreamVersion) > upstreamWidth {
upstreamWidth = len(pkg.UpstreamVersion)
}
if len(pkg.DownstreamVersion) > downstreamWidth {
downstreamWidth = len(pkg.DownstreamVersion)
}
}
// Print the header
fmt.Printf("%-*s | %-*s | %-*s\n", nameWidth, "Name", upstreamWidth, "Upstream", downstreamWidth, "Downstream")
fmt.Printf("%s-+-%s-+-%s\n", strings.Repeat("-", nameWidth), strings.Repeat("-", upstreamWidth), strings.Repeat("-", downstreamWidth))
// Print the package information
for _, pkg := range differences {
fmt.Printf("%-*s | %-*s | %-*s\n", nameWidth, pkg.Name, upstreamWidth, pkg.UpstreamVersion, downstreamWidth, pkg.DownstreamVersion)
}
fmt.Println()
fmt.Println("Packages only in CelerwayOS:")
fmt.Println("Press enter to see the packages")
_, err = fmt.Scanln()
if err != nil {
return fmt.Errorf("scanln: %w", err)
}
nameWidth = 0
upstreamWidth = 0
downstreamWidth = 0
for _, pkg := range cwyOnly {
if len(pkg.Name) > nameWidth {
nameWidth = len(pkg.Name)
}
if len(pkg.UpstreamVersion) > upstreamWidth {
upstreamWidth = len(pkg.UpstreamVersion)
}
if len(pkg.DownstreamVersion) > downstreamWidth {
downstreamWidth = len(pkg.DownstreamVersion)
}
}
// Print the header
fmt.Printf("%-*s | %-*s | %-*s\n", nameWidth, "Name", upstreamWidth, "Upstream", downstreamWidth, "Downstream")
fmt.Printf("%s-+-%s-+-%s\n", strings.Repeat("-", nameWidth), strings.Repeat("-", upstreamWidth), strings.Repeat("-", downstreamWidth))
// Print the package information
for _, pkg := range cwyOnly {
fmt.Printf("%-*s | %-*s | %-*s\n", nameWidth, pkg.Name, upstreamWidth, pkg.UpstreamVersion, downstreamWidth, pkg.DownstreamVersion)
}
return nil
}