Add flag to disable DB update

And add a log line when downloading a package which is very useful to
tell the user the project didn't hang: it's just downloading slowly.
This commit is contained in:
afjoseph authored and Riccardo Schirone committed 2024-03-26 15:26:04 +01:00
1 parent b3fedae5e4
commit d31b330e38
7 files changed
+56 -22

No files matched your search

+2
View File
@@ -5,3 +5,5 @@ rz-pm
rz-pm
coverage.txt
__debug_.*
+12 -1
View File
@@ -16,7 +16,18 @@ Download the rz-pm binary for your system on the [latest release page](https://g
| **GithubCI** | [![Go](https://github.com/rizinorg/rz-pm/actions/workflows/go.yml/badge.svg)](https://github.com/rizinorg/rz-pm/actions/workflows/go.yml) |
# Available packages
The official database is available [here](https://github.com/rizinorg/rz-pm-db).
The official database is available [here](https://github.com/rizinorg/rz-pm-db). You can change the repo by supplying a custom `RZPM_DB_REPO_URL` environment flag like this:
```
$ RZPM_DB_REPO_URL=https://github.com/bunnyfoofoo/my-custom-rz-pm-db rz-pm install rz-custom-plugin
```
Furthermore, to aid with debugging, you can disable auto-updating the `rz-pm-db` upon each command execution by adding `-update-db=false` flag, like this:
```
$ rz-pm -update-db=false install rz-custom-plugin
```
## Package example
+16 -9
View File
@@ -16,7 +16,12 @@ import (
"github.com/urfave/cli/v2"
)
const debugEnvVar = "RZPM_DEBUG"
const (
debugEnvVar = "RZPM_DEBUG"
flagNameDebug = "debug"
flagSkipUpgrade = "skip-upgrade"
flagUpdateDB = "update-db"
)
func setDebug(value bool) {
if value {
@@ -32,7 +37,7 @@ func listPackages(c *cli.Context, installed bool) error {
return fmt.Errorf("wrong usage of list command")
}
site, err := pkg.InitSite(pkg.SiteDir())
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
@@ -79,7 +84,7 @@ func infoPackage(c *cli.Context) error {
return fmt.Errorf("wrong usage of info command")
}
site, err := pkg.InitSite(pkg.SiteDir())
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
@@ -203,7 +208,7 @@ func installPackages(c *cli.Context) error {
cli.ShowCommandHelp(c, "install")
return fmt.Errorf("wrong usage of install command")
}
site, err := pkg.InitSite(pkg.SiteDir())
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
@@ -241,7 +246,7 @@ func uninstallPackages(c *cli.Context) error {
return fmt.Errorf("wrong usage of uninstall command")
}
site, err := pkg.InitSite(pkg.SiteDir())
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
@@ -271,7 +276,7 @@ func cleanPackage(c *cli.Context) error {
return fmt.Errorf("wrong usage of clean command")
}
site, err := pkg.InitSite(pkg.SiteDir())
site, err := pkg.InitSite(pkg.SiteDir(), c.Bool(flagUpdateDB))
if err != nil {
return err
}
@@ -295,9 +300,6 @@ func cleanPackage(c *cli.Context) error {
}
func main() {
const flagNameDebug = "debug"
const flagSkipUpgrade = "skip-upgrade"
cli.VersionFlag = &cli.BoolFlag{
Name: "print-version",
Aliases: []string{"V"},
@@ -324,6 +326,11 @@ RZ_PM_SITE:
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 {
+6 -4
View File
@@ -24,12 +24,14 @@ var ErrRizinPackageWrongHash = errors.New("wrong hash")
const dbPath string = "db"
func InitDatabase(path string, rizinVersion string) (Database, error) {
func InitDatabase(path string, rizinVersion string, updateDB bool) (Database, error) {
d := Database{path}
err := d.updateDatabase(rizinVersion)
if err != nil {
return Database{}, fmt.Errorf("could not download the rz-pm database")
if updateDB {
err := d.updateDatabase(rizinVersion)
if err != nil {
return Database{}, fmt.Errorf("could not download the rz-pm database")
}
}
return d, nil
+1
View File
@@ -210,6 +210,7 @@ func (rp RizinPackage) downloadGit(artifactsPath string) error {
// Download the source code of a package and extract it in the provided path
func (rp RizinPackage) Download(baseArtifactsPath string) error {
log.Printf("Downloading package %s...\n", rp.PackageName)
artifactsPath := rp.artifactsPath(baseArtifactsPath)
err := os.MkdirAll(artifactsPath, os.FileMode(0755))
if err != nil {
+14 -3
View File
@@ -5,6 +5,7 @@ import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
@@ -25,7 +26,17 @@ func SiteDir() string {
return filepath.Join(xdg.DataHome, "rz-pm", "site")
}
const RZPM_DB_REPO_URL = "https://github.com/rizinorg/rz-pm-db"
var RZPM_DB_REPO_URL string
func init() {
dbURL := os.Getenv("RZPM_DB_REPO_URL")
if dbURL != "" {
log.Printf("Using custom rz-pm-db Git repo: %s\n", dbURL)
RZPM_DB_REPO_URL = dbURL
} else {
RZPM_DB_REPO_URL = "https://github.com/rizinorg/rz-pm-db"
}
}
type Site interface {
ListAvailablePackages() ([]Package, error)
@@ -64,7 +75,7 @@ const dbDir string = "rz-pm-db"
const artifactsDir string = "artifacts"
const installedFile string = "installed"
func InitSite(path string) (Site, error) {
func InitSite(path string, updateDB bool) (Site, error) {
// create the filesystem structure
dbSubdir := filepath.Join(path, dbDir)
artifactsSubdir := filepath.Join(path, artifactsDir)
@@ -90,7 +101,7 @@ func InitSite(path string) (Site, error) {
return &RizinSite{}, err
}
d, err := InitDatabase(dbSubdir, rizinVersion)
d, err := InitDatabase(dbSubdir, rizinVersion, updateDB)
if err != nil {
return &RizinSite{}, err
}
+5 -5
View File
@@ -23,7 +23,7 @@ func TestEmptySite(t *testing.T) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "rzpmtest")
require.NoError(t, err, "temp path should be created")
defer os.RemoveAll(tmpPath)
site, err := InitSite(tmpPath)
site, err := InitSite(tmpPath, true)
require.NoError(t, err, "site should be initialized in tmpPath %s", err)
assert.Equal(t, tmpPath, site.GetBaseDir(), "site path should be tmpPath")
_, err = os.Stat(filepath.Join(tmpPath, "rz-pm-db"))
@@ -38,9 +38,9 @@ func TestExistingSite(t *testing.T) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "rzpmtest")
require.Nil(t, err, "temp path should be created")
defer os.RemoveAll(tmpPath)
_, err = InitSite(tmpPath)
_, err = InitSite(tmpPath, true)
require.Nil(t, err, "site should be initialized when dir is empty")
_, err = InitSite(tmpPath)
_, err = InitSite(tmpPath, true)
assert.Nil(t, err, "site should be initialized even when dir is already initialized")
_, err = os.Stat(filepath.Join(tmpPath, "rz-pm-db", "README.md"))
assert.Nil(t, err, "rz-pm-db repository should be downloaded")
@@ -52,7 +52,7 @@ func TestListPackages(t *testing.T) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "rzpmtest")
require.Nil(t, err, "temp path should be created")
defer os.RemoveAll(tmpPath)
site, err := InitSite(tmpPath)
site, err := InitSite(tmpPath, true)
require.Nil(t, err, "site should be initialized when dir is empty")
packages, err := site.ListAvailablePackages()
@@ -176,7 +176,7 @@ func TestListInstalledPackages(t *testing.T) {
tmpPath, err := ioutil.TempDir(os.TempDir(), "rzpmtest")
require.Nil(t, err, "temp path should be created")
defer os.RemoveAll(tmpPath)
site, err := InitSite(tmpPath)
site, err := InitSite(tmpPath, true)
require.Nil(t, err, "site should be initialized when dir is empty")
pkg := FakePackage{myName: "jsdec"}