query.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "sync"
  7. alpm "github.com/jguer/go-alpm"
  8. rpc "github.com/mikkeloscar/aur"
  9. )
  10. // Query is a collection of Results
  11. type aurQuery []rpc.Pkg
  12. // Query holds the results of a repository search.
  13. type repoQuery []alpm.Package
  14. func (q aurQuery) Len() int {
  15. return len(q)
  16. }
  17. func (q aurQuery) Less(i, j int) bool {
  18. if config.SortMode == BottomUp {
  19. return q[i].NumVotes < q[j].NumVotes
  20. }
  21. return q[i].NumVotes > q[j].NumVotes
  22. }
  23. func (q aurQuery) Swap(i, j int) {
  24. q[i], q[j] = q[j], q[i]
  25. }
  26. // FilterPackages filters packages based on source and type from local repository.
  27. func filterPackages() (local []alpm.Package, remote []alpm.Package,
  28. localNames []string, remoteNames []string, err error) {
  29. localDb, err := alpmHandle.LocalDb()
  30. if err != nil {
  31. return
  32. }
  33. dbList, err := alpmHandle.SyncDbs()
  34. if err != nil {
  35. return
  36. }
  37. f := func(k alpm.Package) error {
  38. found := false
  39. // For each DB search for our secret package.
  40. _ = dbList.ForEach(func(d alpm.Db) error {
  41. if found {
  42. return nil
  43. }
  44. _, err := d.PkgByName(k.Name())
  45. if err == nil {
  46. found = true
  47. local = append(local, k)
  48. localNames = append(localNames, k.Name())
  49. }
  50. return nil
  51. })
  52. if !found {
  53. remote = append(remote, k)
  54. remoteNames = append(remoteNames, k.Name())
  55. }
  56. return nil
  57. }
  58. err = localDb.PkgCache().ForEach(f)
  59. return
  60. }
  61. // NarrowSearch searches AUR and narrows based on subarguments
  62. func narrowSearch(pkgS []string, sortS bool) (aurQuery, error) {
  63. if len(pkgS) == 0 {
  64. return nil, nil
  65. }
  66. r, err := rpc.Search(pkgS[0])
  67. if err != nil {
  68. return nil, err
  69. }
  70. if len(pkgS) == 1 {
  71. if sortS {
  72. sort.Sort(aurQuery(r))
  73. }
  74. return r, err
  75. }
  76. var aq aurQuery
  77. var n int
  78. for _, res := range r {
  79. match := true
  80. for _, pkgN := range pkgS[1:] {
  81. if !(strings.Contains(res.Name, pkgN) || strings.Contains(strings.ToLower(res.Description), pkgN)) {
  82. match = false
  83. break
  84. }
  85. }
  86. if match {
  87. n++
  88. aq = append(aq, res)
  89. }
  90. }
  91. if sortS {
  92. sort.Sort(aq)
  93. }
  94. return aq, err
  95. }
  96. // SyncSearch presents a query to the local repos and to the AUR.
  97. func syncSearch(pkgS []string) (err error) {
  98. aq, err := narrowSearch(pkgS, true)
  99. if err != nil {
  100. return err
  101. }
  102. pq, _, err := queryRepo(pkgS)
  103. if err != nil {
  104. return err
  105. }
  106. if config.SortMode == BottomUp {
  107. aq.printSearch(1)
  108. pq.printSearch()
  109. } else {
  110. pq.printSearch()
  111. aq.printSearch(1)
  112. }
  113. return nil
  114. }
  115. // SyncInfo serves as a pacman -Si for repo packages and AUR packages.
  116. func syncInfo(pkgS []string) (err error) {
  117. var info []rpc.Pkg
  118. aurS, repoS, err := packageSlices(pkgS)
  119. if err != nil {
  120. return
  121. }
  122. if len(aurS) != 0 {
  123. noDb := make([]string, 0, len(aurS))
  124. for _, pkg := range aurS {
  125. _, name := splitDbFromName(pkg)
  126. noDb = append(noDb, name)
  127. }
  128. info, err = aurInfo(noDb)
  129. if err != nil {
  130. fmt.Println(err)
  131. }
  132. }
  133. // Repo always goes first
  134. if len(repoS) != 0 {
  135. arguments := cmdArgs.copy()
  136. arguments.delTarget(aurS...)
  137. err = passToPacman(arguments)
  138. if err != nil {
  139. return
  140. }
  141. }
  142. if len(aurS) != 0 {
  143. for _, pkg := range info {
  144. PrintInfo(&pkg)
  145. }
  146. }
  147. return
  148. }
  149. // Search handles repo searches. Creates a RepoSearch struct.
  150. func queryRepo(pkgInputN []string) (s repoQuery, n int, err error) {
  151. dbList, err := alpmHandle.SyncDbs()
  152. if err != nil {
  153. return
  154. }
  155. // BottomUp functions
  156. initL := func(len int) int {
  157. if config.SortMode == TopDown {
  158. return 0
  159. }
  160. return len - 1
  161. }
  162. compL := func(len int, i int) bool {
  163. if config.SortMode == TopDown {
  164. return i < len
  165. }
  166. return i > -1
  167. }
  168. finalL := func(i int) int {
  169. if config.SortMode == TopDown {
  170. return i + 1
  171. }
  172. return i - 1
  173. }
  174. dbS := dbList.Slice()
  175. lenDbs := len(dbS)
  176. for f := initL(lenDbs); compL(lenDbs, f); f = finalL(f) {
  177. pkgS := dbS[f].PkgCache().Slice()
  178. lenPkgs := len(pkgS)
  179. for i := initL(lenPkgs); compL(lenPkgs, i); i = finalL(i) {
  180. match := true
  181. for _, pkgN := range pkgInputN {
  182. if !(strings.Contains(pkgS[i].Name(), pkgN) || strings.Contains(strings.ToLower(pkgS[i].Description()), pkgN)) {
  183. match = false
  184. break
  185. }
  186. }
  187. if match {
  188. n++
  189. s = append(s, pkgS[i])
  190. }
  191. }
  192. }
  193. return
  194. }
  195. // PackageSlices separates an input slice into aur and repo slices
  196. func packageSlices(toCheck []string) (aur []string, repo []string, err error) {
  197. dbList, err := alpmHandle.SyncDbs()
  198. if err != nil {
  199. return
  200. }
  201. for _, _pkg := range toCheck {
  202. db, name := splitDbFromName(_pkg)
  203. found := false
  204. if db == "aur" {
  205. aur = append(aur, _pkg)
  206. continue
  207. } else if db != "" {
  208. repo = append(repo, _pkg)
  209. continue
  210. }
  211. _ = dbList.ForEach(func(db alpm.Db) error {
  212. _, err := db.PkgByName(name)
  213. if err == nil {
  214. found = true
  215. return fmt.Errorf("")
  216. }
  217. return nil
  218. })
  219. if !found {
  220. _, errdb := dbList.PkgCachebyGroup(name)
  221. found = errdb == nil
  222. }
  223. if found {
  224. repo = append(repo, _pkg)
  225. } else {
  226. aur = append(aur, _pkg)
  227. }
  228. }
  229. return
  230. }
  231. // HangingPackages returns a list of packages installed as deps
  232. // and unneeded by the system
  233. func hangingPackages() (hanging []string, err error) {
  234. localDb, err := alpmHandle.LocalDb()
  235. if err != nil {
  236. return
  237. }
  238. f := func(pkg alpm.Package) error {
  239. if pkg.Reason() != alpm.PkgReasonDepend {
  240. return nil
  241. }
  242. requiredby := pkg.ComputeRequiredBy()
  243. if len(requiredby) == 0 {
  244. hanging = append(hanging, pkg.Name())
  245. fmt.Println(pkg.Name() + ": " + magenta(human(pkg.ISize())))
  246. }
  247. return nil
  248. }
  249. err = localDb.PkgCache().ForEach(f)
  250. return
  251. }
  252. // Statistics returns statistics about packages installed in system
  253. func statistics() (info struct {
  254. Totaln int
  255. Expln int
  256. TotalSize int64
  257. }, err error) {
  258. var tS int64 // TotalSize
  259. var nPkg int
  260. var ePkg int
  261. localDb, err := alpmHandle.LocalDb()
  262. if err != nil {
  263. return
  264. }
  265. for _, pkg := range localDb.PkgCache().Slice() {
  266. tS += pkg.ISize()
  267. nPkg++
  268. if pkg.Reason() == 0 {
  269. ePkg++
  270. }
  271. }
  272. info = struct {
  273. Totaln int
  274. Expln int
  275. TotalSize int64
  276. }{
  277. nPkg, ePkg, tS,
  278. }
  279. return
  280. }
  281. // Queries the aur for information about specified packages.
  282. // All packages should be queried in a single rpc request except when the number
  283. // of packages exceeds the number set in config.RequestSplitN.
  284. // If the number does exceed config.RequestSplitN multiple rpc requests will be
  285. // performed concurrently.
  286. func aurInfo(names []string) ([]rpc.Pkg, error) {
  287. info := make([]rpc.Pkg, 0, len(names))
  288. seen := make(map[string]int)
  289. var mux sync.Mutex
  290. var wg sync.WaitGroup
  291. var err error
  292. missing := make([]string, 0, len(names))
  293. orphans := make([]string, 0, len(names))
  294. outOfDate := make([]string, 0, len(names))
  295. makeRequest := func(n, max int) {
  296. defer wg.Done()
  297. tempInfo, requestErr := rpc.Info(names[n:max])
  298. if err != nil {
  299. return
  300. }
  301. if requestErr != nil {
  302. err = requestErr
  303. return
  304. }
  305. mux.Lock()
  306. info = append(info, tempInfo...)
  307. mux.Unlock()
  308. }
  309. for n := 0; n < len(names); n += config.RequestSplitN {
  310. max := min(len(names), n+config.RequestSplitN)
  311. wg.Add(1)
  312. go makeRequest(n, max)
  313. }
  314. wg.Wait()
  315. if err != nil {
  316. return info, err
  317. }
  318. for k, pkg := range info {
  319. seen[pkg.Name] = k
  320. }
  321. for _, name := range names {
  322. i, ok := seen[name]
  323. if !ok {
  324. missing = append(missing, name)
  325. continue
  326. }
  327. pkg := info[i]
  328. if pkg.Maintainer == "" {
  329. orphans = append(orphans, name)
  330. }
  331. if pkg.OutOfDate != 0 {
  332. outOfDate = append(outOfDate, name)
  333. }
  334. }
  335. if len(missing) > 0 {
  336. fmt.Print(bold(red(arrow + " Missing AUR Packages:")))
  337. for _, name := range missing {
  338. fmt.Print(" " + bold(magenta(name)))
  339. }
  340. fmt.Println()
  341. }
  342. if len(orphans) > 0 {
  343. fmt.Print(bold(red(arrow + " Orphaned AUR Packages:")))
  344. for _, name := range orphans {
  345. fmt.Print(" " + bold(magenta(name)))
  346. }
  347. fmt.Println()
  348. }
  349. if len(outOfDate) > 0 {
  350. fmt.Print(bold(red(arrow + " Out Of Date AUR Packages:")))
  351. for _, name := range outOfDate {
  352. fmt.Print(" " + bold(magenta(name)))
  353. }
  354. fmt.Println()
  355. }
  356. return info, nil
  357. }