yay.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/jguer/yay/aur"
  9. pac "github.com/jguer/yay/pacman"
  10. "github.com/jguer/yay/util"
  11. )
  12. func usage() {
  13. fmt.Println(`usage: yay <operation> [...]
  14. operations:
  15. yay {-h --help}
  16. yay {-V --version}
  17. yay {-D --database} <options> <package(s)>
  18. yay {-F --files} [options] [package(s)]
  19. yay {-Q --query} [options] [package(s)]
  20. yay {-R --remove} [options] <package(s)>
  21. yay {-S --sync} [options] [package(s)]
  22. yay {-T --deptest} [options] [package(s)]
  23. yay {-U --upgrade} [options] <file(s)>
  24. New operations:
  25. yay -Qstats displays system information
  26. yay -Cd remove unneeded dependencies
  27. yay -G [package(s)] get pkgbuild from ABS or AUR
  28. New options:
  29. --topdown shows repository's packages first and then aur's
  30. --bottomup shows aur's packages first and then repository's
  31. --noconfirm skip user input on package install
  32. `)
  33. }
  34. var version = "1.116"
  35. func parser() (op string, options []string, packages []string, err error) {
  36. if len(os.Args) < 2 {
  37. err = fmt.Errorf("no operation specified")
  38. return
  39. }
  40. op = "yogurt"
  41. for _, arg := range os.Args[1:] {
  42. if arg[0] == '-' && arg[1] != '-' {
  43. switch arg {
  44. case "-b":
  45. util.Build = true
  46. default:
  47. op = arg
  48. }
  49. continue
  50. }
  51. if arg[0] == '-' && arg[1] == '-' {
  52. switch arg {
  53. case "--build":
  54. util.Build = true
  55. case "--bottomup":
  56. util.SortMode = util.BottomUp
  57. case "--topdown":
  58. util.SortMode = util.TopDown
  59. case "--complete":
  60. util.Shell = "sh"
  61. complete()
  62. os.Exit(0)
  63. case "--fcomplete":
  64. util.Shell = "fish"
  65. complete()
  66. os.Exit(0)
  67. case "--help":
  68. usage()
  69. os.Exit(0)
  70. case "--noconfirm":
  71. util.NoConfirm = true
  72. fallthrough
  73. default:
  74. options = append(options, arg)
  75. }
  76. continue
  77. }
  78. packages = append(packages, arg)
  79. }
  80. return
  81. }
  82. func main() {
  83. op, options, pkgs, err := parser()
  84. if err != nil {
  85. fmt.Println(err)
  86. os.Exit(1)
  87. }
  88. switch op {
  89. case "-Cd":
  90. err = cleanDependencies(pkgs)
  91. case "-G":
  92. for _, pkg := range pkgs {
  93. err = getPkgbuild(pkg)
  94. if err != nil {
  95. fmt.Println(pkg+":", err)
  96. }
  97. }
  98. case "-Qstats":
  99. err = localStatistics(version)
  100. case "-Ss", "-Ssq", "-Sqs":
  101. if op == "-Ss" {
  102. util.SearchVerbosity = util.Detailed
  103. } else {
  104. util.SearchVerbosity = util.Minimal
  105. }
  106. if pkgs != nil {
  107. err = syncSearch(pkgs)
  108. }
  109. case "-S":
  110. err = install(pkgs, options)
  111. case "-Syu", "-Suy":
  112. err = upgrade(options)
  113. case "-Si":
  114. err = syncInfo(pkgs, options)
  115. case "yogurt":
  116. util.SearchVerbosity = util.NumberMenu
  117. if pkgs != nil {
  118. err = numberMenu(pkgs, options)
  119. }
  120. default:
  121. err = passToPacman(op, pkgs, options)
  122. }
  123. if err != nil {
  124. fmt.Println(err)
  125. os.Exit(1)
  126. }
  127. }
  128. // NumberMenu presents a CLI for selecting packages to install.
  129. func numberMenu(pkgS []string, flags []string) (err error) {
  130. var num int
  131. aq, err := aur.NarrowSearch(pkgS, true)
  132. if err != nil {
  133. fmt.Println("Error during AUR search:", err)
  134. }
  135. numaq := len(aq)
  136. pq, numpq, err := pac.Search(pkgS)
  137. if err != nil {
  138. return
  139. }
  140. if numpq == 0 && numaq == 0 {
  141. return fmt.Errorf("no packages match search")
  142. }
  143. if util.SortMode == util.BottomUp {
  144. printAURSearch(aq, numpq)
  145. pq.PrintSearch()
  146. } else {
  147. pq.PrintSearch()
  148. printAURSearch(aq, numpq)
  149. }
  150. fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
  151. reader := bufio.NewReader(os.Stdin)
  152. numberBuf, overflow, err := reader.ReadLine()
  153. if err != nil || overflow {
  154. fmt.Println(err)
  155. return
  156. }
  157. numberString := string(numberBuf)
  158. var aurInstall []string
  159. var repoInstall []string
  160. result := strings.Fields(numberString)
  161. for _, numS := range result {
  162. num, err = strconv.Atoi(numS)
  163. if err != nil {
  164. continue
  165. }
  166. // Install package
  167. if num > numaq+numpq-1 || num < 0 {
  168. continue
  169. } else if num > numpq-1 {
  170. if util.SortMode == util.BottomUp {
  171. aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
  172. } else {
  173. aurInstall = append(aurInstall, aq[num-numpq].Name)
  174. }
  175. } else {
  176. if util.SortMode == util.BottomUp {
  177. repoInstall = append(repoInstall, pq[numpq-num-1].Name)
  178. } else {
  179. repoInstall = append(repoInstall, pq[num].Name)
  180. }
  181. }
  182. }
  183. if len(repoInstall) != 0 {
  184. pac.Install(repoInstall, flags)
  185. }
  186. if len(aurInstall) != 0 {
  187. aur.Install(aurInstall, flags)
  188. }
  189. return nil
  190. }