Skip to content

Commit

Permalink
uninstall: reuse GetAvailableVersions
Browse files Browse the repository at this point in the history
  • Loading branch information
elhimov committed Jan 20, 2025
1 parent 2c50b24 commit ec9485c
Showing 1 changed file with 38 additions and 97 deletions.
135 changes: 38 additions & 97 deletions cli/uninstall/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strings"

"github.com/tarantool/tt/cli/install"
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand All @@ -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
}
Expand Down

0 comments on commit ec9485c

Please sign in to comment.