yay.go 4.9 KB

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