From e52bc4f2b21e6aa4ca4884414a12286bca1f6d4c Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 19 Dec 2020 17:23:59 +0100 Subject: [PATCH] Refactor: get systemdb from config, which knows which one to load --- cmd/database/create.go | 10 +--------- cmd/database/remove.go | 12 +----------- cmd/install.go | 10 +--------- cmd/reclaim.go | 11 +---------- cmd/replace.go | 10 +--------- cmd/search.go | 10 +--------- cmd/uninstall.go | 10 +--------- cmd/upgrade.go | 12 +----------- pkg/config/config.go | 11 +++++++++++ 9 files changed, 19 insertions(+), 77 deletions(-) diff --git a/cmd/database/create.go b/cmd/database/create.go index 1c4982c0..3b5174d9 100644 --- a/cmd/database/create.go +++ b/cmd/database/create.go @@ -17,7 +17,6 @@ package cmd_database import ( "io/ioutil" - "path/filepath" "github.com/mudler/luet/pkg/compiler" . "github.com/mudler/luet/pkg/logger" @@ -51,7 +50,7 @@ For reference, inspect a "metadata.yaml" file generated while running "luet buil }, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase + systemDB := LuetCfg.GetSystemDB() for _, a := range args { dat, err := ioutil.ReadFile(a) @@ -63,13 +62,6 @@ For reference, inspect a "metadata.yaml" file generated while running "luet buil Fatal("Failed reading yaml ", a, ": ", err.Error()) } - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - files := art.GetFiles() if _, err := systemDB.CreatePackage(art.GetCompileSpec().GetPackage()); err != nil { diff --git a/cmd/database/remove.go b/cmd/database/remove.go index 7d20e066..9cc50ffd 100644 --- a/cmd/database/remove.go +++ b/cmd/database/remove.go @@ -16,13 +16,10 @@ package cmd_database import ( - "path/filepath" - . "github.com/mudler/luet/pkg/logger" helpers "github.com/mudler/luet/cmd/helpers" . "github.com/mudler/luet/pkg/config" - pkg "github.com/mudler/luet/pkg/package" "github.com/spf13/cobra" ) @@ -44,7 +41,7 @@ This commands takes multiple packages as arguments and prunes their entries from }, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase + systemDB := LuetCfg.GetSystemDB() for _, a := range args { pack, err := helpers.ParsePackageStr(a) @@ -52,13 +49,6 @@ This commands takes multiple packages as arguments and prunes their entries from Fatal("Invalid package string ", a, ": ", err.Error()) } - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - if err := systemDB.RemovePackage(pack); err != nil { Fatal("Failed removing ", a, ": ", err.Error()) } diff --git a/cmd/install.go b/cmd/install.go index 6139d3a1..b4e74431 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -16,7 +16,6 @@ package cmd import ( "os" - "path/filepath" installer "github.com/mudler/luet/pkg/installer" "github.com/mudler/luet/pkg/solver" @@ -63,7 +62,6 @@ To force install a package: }, Run: func(cmd *cobra.Command, args []string) { var toInstall pkg.Packages - var systemDB pkg.PackageDatabase for _, a := range args { pack, err := helpers.ParsePackageStr(a) @@ -120,13 +118,7 @@ To force install a package: }) inst.Repositories(repos) - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} err := inst.Install(toInstall, system) if err != nil { Fatal("Error: " + err.Error()) diff --git a/cmd/reclaim.go b/cmd/reclaim.go index ae066389..96188b0b 100644 --- a/cmd/reclaim.go +++ b/cmd/reclaim.go @@ -16,13 +16,11 @@ package cmd import ( "os" - "path/filepath" installer "github.com/mudler/luet/pkg/installer" . "github.com/mudler/luet/pkg/config" . "github.com/mudler/luet/pkg/logger" - pkg "github.com/mudler/luet/pkg/package" "github.com/spf13/cobra" ) @@ -42,7 +40,6 @@ var reclaimCmd = &cobra.Command{ It scans the target file system, and if finds a match with a package available in the repositories, it marks as installed in the system database. `, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase // This shouldn't be necessary, but we need to unmarshal the repositories to a concrete struct, thus we need to port them back to the Repositories type repos := installer.Repositories{} @@ -65,13 +62,7 @@ It scans the target file system, and if finds a match with a package available i }) inst.Repositories(repos) - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} err := inst.Reclaim(system) if err != nil { Fatal("Error: " + err.Error()) diff --git a/cmd/replace.go b/cmd/replace.go index c8e51e25..4db266ca 100644 --- a/cmd/replace.go +++ b/cmd/replace.go @@ -16,7 +16,6 @@ package cmd import ( "os" - "path/filepath" installer "github.com/mudler/luet/pkg/installer" "github.com/mudler/luet/pkg/solver" @@ -54,7 +53,6 @@ var replaceCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { var toUninstall pkg.Packages var toAdd pkg.Packages - var systemDB pkg.PackageDatabase f := LuetCfg.Viper.GetStringSlice("for") stype := LuetCfg.Viper.GetString("solver.type") @@ -120,13 +118,7 @@ var replaceCmd = &cobra.Command{ }) inst.Repositories(repos) - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} err := inst.Swap(toUninstall, toAdd, system) if err != nil { Fatal("Error: " + err.Error()) diff --git a/cmd/search.go b/cmd/search.go index aa8de7e6..c8c23755 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -17,7 +17,6 @@ package cmd import ( "fmt" "os" - "path/filepath" "strings" "github.com/ghodss/yaml" @@ -112,7 +111,6 @@ Search can also return results in the terminal in different ways: as terminal ou LuetCfg.Viper.BindPFlag("solver.max_attempts", cmd.Flags().Lookup("solver-attempts")) }, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase var results Results if len(args) > 1 { Fatal("Wrong number of arguments (expected 1)") @@ -213,13 +211,7 @@ Search can also return results in the terminal in different ways: as terminal ou } } else { - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} var err error iMatches := pkg.Packages{} diff --git a/cmd/uninstall.go b/cmd/uninstall.go index d83b148e..c0660a9e 100644 --- a/cmd/uninstall.go +++ b/cmd/uninstall.go @@ -16,7 +16,6 @@ package cmd import ( "os" - "path/filepath" helpers "github.com/mudler/luet/cmd/helpers" . "github.com/mudler/luet/pkg/config" @@ -45,7 +44,6 @@ var uninstallCmd = &cobra.Command{ LuetCfg.Viper.BindPFlag("yes", cmd.Flags().Lookup("yes")) }, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase toRemove := []pkg.Package{} for _, a := range args { @@ -94,13 +92,7 @@ var uninstallCmd = &cobra.Command{ PreserveSystemEssentialData: true, }) - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} if err := inst.Uninstall(system, toRemove...); err != nil { Fatal("Error: " + err.Error()) diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 5a70d58f..d55a59bc 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -16,12 +16,10 @@ package cmd import ( "os" - "path/filepath" . "github.com/mudler/luet/pkg/config" installer "github.com/mudler/luet/pkg/installer" . "github.com/mudler/luet/pkg/logger" - pkg "github.com/mudler/luet/pkg/package" "github.com/mudler/luet/pkg/solver" "github.com/spf13/cobra" @@ -43,7 +41,6 @@ var upgradeCmd = &cobra.Command{ }, Long: `Upgrades packages in parallel`, Run: func(cmd *cobra.Command, args []string) { - var systemDB pkg.PackageDatabase repos := installer.Repositories{} for _, repo := range LuetCfg.SystemRepositories { @@ -97,14 +94,7 @@ var upgradeCmd = &cobra.Command{ }) inst.Repositories(repos) - if LuetCfg.GetSystem().DatabaseEngine == "boltdb" { - systemDB = pkg.NewBoltDatabase( - filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) - } else { - systemDB = pkg.NewInMemoryDatabase(true) - } - system := &installer.System{Database: systemDB, Target: LuetCfg.GetSystem().Rootfs} - + system := &installer.System{Database: LuetCfg.GetSystemDB(), Target: LuetCfg.GetSystem().Rootfs} if err := inst.Upgrade(system); err != nil { Fatal("Error: " + err.Error()) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 5e26d9db..1c3207b1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -28,6 +28,7 @@ import ( "time" "github.com/mudler/luet/pkg/helpers" + pkg "github.com/mudler/luet/pkg/package" solver "github.com/mudler/luet/pkg/solver" v "github.com/spf13/viper" @@ -276,6 +277,16 @@ func GenDefault(viper *v.Viper) { viper.SetDefault("solver.max_attempts", 9000) } +func (c *LuetConfig) GetSystemDB() pkg.PackageDatabase { + switch LuetCfg.GetSystem().DatabaseEngine { + case "boltdb": + return pkg.NewBoltDatabase( + filepath.Join(LuetCfg.GetSystem().GetSystemRepoDatabaseDirPath(), "luet.db")) + default: + return pkg.NewInMemoryDatabase(true) + } +} + func (c *LuetConfig) AddSystemRepository(r LuetRepository) { c.SystemRepositories = append(c.SystemRepositories, r) }