yay.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. if op[0] == 'R' {
  127. vcs.RemovePackage(pkgs)
  128. }
  129. err = config.PassToPacman(op, pkgs, options)
  130. }
  131. if vcs.Updated {
  132. vcs.SaveBranchInfo()
  133. }
  134. if changedConfig {
  135. config.SaveConfig()
  136. }
  137. config.AlpmHandle.Release()
  138. if err != nil {
  139. fmt.Println(err)
  140. os.Exit(1)
  141. }
  142. }
  143. // NumberMenu presents a CLI for selecting packages to install.
  144. func numberMenu(pkgS []string, flags []string) (err error) {
  145. var num int
  146. aq, err := aur.NarrowSearch(pkgS, true)
  147. if err != nil {
  148. fmt.Println("Error during AUR search:", err)
  149. }
  150. numaq := len(aq)
  151. pq, numpq, err := pac.Search(pkgS)
  152. if err != nil {
  153. return
  154. }
  155. if numpq == 0 && numaq == 0 {
  156. return fmt.Errorf("no packages match search")
  157. }
  158. if config.YayConf.SortMode == config.BottomUp {
  159. printAURSearch(aq, numpq)
  160. pq.PrintSearch()
  161. } else {
  162. pq.PrintSearch()
  163. printAURSearch(aq, numpq)
  164. }
  165. fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
  166. reader := bufio.NewReader(os.Stdin)
  167. numberBuf, overflow, err := reader.ReadLine()
  168. if err != nil || overflow {
  169. fmt.Println(err)
  170. return
  171. }
  172. numberString := string(numberBuf)
  173. var aurInstall []string
  174. var repoInstall []string
  175. result := strings.Fields(numberString)
  176. for _, numS := range result {
  177. num, err = strconv.Atoi(numS)
  178. if err != nil {
  179. continue
  180. }
  181. // Install package
  182. if num > numaq+numpq-1 || num < 0 {
  183. continue
  184. } else if num > numpq-1 {
  185. if config.YayConf.SortMode == config.BottomUp {
  186. aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
  187. } else {
  188. aurInstall = append(aurInstall, aq[num-numpq].Name)
  189. }
  190. } else {
  191. if config.YayConf.SortMode == config.BottomUp {
  192. repoInstall = append(repoInstall, pq[numpq-num-1].Name())
  193. } else {
  194. repoInstall = append(repoInstall, pq[num].Name())
  195. }
  196. }
  197. }
  198. if len(repoInstall) != 0 {
  199. err = config.PassToPacman("-S", repoInstall, flags)
  200. }
  201. if len(aurInstall) != 0 {
  202. err = aur.Install(aurInstall, flags)
  203. }
  204. return err
  205. }