query.go 8.1 KB

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