Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
refactor: introduce finder strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jun 10, 2023
1 parent 5408e9b commit 85acf39
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/pythonfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
pep440Version "github.com/aquasecurity/go-pep440-version"
)

// finderStrategy is the strategy used by the finder to find Python executables.
type finderStrategy int

const (
// findAll finds all Python executables.
findAll finderStrategy = iota
// findOne finds the one Python executable matching the given version.
findOne
)

// finder is a Python version finder.
type finder struct {
providers []Provider
Expand All @@ -25,7 +35,7 @@ func New() *finder {
// This function returns ErrVersionNotFound if no version matching the given
// version is found or there is no Python version installed on the system.
func (f *finder) Find(version string) (*PythonExecutable, error) {
versions, err := f.find(version, 1)
versions, err := f.find(version, findOne)
if err != nil {
return nil, err
}
Expand All @@ -37,10 +47,10 @@ func (f *finder) Find(version string) (*PythonExecutable, error) {
// FindAll returns all the Python versions available on the system which can be
// found by the providers.
func (f *finder) FindAll() ([]*PythonExecutable, error) {
return f.find("", -1)
return f.find("", findAll)
}

func (f *finder) find(version string, n int) ([]*PythonExecutable, error) {
func (f *finder) find(version string, strategy finderStrategy) ([]*PythonExecutable, error) {
var versionInfo *pep440Version.Version

if version != "" {
Expand Down Expand Up @@ -91,7 +101,7 @@ ProviderLoop:
versions = append(versions, pythonExecutable)
}

if n > 0 && len(versions) == n {
if strategy == findOne && len(versions) == 1 {
break ProviderLoop
}
}
Expand Down

0 comments on commit 85acf39

Please sign in to comment.