yay.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/jguer/yay/aur"
  9. vcs "github.com/jguer/yay/aur/vcs"
  10. "github.com/jguer/yay/config"
  11. pac "github.com/jguer/yay/pacman"
  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 = "2.116"
  36. func parser() (op string, options []string, packages []string, changedConfig bool, err error) {
  37. if len(os.Args) < 2 {
  38. err = fmt.Errorf("no operation specified")
  39. return
  40. }
  41. changedConfig = false
  42. op = "yogurt"
  43. for _, arg := range os.Args[1:] {
  44. if arg[0] == '-' && arg[1] != '-' {
  45. switch arg {
  46. default:
  47. op = arg
  48. }
  49. continue
  50. }
  51. if arg[0] == '-' && arg[1] == '-' {
  52. changedConfig = true
  53. switch arg {
  54. case "--gendb":
  55. aur.CreateDevelDB()
  56. vcs.SaveBranchInfo()
  57. os.Exit(0)
  58. case "--devel":
  59. config.YayConf.Devel = true
  60. case "--nodevel":
  61. config.YayConf.Devel = false
  62. case "--topdown":
  63. config.YayConf.SortMode = config.TopDown
  64. case "--complete":
  65. config.YayConf.Shell = "sh"
  66. complete()
  67. os.Exit(0)
  68. case "--fcomplete":
  69. config.YayConf.Shell = "fish"
  70. complete()
  71. os.Exit(0)
  72. case "--help":
  73. usage()
  74. os.Exit(0)
  75. case "--noconfirm":
  76. config.YayConf.NoConfirm = true
  77. fallthrough
  78. default:
  79. options = append(options, arg)
  80. }
  81. continue
  82. }
  83. packages = append(packages, arg)
  84. }
  85. return
  86. }
  87. func main() {
  88. op, options, pkgs, changedConfig, err := parser()
  89. if err != nil {
  90. fmt.Println(err)
  91. os.Exit(1)
  92. }
  93. switch op {
  94. case "-Cd":
  95. err = cleanDependencies(pkgs)
  96. case "-G":
  97. for _, pkg := range pkgs {
  98. err = getPkgbuild(pkg)
  99. if err != nil {
  100. fmt.Println(pkg+":", err)
  101. }
  102. }
  103. case "-Qstats":
  104. err = localStatistics(version)
  105. case "-Ss", "-Ssq", "-Sqs":
  106. if op == "-Ss" {
  107. config.YayConf.SearchMode = config.Detailed
  108. } else {
  109. config.YayConf.SearchMode = config.Minimal
  110. }
  111. if pkgs != nil {
  112. err = syncSearch(pkgs)
  113. }
  114. case "-S":
  115. err = install(pkgs, options)
  116. case "-Syu", "-Suy":
  117. err = upgrade(options)
  118. case "-Si":
  119. err = syncInfo(pkgs, options)
  120. case "yogurt":
  121. config.YayConf.SearchMode = config.NumberMenu
  122. if pkgs != nil {
  123. err = numberMenu(pkgs, options)
  124. }
  125. default:
  126. err = config.PassToPacman(op, pkgs, options)
  127. }
  128. if vcs.Updated {
  129. vcs.SaveBranchInfo()
  130. }
  131. if changedConfig {
  132. config.SaveConfig()
  133. }
  134. config.AlpmHandle.Release()
  135. if err != nil {
  136. fmt.Println(err)
  137. os.Exit(1)
  138. }
  139. }
  140. // NumberMenu presents a CLI for selecting packages to install.
  141. func numberMenu(pkgS []string, flags []string) (err error) {
  142. var num int
  143. aq, err := aur.NarrowSearch(pkgS, true)
  144. if err != nil {
  145. fmt.Println("Error during AUR search:", err)
  146. }
  147. numaq := len(aq)
  148. pq, numpq, err := pac.Search(pkgS)
  149. if err != nil {
  150. return
  151. }
  152. if numpq == 0 && numaq == 0 {
  153. return fmt.Errorf("no packages match search")
  154. }
  155. if config.YayConf.SortMode == config.BottomUp {
  156. printAURSearch(aq, numpq)
  157. pq.PrintSearch()
  158. } else {
  159. pq.PrintSearch()
  160. printAURSearch(aq, numpq)
  161. }
  162. fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
  163. reader := bufio.NewReader(os.Stdin)
  164. numberBuf, overflow, err := reader.ReadLine()
  165. if err != nil || overflow {
  166. fmt.Println(err)
  167. return
  168. }
  169. numberString := string(numberBuf)
  170. var aurInstall []string
  171. var repoInstall []string
  172. result := strings.Fields(numberString)
  173. for _, numS := range result {
  174. num, err = strconv.Atoi(numS)
  175. if err != nil {
  176. continue
  177. }
  178. // Install package
  179. if num > numaq+numpq-1 || num < 0 {
  180. continue
  181. } else if num > numpq-1 {
  182. if config.YayConf.SortMode == config.BottomUp {
  183. aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
  184. } else {
  185. aurInstall = append(aurInstall, aq[num-numpq].Name)
  186. }
  187. } else {
  188. if config.YayConf.SortMode == config.BottomUp {
  189. repoInstall = append(repoInstall, pq[numpq-num-1].Name())
  190. } else {
  191. repoInstall = append(repoInstall, pq[num].Name())
  192. }
  193. }
  194. }
  195. if len(repoInstall) != 0 {
  196. err = config.PassToPacman("-S", repoInstall, flags)
  197. }
  198. if len(aurInstall) != 0 {
  199. err = aur.Install(aurInstall, flags)
  200. }
  201. return err
  202. }