Skip to content

Commit

Permalink
Refactor Packages GetByID methods
Browse files Browse the repository at this point in the history
  • Loading branch information
quentinguidee committed Sep 7, 2023
1 parent 1124880 commit 3cf8523
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion repository/package_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions repository/package_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion router/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion router/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
52 changes: 29 additions & 23 deletions services/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion services/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cf8523

Please sign in to comment.