diff --git a/.gitignore b/.gitignore index 61a3123..48c670d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,5 @@ rz-pm rz-pm coverage.txt + +__debug_.* diff --git a/README.md b/README.md index 3a3d698..8735e53 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.go b/main.go index e8e6444..f86a165 100644 --- a/main.go +++ b/main.go @@ -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: "should update the DB?", + Value: true, + }, } app.Before = func(c *cli.Context) error { diff --git a/pkg/database.go b/pkg/database.go index 92dfd05..318bee6 100644 --- a/pkg/database.go +++ b/pkg/database.go @@ -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, shouldUpdateDB 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 shouldUpdateDB { + err := d.updateDatabase(rizinVersion) + if err != nil { + return Database{}, fmt.Errorf("could not download the rz-pm database") + } } return d, nil diff --git a/pkg/package.go b/pkg/package.go index ff627ad..0dcbbb4 100644 --- a/pkg/package.go +++ b/pkg/package.go @@ -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 { diff --git a/pkg/site.go b/pkg/site.go index c62f295..9c7d301 100644 --- a/pkg/site.go +++ b/pkg/site.go @@ -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, shouldUpdateDB 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, shouldUpdateDB) if err != nil { return &RizinSite{}, err } diff --git a/pkg/site_test.go b/pkg/site_test.go index d0221a5..458620e 100644 --- a/pkg/site_test.go +++ b/pkg/site_test.go @@ -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"}