yay.go 5.0 KB

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