-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
438 lines (391 loc) · 9.68 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/hashicorp/go-version"
"github.com/inconshreveable/go-update"
"github.com/rizinorg/rz-pm/pkg"
"github.com/urfave/cli/v2"
)
const (
debugEnvVar = "RZPM_DEBUG"
flagNameDebug = "debug"
flagSkipUpgrade = "skip-upgrade"
flagUpdateDB = "update-db"
)
func setDebug(value bool) {
if value {
log.SetOutput(os.Stderr)
} else {
log.SetOutput(ioutil.Discard)
}
}
func listPackages(c *cli.Context, installed bool) error {
if c.Args().Len() != 0 {
cli.ShowCommandHelp(c, "list")
return fmt.Errorf("wrong usage of list command")
}
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
var packages []pkg.Package
if installed {
packages, err = site.ListInstalledPackages()
} else {
packages, err = site.ListAvailablePackages()
}
if err != nil {
return err
}
green := color.New(color.Bold, color.FgGreen).SprintFunc()
red := color.New(color.Bold, color.FgRed).SprintFunc()
for _, myPkg := range packages {
info := ""
if site.IsPackageInstalled(myPkg) {
info = green(" [installed]")
installedPackage, err := site.GetInstalledPackage(myPkg.Name())
if err == nil && installedPackage.RizinVersion != nil {
if pkg.GetMajorMinorVersion(site.RizinVersion()) != *installedPackage.RizinVersion {
info += red(fmt.Sprintf(" [for rizin v%s]", *installedPackage.RizinVersion))
}
}
}
fmt.Printf("%s: %s%s\n", myPkg.Name(), myPkg.Summary(), info)
}
return nil
}
func listAvailablePackages(c *cli.Context) error {
return listPackages(c, false)
}
func listInstalledPackages(c *cli.Context) error {
return listPackages(c, true)
}
func infoPackage(c *cli.Context) error {
packageName := c.Args().First()
if packageName == "" || c.Args().Len() != 1 {
cli.ShowCommandHelp(c, "info")
return fmt.Errorf("wrong usage of info command")
}
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
var pkg pkg.Package
if c.Bool("file") {
pkg, err = site.GetPackageFromFile(packageName)
} else {
pkg, err = site.GetPackage(packageName)
}
if err != nil {
return err
}
var isInstalled string
if site.IsPackageInstalled(pkg) {
isInstalled = "yes"
} else {
isInstalled = "no"
}
fmt.Printf("Name: %s\n", pkg.Name())
fmt.Printf("Version: %s\n", pkg.Version())
fmt.Printf("Summary: %s\n", pkg.Summary())
fmt.Printf("Description: %s\n", pkg.Description())
fmt.Printf("Installed: %s\n", isInstalled)
return nil
}
func getNewRzPmVersion() (*version.Version, error) {
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get("https://github.com/rizinorg/rz-pm/releases/latest")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 302 {
return nil, fmt.Errorf("expected a redirection when querying releases/latest URL")
}
redirect_url, err := resp.Location()
if err != nil {
return nil, err
}
redirect_url_parts := strings.Split(redirect_url.Path, "/")
new_version_str := redirect_url_parts[len(redirect_url_parts)-1]
return version.NewVersion(new_version_str)
}
func getRzPmName() string {
name := "rz-pm-" + runtime.GOOS + "-"
if runtime.GOARCH == "amd64" {
name += "x86_64"
} else {
name += runtime.GOARCH
}
if runtime.GOOS == "windows" {
name += ".exe"
}
return name
}
func checkUpgrade(c *cli.Context) (bool, *version.Version, *version.Version, error) {
new_version, err := getNewRzPmVersion()
if err != nil {
return false, nil, nil, err
}
current_version, err := version.NewVersion(c.App.Version)
if err != nil {
return false, nil, nil, err
}
if new_version.LessThanOrEqual(current_version) {
return false, nil, nil, nil
}
return true, current_version, new_version, nil
}
func upgradeRzPm(c *cli.Context) error {
needsUpgrade, current_version, new_version, err := checkUpgrade(c)
if err != nil {
return nil
} else if !needsUpgrade {
fmt.Printf("You are already on the latest rz-pm version!\n")
return nil
}
fmt.Println("Your version of rz-pm is not the latest one.")
fmt.Printf("Currently installed version: %s, available version: %s\n", current_version, new_version)
fmt.Println("Downloading the new version...")
client := http.Client{}
rz_pm_name := getRzPmName()
resp, err := client.Get("https://github.com/rizinorg/rz-pm/releases/latest/download/" + rz_pm_name)
if err != nil {
return err
}
defer resp.Body.Close()
err = update.Apply(resp.Body, update.Options{})
if err != nil {
return err
}
fmt.Printf("Upgrade to rz-pm version %s was successful!\n", new_version)
return nil
}
func installPackages(c *cli.Context) error {
if c.Args().Len() < 1 {
cli.ShowCommandHelp(c, "install")
return fmt.Errorf("wrong usage of install command")
}
for _, packageName := range c.Args().Slice() {
if packageName == "" {
cli.ShowCommandHelp(c, "install")
return fmt.Errorf("wrong usage of install command")
}
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
var pkg pkg.Package
if c.Bool("file") {
pkg, err = site.GetPackageFromFile(packageName)
} else {
pkg, err = site.GetPackage(packageName)
}
if err != nil {
return err
}
if c.Bool("clean") {
site.CleanPackage(pkg)
}
err = site.InstallPackage(pkg)
if err != nil {
return err
}
}
return nil
}
func uninstallPackages(c *cli.Context) error {
if c.Args().Len() < 1 {
cli.ShowCommandHelp(c, "uninstall")
return fmt.Errorf("wrong usage of uninstall command")
}
for _, packageName := range c.Args().Slice() {
if packageName == "" {
cli.ShowCommandHelp(c, "uninstall")
return fmt.Errorf("wrong usage of uninstall command")
}
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
var pkg pkg.Package
if c.Bool("file") {
pkg, err = site.GetPackageFromFile(packageName)
} else {
pkg, err = site.GetPackage(packageName)
}
if err != nil {
return err
}
err = site.UninstallPackage(pkg)
if err != nil {
return err
}
}
return nil
}
func cleanPackage(c *cli.Context) error {
packageName := c.Args().First()
if packageName == "" || c.Args().Len() != 1 {
cli.ShowCommandHelp(c, "clean")
return fmt.Errorf("wrong usage of clean command")
}
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
var pkg pkg.Package
if c.Bool("file") {
pkg, err = site.GetPackageFromFile(packageName)
} else {
pkg, err = site.GetPackage(packageName)
}
if err != nil {
return err
}
err = site.CleanPackage(pkg)
if err != nil {
return err
}
fmt.Printf("Package %s build artifacts have been cleaned.\n", pkg.Name())
return nil
}
func main() {
cli.VersionFlag = &cli.BoolFlag{
Name: "print-version",
Aliases: []string{"V"},
Usage: "print only the version",
}
app := cli.NewApp()
app.Name = "rz-pm"
app.Usage = "Rizin package manager"
app.Version = "v0.3.3"
cli.AppHelpTemplate = fmt.Sprintf(`%s
RZ_PM_SITE:
%s
`, cli.AppHelpTemplate, pkg.SiteDir())
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: flagNameDebug,
Usage: "enable debug logs",
EnvVars: []string{debugEnvVar},
},
&cli.BoolFlag{
Name: flagSkipUpgrade,
Usage: "skip auto-upgrade on start",
},
&cli.BoolFlag{
Name: flagUpdateDB,
Usage: "Update the DB?",
Value: true,
},
}
app.Before = func(c *cli.Context) error {
setDebug(c.Bool(flagNameDebug))
if !c.Bool(flagSkipUpgrade) && c.Args().First() != "upgrade" {
needsUpgrade, current_version, new_version, err := checkUpgrade(c)
if err != nil {
return nil
} else if !needsUpgrade {
return nil
}
fmt.Println("Your version of rz-pm is not the latest one.")
fmt.Printf("Currently installed version: %s, available version: %s\n", current_version, new_version)
fmt.Println()
fmt.Println("Run the 'upgrade' command to upgrade rz-pm or use the '--skip-upgrade' flag.")
os.Exit(0)
}
return nil
}
app.Commands = []*cli.Command{
{
Name: "install",
Usage: "install a package",
ArgsUsage: "<package-name> [<package-name> ...]",
Action: installPackages,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clean",
Usage: "do a clean before installing the package",
},
&cli.BoolFlag{
Name: "file",
Usage: "install a local file(s)",
},
},
},
{
Name: "list",
Aliases: []string{"ls"},
Usage: "list packages",
Action: listAvailablePackages,
Subcommands: []*cli.Command{
{
Name: "available",
Usage: "list all available packages",
Action: listAvailablePackages,
},
{
Name: "installed",
Usage: "list installed packages",
Action: listInstalledPackages,
},
},
},
{
Name: "uninstall",
Usage: "uninstall a package",
ArgsUsage: "<package-name> [<package-name> ...]",
Action: uninstallPackages,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "file",
Usage: "info a local file",
},
},
},
{
Name: "clean",
Usage: "remove any temporary build artifacts of a package",
ArgsUsage: "<package-name>",
Action: cleanPackage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "file",
Usage: "info a local file",
},
},
},
{
Name: "info",
Usage: "info about a package",
Action: infoPackage,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "file",
Usage: "info a local file",
},
},
},
{
Name: "upgrade",
Usage: "upgrade rz-pm",
Action: upgradeRzPm,
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
}