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 22, 2025
1 parent e2e61b8 commit 33cbe69
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 109 deletions.
133 changes: 34 additions & 99 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 All @@ -17,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 @@ -170,31 +165,15 @@ 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
Expand All @@ -220,82 +199,38 @@ func GetAvailableVersions(program string, dir 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
}
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
latestVersion := slices.MaxFunc(versions, func(a, b version.Version) int {
if a.Str == b.Str {
return 0
}

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 program + latestVersion.Str, nil
}

// switchProgramToLatestVersion switches the active version of the program to the latest installed.
Expand All @@ -305,7 +240,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
20 changes: 10 additions & 10 deletions cli/uninstall/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestGetAvailableVersions(t *testing.T) {
func TestSearchLatestVersion(t *testing.T) {
type testCase struct {
name string
linkName string
program string
binDst string
headerDst string
expectedVer string
Expand All @@ -75,57 +75,57 @@ func TestSearchLatestVersion(t *testing.T) {
cases := []testCase{
{
name: "basic",
linkName: "tarantool",
program: "tarantool",
binDst: "./testdata/bin_basic",
headerDst: "./testdata/inc_basic",
expectedVer: "tarantool_3.0.0-entrypoint",
},
{
name: "no includes",
linkName: "tarantool",
program: "tarantool-ee",
binDst: "./testdata/bin_basic",
headerDst: "./testdata/inc_invalid",
expectedVer: "tarantool-ee_2.8.4-0-r510",
},
{
name: "tarantool-dev",
linkName: "tarantool",
program: "tarantool-dev",
binDst: "./testdata/bin_dev",
headerDst: "./testdata/inc_basic",
expectedVer: "",
},
{
name: "hash version",
linkName: "tarantool",
program: "tarantool",
binDst: "./testdata/bin_hash",
headerDst: "./testdata/inc_hash",
expectedVer: "tarantool_aaaaaaa",
},
{
name: "hash invalid headers",
linkName: "tarantool",
program: "tarantool",
binDst: "./testdata/bin_hash",
headerDst: "./testdata/inc_invalid_hash",
expectedVer: "tarantool_bbbbbbb",
},
{
name: "tt, include-dir basic",
linkName: "tt",
program: "tt",
binDst: "./testdata/bin_basic",
headerDst: "./testdata/inc_basic",
expectedVer: "tt_2.0.0",
},
// Test that include dir doesn't affect the search for `tt`.
{
name: "tt, include-dir invalid",
linkName: "tt",
program: "tt",
binDst: "./testdata/bin_basic",
headerDst: "./testdata/inc_invalid",
expectedVer: "tt_2.0.0",
},
{
name: "filename as a bin dir",
linkName: "tt",
program: "tt",
binDst: "./testdata/bin_basic/tarantool",
headerDst: "./testdata/inc_basic",
isErr: true,
Expand All @@ -134,7 +134,7 @@ func TestSearchLatestVersion(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ver, err := searchLatestVersion(tc.linkName, tc.binDst, tc.headerDst)
ver, err := searchLatestVersion(tc.program, tc.binDst, tc.headerDst)
if !tc.isErr {
assert.NoError(t, err)
assert.Equal(t, tc.expectedVer, ver)
Expand Down

0 comments on commit 33cbe69

Please sign in to comment.