yay.go 4.5 KB

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