Skip to content

Commit

Permalink
Add flag to disable DB update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
afjoseph authored and ret2libc committed Mar 26, 2024
1 parent b3fedae commit d31b330
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ rz-pm
rz-pm

coverage.txt

__debug_.*
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 16 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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"},
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 14 additions & 3 deletions pkg/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/site_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit d31b330

Please sign in to comment.