query.go 8.0 KB

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