Skip to content

Commit ec9485c

Browse files
committed
uninstall: reuse GetAvailableVersions
1 parent 2c50b24 commit ec9485c

File tree

1 file changed

+38
-97
lines changed

1 file changed

+38
-97
lines changed

cli/uninstall/uninstall.go

+38-97
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"path/filepath"
88
"regexp"
9+
"slices"
910
"strings"
1011

1112
"github.com/tarantool/tt/cli/install"
@@ -170,46 +171,30 @@ func getAllTtVersionFormats(programName, ttVersion string) ([]string, error) {
170171

171172
// getDefault returns a default version of an installed program.
172173
func getDefault(program, dir string) (string, error) {
173-
var ver string
174-
175-
re := regexp.MustCompile(
176-
"^" + program + version.FsSeparator + verRegexp + "$",
177-
)
178-
179-
installedPrograms, err := os.ReadDir(dir)
180-
if err != nil {
181-
return "", err
182-
}
183-
184-
for _, file := range installedPrograms {
185-
matches := util.FindNamedMatches(re, file.Name())
186-
if ver != "" {
187-
return "", fmt.Errorf("%s has more than one installed version, "+
188-
"please specify the version to uninstall", program)
189-
} else {
190-
ver = matches["ver"]
191-
}
192-
}
193-
194-
if ver == "" {
174+
versions := GetAvailableVersions(program, dir)
175+
if len(versions) == 0 {
195176
return "", fmt.Errorf("%s has no installed version", program)
196177
}
197-
return ver, nil
178+
if len(versions) > 1 {
179+
return "", fmt.Errorf("%s has more than one installed version, "+
180+
"please specify the version to uninstall", program)
181+
}
182+
return versions[0], nil
198183
}
199184

200185
// GetAvailableVersions returns a list of the program's versions installed into
201-
// the binDir directory.
202-
func GetAvailableVersions(program string, binDir string) []string {
186+
// the 'dir' directory.
187+
func GetAvailableVersions(program string, dir string) []string {
203188
list := []string{}
204189
re := regexp.MustCompile(
205190
"^" + progRegexp + version.FsSeparator + verRegexp + "$",
206191
)
207192

208-
if binDir == "" {
193+
if dir == "" {
209194
return nil
210195
}
211196

212-
installedPrograms, err := os.ReadDir(binDir)
197+
installedPrograms, err := os.ReadDir(dir)
213198
if err != nil {
214199
return nil
215200
}
@@ -225,82 +210,38 @@ func GetAvailableVersions(program string, binDir string) []string {
225210
}
226211

227212
// searchLatestVersion searches for the latest installed version of the program.
228-
func searchLatestVersion(linkName, binDst, headerDst string) (string, error) {
229-
var programsToSearch []string
230-
if linkName == "tarantool" {
231-
programsToSearch = []string{search.ProgramCe, search.ProgramEe}
232-
} else {
233-
programsToSearch = []string{linkName}
213+
func searchLatestVersion(program, binDst, headerDst string) (string, error) {
214+
binVersions := GetAvailableVersions(program, binDst)
215+
headerVersions := GetAvailableVersions(program, headerDst)
216+
217+
// Find intersection and convert to version.Version
218+
versions := []version.Version{}
219+
for _, binVersion := range binVersions {
220+
if slices.Contains(headerVersions, binVersion) {
221+
ver, err := version.Parse(binVersion)
222+
if err != nil {
223+
continue
224+
}
225+
versions = append(versions, ver)
226+
}
234227
}
235228

236-
programRegex := regexp.MustCompile(
237-
"^" + progRegexp + version.FsSeparator + verRegexp + "$",
238-
)
239-
240-
binaries, err := os.ReadDir(binDst)
241-
if err != nil {
242-
return "", err
229+
if len(versions) == 0 {
230+
return "", fmt.Errorf("no version found")
243231
}
244232

245-
latestVersionInfo := version.Version{}
246-
latestVersion := ""
247-
hashFound := false
248-
latestHash := ""
249-
250-
for _, binary := range binaries {
251-
if binary.IsDir() {
252-
continue
233+
latestVersion := slices.MaxFunc(versions, func(a, b version.Version) int {
234+
if a.Str == b.Str {
235+
return 0
253236
}
254-
binaryName := binary.Name()
255-
matches := util.FindNamedMatches(programRegex, binaryName)
256-
257-
// Need to match for the program and version.
258-
if len(matches) != 2 {
259-
log.Debugf("%q skipped: unexpected format", binaryName)
260-
continue
261-
}
262-
263-
programName := matches["prog"]
264-
// Need to find the program in the list of suitable.
265-
if util.Find(programsToSearch, programName) == -1 {
266-
continue
237+
isCommitHash, _ := util.IsValidCommitHash(a.Str)
238+
if isCommitHash || version.IsLess(a, b) {
239+
return -1
267240
}
268-
isRightFormat, _ := util.IsValidCommitHash(matches["ver"])
241+
return 1
242+
})
269243

270-
if isRightFormat {
271-
if hashFound {
272-
continue
273-
}
274-
if strings.Contains(programName, "tarantool") {
275-
// Check for headers.
276-
if _, err := os.Stat(filepath.Join(headerDst, binaryName)); os.IsNotExist(err) {
277-
continue
278-
}
279-
}
280-
hashFound = true
281-
latestHash = binaryName
282-
continue
283-
}
284-
ver, err := version.Parse(matches["ver"])
285-
if err != nil {
286-
continue
287-
}
288-
if strings.Contains(programName, "tarantool") {
289-
// Check for headers.
290-
if _, err := os.Stat(filepath.Join(headerDst, binaryName)); os.IsNotExist(err) {
291-
continue
292-
}
293-
}
294-
// Update latest version.
295-
if latestVersion == "" || version.IsLess(latestVersionInfo, ver) {
296-
latestVersionInfo = ver
297-
latestVersion = binaryName
298-
}
299-
}
300-
if latestVersion != "" {
301-
return latestVersion, nil
302-
}
303-
return latestHash, nil
244+
return latestVersion.Str, nil
304245
}
305246

306247
// switchProgramToLatestVersion switches the active version of the program to the latest installed.
@@ -310,7 +251,7 @@ func switchProgramToLatestVersion(program, binDst, headerDst string) error {
310251
linkName = "tarantool"
311252
}
312253

313-
progToSwitch, err := searchLatestVersion(linkName, binDst, headerDst)
254+
progToSwitch, err := searchLatestVersion(program, binDst, headerDst)
314255
if err != nil {
315256
return err
316257
}

0 commit comments

Comments
 (0)