Skip to content

Commit

Permalink
uninstall: simplify GetAvailableVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
elhimov committed Jan 20, 2025
1 parent ec9485c commit 6476d84
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions cli/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ import (
)

const (
progRegexp = "(?P<prog>" +
search.ProgramTt + "|" +
search.ProgramCe + "|" +
search.ProgramEe + ")"
verRegexp = "(?P<ver>.*)"

MajorMinorPatchRegexp = `^[0-9]+\.[0-9]+\.[0-9]+`
)

Expand Down Expand Up @@ -185,28 +179,23 @@ func getDefault(program, dir string) (string, error) {
// GetAvailableVersions returns a list of the program's versions installed into
// the 'dir' directory.
func GetAvailableVersions(program string, dir string) []string {
list := []string{}
re := regexp.MustCompile(
"^" + progRegexp + version.FsSeparator + verRegexp + "$",
)

if dir == "" {
return nil
}

installedPrograms, err := os.ReadDir(dir)
versionPrefix := filepath.Join(dir, program+version.FsSeparator)

programFiles, err := filepath.Glob(versionPrefix + "*")
if err != nil {
return nil
}

for _, file := range installedPrograms {
matches := util.FindNamedMatches(re, file.Name())
if len(matches) != 0 && matches["prog"] == program {
list = append(list, matches["ver"])
}
versions := []string{}
for _, file := range programFiles {
versions = append(versions, file[len(versionPrefix):])
}

return list
return versions
}

// searchLatestVersion searches for the latest installed version of the program.
Expand Down

0 comments on commit 6476d84

Please sign in to comment.