Changed upgrade logic to use alpm.pkg

This commit is contained in:
Jguer 2017-05-07 13:52:08 +01:00
parent a20646fa24
commit 1a58017817
3 changed files with 36 additions and 35 deletions

View File

@ -90,11 +90,10 @@ func Install(pkgName []string, flags []string) (err error) {
func Upgrade(flags []string) error {
fmt.Println("\x1b[1;36;1m::\x1b[0m\x1b[1m Starting AUR upgrade...\x1b[0m")
foreign, n, err := pacman.ForeignPackages()
if err != nil || n == 0 {
foreign, err := pacman.ForeignPackages()
if err != nil {
return err
}
keys := make([]string, len(foreign))
i := 0
for k := range foreign {
@ -108,12 +107,14 @@ func Upgrade(flags []string) error {
}
outdated := q[:0]
for _, res := range q {
for i, res := range q {
fmt.Printf("\r Checking %d/%d packages...", i+1, len(q))
if _, ok := foreign[res.Name]; ok {
// Leaving this here for now, warn about downgrades later
if res.LastModified > int(foreign[res.Name].Date) {
if int64(res.LastModified) > foreign[res.Name].BuildDate().UnixNano() {
fmt.Printf("\x1b[1m\x1b[32m==>\x1b[33;1m %s: \x1b[0m%s \x1b[33;1m-> \x1b[0m%s\n",
res.Name, foreign[res.Name].Version, res.Version)
res.Name, foreign[res.Name].Version(), res.Version)
outdated = append(outdated, res)
}
}
@ -121,12 +122,12 @@ func Upgrade(flags []string) error {
//If there are no outdated packages, don't prompt
if len(outdated) == 0 {
fmt.Println(" there is nothing to do")
fmt.Println("\n there is nothing to do")
return nil
}
// Install updated packages
if !config.ContinueTask("Proceed with upgrade?", "nN") {
if !config.ContinueTask("\nProceed with upgrade?", "nN") {
return nil
}

View File

@ -23,11 +23,9 @@ func Search(pkgInputN []string) (s Query, n int, err error) {
initL := func(len int) int {
return len - 1
}
compL := func(len int, i int) bool {
return i > -1
}
finalL := func(i int) int {
return i - 1
}
@ -37,11 +35,9 @@ func Search(pkgInputN []string) (s Query, n int, err error) {
initL = func(len int) int {
return 0
}
compL = func(len int, i int) bool {
return i < len
}
finalL = func(i int) int {
return i + 1
}
@ -202,6 +198,14 @@ func DepSatisfier(toCheck []string) (repo []string, notFound []string, err error
return
}
// PkgNameSlice returns a slice of package names
// func (s Query) PkgNameSlice() (pkgNames []string) {
// for _, e := range s {
// pkgNames = append(pkgNames, e.Name())
// }
// return
// }
// CleanRemove sends a full removal command to pacman with the pkgName slice
func CleanRemove(pkgName []string) (err error) {
if len(pkgName) == 0 {
@ -213,11 +217,7 @@ func CleanRemove(pkgName []string) (err error) {
}
// ForeignPackages returns a map of foreign packages, with their version and date as values.
func ForeignPackages() (foreign map[string]*struct {
Version string
Date int64
}, n int, err error) {
func ForeignPackages() (foreign map[string]alpm.Package, err error) {
localDb, err := config.AlpmHandle.LocalDb()
if err != nil {
return
@ -227,31 +227,28 @@ func ForeignPackages() (foreign map[string]*struct {
return
}
foreign = make(map[string]*struct {
Version string
Date int64
})
// Find foreign packages in system
for _, pkg := range localDb.PkgCache().Slice() {
// Change to more effective method
foreign = make(map[string]alpm.Package)
f := func(k alpm.Package) error {
found := false
for _, db := range dbList.Slice() {
_, err = db.PkgByName(pkg.Name())
dbList.ForEach(func(d alpm.Db) error {
if found {
return nil
}
_, err = d.PkgByName(k.Name())
if err == nil {
found = true
break
}
}
return nil
})
if !found {
foreign[pkg.Name()] = &struct {
Version string
Date int64
}{pkg.Version(), pkg.InstallDate().Unix()}
n++
foreign[k.Name()] = k
}
return nil
}
err = localDb.PkgCache().ForEach(f)
return
}

View File

@ -96,12 +96,15 @@ func localStatistics(version string) error {
return err
}
foreignS, foreign, _ := pac.ForeignPackages()
foreignS, err := pac.ForeignPackages()
if err != nil {
return err
}
fmt.Printf("\n Yay version r%s\n", version)
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", info.Totaln)
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", foreign)
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", len(foreignS))
fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", info.Expln)
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", size(info.TotalSize))
fmt.Println("\x1B[1;34m===========================================\x1B[0m")