From ec9485c362dbb211cbc35553d85839c194bcf02a Mon Sep 17 00:00:00 2001 From: Mikhail Elhimov Date: Tue, 21 Jan 2025 01:00:53 +0300 Subject: [PATCH] uninstall: reuse `GetAvailableVersions` --- cli/uninstall/uninstall.go | 135 +++++++++++-------------------------- 1 file changed, 38 insertions(+), 97 deletions(-) diff --git a/cli/uninstall/uninstall.go b/cli/uninstall/uninstall.go index c3b98f5e5..b6ca33905 100644 --- a/cli/uninstall/uninstall.go +++ b/cli/uninstall/uninstall.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "regexp" + "slices" "strings" "github.com/tarantool/tt/cli/install" @@ -170,46 +171,30 @@ func getAllTtVersionFormats(programName, ttVersion string) ([]string, error) { // getDefault returns a default version of an installed program. func getDefault(program, dir string) (string, error) { - var ver string - - re := regexp.MustCompile( - "^" + program + version.FsSeparator + verRegexp + "$", - ) - - installedPrograms, err := os.ReadDir(dir) - if err != nil { - return "", err - } - - for _, file := range installedPrograms { - matches := util.FindNamedMatches(re, file.Name()) - if ver != "" { - return "", fmt.Errorf("%s has more than one installed version, "+ - "please specify the version to uninstall", program) - } else { - ver = matches["ver"] - } - } - - if ver == "" { + versions := GetAvailableVersions(program, dir) + if len(versions) == 0 { return "", fmt.Errorf("%s has no installed version", program) } - return ver, nil + if len(versions) > 1 { + return "", fmt.Errorf("%s has more than one installed version, "+ + "please specify the version to uninstall", program) + } + return versions[0], nil } // GetAvailableVersions returns a list of the program's versions installed into -// the binDir directory. -func GetAvailableVersions(program string, binDir string) []string { +// the 'dir' directory. +func GetAvailableVersions(program string, dir string) []string { list := []string{} re := regexp.MustCompile( "^" + progRegexp + version.FsSeparator + verRegexp + "$", ) - if binDir == "" { + if dir == "" { return nil } - installedPrograms, err := os.ReadDir(binDir) + installedPrograms, err := os.ReadDir(dir) if err != nil { return nil } @@ -225,82 +210,38 @@ func GetAvailableVersions(program string, binDir string) []string { } // searchLatestVersion searches for the latest installed version of the program. -func searchLatestVersion(linkName, binDst, headerDst string) (string, error) { - var programsToSearch []string - if linkName == "tarantool" { - programsToSearch = []string{search.ProgramCe, search.ProgramEe} - } else { - programsToSearch = []string{linkName} +func searchLatestVersion(program, binDst, headerDst string) (string, error) { + binVersions := GetAvailableVersions(program, binDst) + headerVersions := GetAvailableVersions(program, headerDst) + + // Find intersection and convert to version.Version + versions := []version.Version{} + for _, binVersion := range binVersions { + if slices.Contains(headerVersions, binVersion) { + ver, err := version.Parse(binVersion) + if err != nil { + continue + } + versions = append(versions, ver) + } } - programRegex := regexp.MustCompile( - "^" + progRegexp + version.FsSeparator + verRegexp + "$", - ) - - binaries, err := os.ReadDir(binDst) - if err != nil { - return "", err + if len(versions) == 0 { + return "", fmt.Errorf("no version found") } - latestVersionInfo := version.Version{} - latestVersion := "" - hashFound := false - latestHash := "" - - for _, binary := range binaries { - if binary.IsDir() { - continue + latestVersion := slices.MaxFunc(versions, func(a, b version.Version) int { + if a.Str == b.Str { + return 0 } - binaryName := binary.Name() - matches := util.FindNamedMatches(programRegex, binaryName) - - // Need to match for the program and version. - if len(matches) != 2 { - log.Debugf("%q skipped: unexpected format", binaryName) - continue - } - - programName := matches["prog"] - // Need to find the program in the list of suitable. - if util.Find(programsToSearch, programName) == -1 { - continue + isCommitHash, _ := util.IsValidCommitHash(a.Str) + if isCommitHash || version.IsLess(a, b) { + return -1 } - isRightFormat, _ := util.IsValidCommitHash(matches["ver"]) + return 1 + }) - if isRightFormat { - if hashFound { - continue - } - if strings.Contains(programName, "tarantool") { - // Check for headers. - if _, err := os.Stat(filepath.Join(headerDst, binaryName)); os.IsNotExist(err) { - continue - } - } - hashFound = true - latestHash = binaryName - continue - } - ver, err := version.Parse(matches["ver"]) - if err != nil { - continue - } - if strings.Contains(programName, "tarantool") { - // Check for headers. - if _, err := os.Stat(filepath.Join(headerDst, binaryName)); os.IsNotExist(err) { - continue - } - } - // Update latest version. - if latestVersion == "" || version.IsLess(latestVersionInfo, ver) { - latestVersionInfo = ver - latestVersion = binaryName - } - } - if latestVersion != "" { - return latestVersion, nil - } - return latestHash, nil + return latestVersion.Str, nil } // switchProgramToLatestVersion switches the active version of the program to the latest installed. @@ -310,7 +251,7 @@ func switchProgramToLatestVersion(program, binDst, headerDst string) error { linkName = "tarantool" } - progToSwitch, err := searchLatestVersion(linkName, binDst, headerDst) + progToSwitch, err := searchLatestVersion(program, binDst, headerDst) if err != nil { return err }