2020-08-01 01:20:00 +02:00
|
|
|
package upgrade
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-11 18:41:34 +02:00
|
|
|
"strings"
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/db"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/intrange"
|
2023-04-11 23:15:47 +02:00
|
|
|
"github.com/Jguer/yay/v12/pkg/query"
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/text"
|
2020-08-01 01:20:00 +02:00
|
|
|
)
|
|
|
|
|
2021-02-05 22:47:03 +01:00
|
|
|
// Filter decides if specific package should be included in theincluded in the results.
|
2023-02-17 20:01:26 +01:00
|
|
|
type Filter func(*Upgrade) bool
|
2021-02-05 22:47:03 +01:00
|
|
|
|
2020-08-01 01:20:00 +02:00
|
|
|
// Upgrade type describes a system upgrade.
|
2021-02-16 16:49:38 +01:00
|
|
|
type Upgrade = db.Upgrade
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2022-11-15 15:44:50 +01:00
|
|
|
func StylizedNameWithRepository(u *Upgrade) string {
|
2020-08-01 01:20:00 +02:00
|
|
|
return text.Bold(text.ColorHash(u.Repository)) + "/" + text.Bold(u.Name)
|
|
|
|
}
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
// upSlice is a slice of Upgrades.
|
2021-04-19 13:23:40 +02:00
|
|
|
type UpSlice struct {
|
2021-04-19 13:43:13 +02:00
|
|
|
Up []Upgrade
|
|
|
|
Repos []string
|
2021-04-19 13:23:40 +02:00
|
|
|
}
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2021-04-19 13:23:40 +02:00
|
|
|
func (u UpSlice) Len() int { return len(u.Up) }
|
|
|
|
func (u UpSlice) Swap(i, j int) { u.Up[i], u.Up[j] = u.Up[j], u.Up[i] }
|
2020-08-01 01:20:00 +02:00
|
|
|
|
|
|
|
func (u UpSlice) Less(i, j int) bool {
|
2021-04-19 13:43:13 +02:00
|
|
|
if u.Up[i].Repository == u.Up[j].Repository {
|
|
|
|
iRunes := []rune(u.Up[i].Name)
|
|
|
|
jRunes := []rune(u.Up[j].Name)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-01 01:20:00 +02:00
|
|
|
return text.LessRunes(iRunes, jRunes)
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:43:13 +02:00
|
|
|
for _, db := range u.Repos {
|
|
|
|
if db == u.Up[i].Repository {
|
|
|
|
return true
|
|
|
|
} else if db == u.Up[j].Repository {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iRunes := []rune(u.Up[i].Repository)
|
|
|
|
jRunes := []rune(u.Up[j].Repository)
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2020-08-01 01:20:00 +02:00
|
|
|
return text.LessRunes(iRunes, jRunes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print prints the details of the packages to upgrade.
|
2023-02-17 20:01:26 +01:00
|
|
|
func (u UpSlice) Print(logger *text.Logger) {
|
2020-08-01 01:20:00 +02:00
|
|
|
longestName, longestVersion := 0, 0
|
2021-08-11 20:13:28 +02:00
|
|
|
|
2022-11-15 15:44:50 +01:00
|
|
|
for k := range u.Up {
|
|
|
|
upgrade := &u.Up[k]
|
|
|
|
packNameLen := len(StylizedNameWithRepository(upgrade))
|
2023-04-11 23:15:47 +02:00
|
|
|
packVersion, _ := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
|
2020-08-01 01:20:00 +02:00
|
|
|
packVersionLen := len(packVersion)
|
|
|
|
longestName = intrange.Max(packNameLen, longestName)
|
|
|
|
longestVersion = intrange.Max(packVersionLen, longestVersion)
|
|
|
|
}
|
|
|
|
|
2023-04-11 18:41:34 +02:00
|
|
|
lenUp := len(u.Up)
|
|
|
|
longestNumber := len(fmt.Sprintf("%v", lenUp))
|
2020-08-01 01:20:00 +02:00
|
|
|
namePadding := fmt.Sprintf("%%-%ds ", longestName)
|
|
|
|
versionPadding := fmt.Sprintf("%%-%ds", longestVersion)
|
2023-04-11 18:41:34 +02:00
|
|
|
numberPadding := fmt.Sprintf("%%%dd ", longestNumber)
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2022-11-15 15:44:50 +01:00
|
|
|
for k := range u.Up {
|
|
|
|
upgrade := &u.Up[k]
|
2023-04-11 23:15:47 +02:00
|
|
|
left, right := query.GetVersionDiff(upgrade.LocalVersion, upgrade.RemoteVersion)
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2023-04-11 18:41:34 +02:00
|
|
|
logger.Printf(text.Magenta(fmt.Sprintf(numberPadding, lenUp-k)))
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2023-02-17 20:01:26 +01:00
|
|
|
logger.Printf(namePadding, StylizedNameWithRepository(upgrade))
|
2020-08-01 01:20:00 +02:00
|
|
|
|
2023-02-17 20:01:26 +01:00
|
|
|
logger.Printf("%s -> %s\n", fmt.Sprintf(versionPadding, left), right)
|
2023-04-11 18:41:34 +02:00
|
|
|
if upgrade.Extra != "" {
|
|
|
|
logger.Println(strings.Repeat(" ", longestNumber), upgrade.Extra)
|
|
|
|
}
|
2020-08-01 01:20:00 +02:00
|
|
|
}
|
|
|
|
}
|