6
6
"os"
7
7
"path/filepath"
8
8
"regexp"
9
+ "slices"
9
10
"strings"
10
11
11
12
"github.com/tarantool/tt/cli/install"
@@ -170,46 +171,30 @@ func getAllTtVersionFormats(programName, ttVersion string) ([]string, error) {
170
171
171
172
// getDefault returns a default version of an installed program.
172
173
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 {
195
176
return "" , fmt .Errorf ("%s has no installed version" , program )
196
177
}
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
198
183
}
199
184
200
185
// 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 {
203
188
list := []string {}
204
189
re := regexp .MustCompile (
205
190
"^" + progRegexp + version .FsSeparator + verRegexp + "$" ,
206
191
)
207
192
208
- if binDir == "" {
193
+ if dir == "" {
209
194
return nil
210
195
}
211
196
212
- installedPrograms , err := os .ReadDir (binDir )
197
+ installedPrograms , err := os .ReadDir (dir )
213
198
if err != nil {
214
199
return nil
215
200
}
@@ -225,82 +210,38 @@ func GetAvailableVersions(program string, binDir string) []string {
225
210
}
226
211
227
212
// 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
+ }
234
227
}
235
228
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" )
243
231
}
244
232
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
253
236
}
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
267
240
}
268
- isRightFormat , _ := util .IsValidCommitHash (matches ["ver" ])
241
+ return 1
242
+ })
269
243
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
304
245
}
305
246
306
247
// switchProgramToLatestVersion switches the active version of the program to the latest installed.
@@ -310,7 +251,7 @@ func switchProgramToLatestVersion(program, binDst, headerDst string) error {
310
251
linkName = "tarantool"
311
252
}
312
253
313
- progToSwitch , err := searchLatestVersion (linkName , binDst , headerDst )
254
+ progToSwitch , err := searchLatestVersion (program , binDst , headerDst )
314
255
if err != nil {
315
256
return err
316
257
}
0 commit comments