yay.go 5.1 KB

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