From 3cf8523549876a23b5e5f5f01ffc611fa2171118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Guid=C3=A9e?= Date: Thu, 7 Sep 2023 11:23:57 -0400 Subject: [PATCH] Refactor Packages GetByID methods --- repository/package_fs.go | 2 +- repository/package_fs_test.go | 4 +-- router/instance.go | 2 +- router/packages.go | 2 +- services/package.go | 52 +++++++++++++++++++---------------- services/package_test.go | 2 +- types/package.go | 2 +- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/repository/package_fs.go b/repository/package_fs.go index 51e7a824..b13815d1 100644 --- a/repository/package_fs.go +++ b/repository/package_fs.go @@ -50,7 +50,7 @@ func NewPackageFSRepository(params *PackageRepositoryParams) PackageFSRepository return repo } -func (r *PackageFSRepository) Get(id string) (types.Package, error) { +func (r *PackageFSRepository) GetByID(id string) (types.Package, error) { r.pkgsMutex.RLock() defer r.pkgsMutex.RUnlock() diff --git a/repository/package_fs_test.go b/repository/package_fs_test.go index d0c0aeea..2b122769 100644 --- a/repository/package_fs_test.go +++ b/repository/package_fs_test.go @@ -45,10 +45,10 @@ func (suite *PackageRepositoryTestSuite) TestReload() { } func (suite *PackageRepositoryTestSuite) TestGet() { - pkg, err := suite.repo.Get("redis") + pkg, err := suite.repo.GetByID("redis") assert.NoError(suite.T(), err) assert.Equal(suite.T(), pkg.Name, "Redis") - _, err = suite.repo.Get("undefined_package_name") + _, err = suite.repo.GetByID("undefined_package_name") assert.ErrorIs(suite.T(), err, ErrPkgNotFound) } diff --git a/router/instance.go b/router/instance.go index 60d97405..dca72fac 100644 --- a/router/instance.go +++ b/router/instance.go @@ -238,7 +238,7 @@ func handleGetDependencies(c *gin.Context) { if i.Methods.Script.Dependencies != nil { for name := range *i.Methods.Script.Dependencies { - dep, err := packageService.Get(name) + dep, err := packageService.GetByID(name) if err != nil { logger.Error(err).Print() continue diff --git a/router/packages.go b/router/packages.go index 90d37eb7..f3709edc 100644 --- a/router/packages.go +++ b/router/packages.go @@ -29,7 +29,7 @@ func handleInstallPackages(c *gin.Context) { } for _, d := range body.Packages { - pkg, err := packageService.Get(d.Name) + pkg, err := packageService.GetByID(d.Name) if err != nil { logger.Warn("dependency not found"). AddKeyValue("name", d.Name). diff --git a/services/package.go b/services/package.go index fde79e74..25a9141f 100644 --- a/services/package.go +++ b/services/package.go @@ -81,38 +81,44 @@ func (s *PackageService) InstallationCommand(p *types.Package, pm string) (Insta return InstallCmd{}, ErrPkgManagerNotFound } -func (s *PackageService) Get(id string) (types.Package, error) { - p, err := s.packageRepo.Get(id) +func (s *PackageService) GetByID(id string) (types.Package, error) { + pkg, err := s.packageRepo.GetByID(id) if err != nil { return types.Package{}, err } - pkgPath := s.packageRepo.GetPath(id) + installed, err := s.checkIsInstalled(id, pkg) + pkg.Installed = &installed + return pkg, err +} - isScript := strings.HasPrefix(p.Check, "script:") - installed := false +func (s *PackageService) checkIsInstalled(id string, pkg types.Package) (bool, error) { + pkgPath := s.packageRepo.GetPath(id) + isScript := strings.HasPrefix(pkg.Check, "script:") if isScript { - script := strings.Split(p.Check, ":")[1] - - cmd := exec.Command(path.Join(pkgPath, script)) - - err = cmd.Run() - if cmd.ProcessState.ExitCode() == 0 { - installed = true - } - if err != nil { - return types.Package{}, err - } - } else { - _, err := exec.LookPath(p.Check) - if err == nil { - installed = true - } + return s.checkIsInstalledWithScript(pkgPath, pkg.Check) + } + return s.checkIsInstalledWithCommand(pkg.Check) +} + +func (s *PackageService) checkIsInstalledWithScript(pkgPath, check string) (bool, error) { + script := strings.Split(check, ":")[1] + cmd := exec.Command(path.Join(pkgPath, script)) + + err := cmd.Run() + if err != nil || cmd.ProcessState.ExitCode() != 0 { + return false, err } + return true, nil +} - p.Installed = &installed - return p, nil +func (s *PackageService) checkIsInstalledWithCommand(cmd string) (bool, error) { + _, err := exec.LookPath(cmd) + if err != nil { + return false, err + } + return true, nil } func (s *PackageService) Reload() error { diff --git a/services/package_test.go b/services/package_test.go index 03e2d5b7..832abd82 100644 --- a/services/package_test.go +++ b/services/package_test.go @@ -61,7 +61,7 @@ type MockPackageRepository struct { mock.Mock } -func (m *MockPackageRepository) Get(id string) (types.Package, error) { +func (m *MockPackageRepository) GetByID(id string) (types.Package, error) { m.Called(id) return types.Package{}, nil } diff --git a/types/package.go b/types/package.go index 0ff3620b..e2a7a8c8 100644 --- a/types/package.go +++ b/types/package.go @@ -20,7 +20,7 @@ type Package struct { } type PackageRepository interface { - Get(id string) (Package, error) + GetByID(id string) (Package, error) GetPath(id string) string // Reload the repository