query.go 8.0 KB

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