query.go 8.4 KB

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