yay/query.go

134 lines
3.3 KiB
Go
Raw Normal View History

2017-04-29 19:12:12 +02:00
package main
import (
"fmt"
"github.com/jguer/yay/aur"
2017-05-06 19:32:33 +02:00
"github.com/jguer/yay/config"
2017-04-29 19:12:12 +02:00
pac "github.com/jguer/yay/pacman"
rpc "github.com/mikkeloscar/aur"
2017-04-29 19:12:12 +02:00
)
// PrintSearch handles printing search results in a given format
func printAURSearch(q aur.Query, start int) {
2017-05-06 19:32:33 +02:00
localDb, _ := config.AlpmHandle.LocalDb()
for i, res := range q {
var toprint string
2017-05-07 03:43:49 +02:00
if config.YayConf.SearchMode == config.NumberMenu {
if config.YayConf.SortMode == config.BottomUp {
toprint += fmt.Sprintf("%d ", len(q)+start-i-1)
} else {
toprint += fmt.Sprintf("%d ", start+i)
}
2017-05-07 03:43:49 +02:00
} else if config.YayConf.SearchMode == config.Minimal {
fmt.Println(res.Name)
continue
}
toprint += fmt.Sprintf("\x1b[1m%s/\x1b[33m%s \x1b[36m%s \x1b[0m(%d) ", "aur", res.Name, res.Version, res.NumVotes)
if res.Maintainer == "" {
toprint += fmt.Sprintf("\x1b[31;40m(Orphaned)\x1b[0m ")
}
if res.OutOfDate != 0 {
toprint += fmt.Sprintf("\x1b[31;40m(Out-of-date)\x1b[0m ")
}
if _, err := localDb.PkgByName(res.Name); err == nil {
toprint += fmt.Sprintf("\x1b[32;40mInstalled\x1b[0m")
}
toprint += "\n" + res.Description
fmt.Println(toprint)
}
return
}
2017-04-29 19:12:12 +02:00
// SyncSearch presents a query to the local repos and to the AUR.
func syncSearch(pkgS []string) (err error) {
aq, err := aur.NarrowSearch(pkgS, true)
2017-04-29 19:12:12 +02:00
if err != nil {
return err
}
pq, _, err := pac.Search(pkgS)
if err != nil {
return err
}
2017-05-07 03:43:49 +02:00
if config.YayConf.SortMode == config.BottomUp {
printAURSearch(aq, 0)
2017-04-29 19:12:12 +02:00
pq.PrintSearch()
} else {
pq.PrintSearch()
printAURSearch(aq, 0)
2017-04-29 19:12:12 +02:00
}
return nil
}
// SyncInfo serves as a pacman -Si for repo packages and AUR packages.
func syncInfo(pkgS []string, flags []string) (err error) {
2017-04-29 19:12:12 +02:00
aurS, repoS, err := pac.PackageSlices(pkgS)
if err != nil {
return
}
q, err := rpc.Info(aurS)
2017-04-29 19:12:12 +02:00
if err != nil {
fmt.Println(err)
}
for _, aurP := range q {
2017-05-06 19:32:33 +02:00
aur.PrintInfo(&aurP)
2017-04-29 19:12:12 +02:00
}
if len(repoS) != 0 {
2017-05-07 03:43:49 +02:00
err = config.PassToPacman("-Si", repoS, flags)
2017-04-29 19:12:12 +02:00
}
return
}
// LocalStatistics returns installed packages statistics.
func localStatistics(version string) error {
2017-04-29 19:12:12 +02:00
info, err := pac.Statistics()
if err != nil {
return err
}
foreignS, foreign, _ := pac.ForeignPackages()
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;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")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
pac.BiggestPackages()
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
keys := make([]string, len(foreignS))
i := 0
for k := range foreignS {
keys[i] = k
i++
}
q, err := rpc.Info(keys)
2017-04-29 19:12:12 +02:00
if err != nil {
return err
}
for _, res := range q {
if res.Maintainer == "" {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is orphaned.\x1b[0m\n", res.Name)
}
if res.OutOfDate != 0 {
fmt.Printf("\x1b[1;31;40mWarning: \x1B[1;33;40m%s\x1b[0;37;40m is out-of-date in AUR.\x1b[0m\n", res.Name)
}
}
return nil
}