yay.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "github.com/jguer/yay/aur"
  9. pac "github.com/jguer/yay/pacman"
  10. "github.com/jguer/yay/util"
  11. )
  12. func usage() {
  13. fmt.Println(`usage: yay <operation> [...]
  14. operations:
  15. yay {-h --help}
  16. yay {-V --version}
  17. yay {-D --database} <options> <package(s)>
  18. yay {-F --files} [options] [package(s)]
  19. yay {-Q --query} [options] [package(s)]
  20. yay {-R --remove} [options] <package(s)>
  21. yay {-S --sync} [options] [package(s)]
  22. yay {-T --deptest} [options] [package(s)]
  23. yay {-U --upgrade} [options] <file(s)>
  24. New operations:
  25. yay -Qstats displays system information
  26. yay -Cd remove unneeded dependencies
  27. yay -G [package(s)] get pkgbuild from ABS or AUR
  28. New options:
  29. --topdown shows repository's packages first and then aur's
  30. --bottomup shows aur's packages first and then repository's
  31. --noconfirm skip user input on package install
  32. `)
  33. }
  34. var version = "1.116"
  35. func parser() (op string, options []string, packages []string, err error) {
  36. if len(os.Args) < 2 {
  37. err = fmt.Errorf("no operation specified")
  38. return
  39. }
  40. op = "yogurt"
  41. for _, arg := range os.Args[1:] {
  42. if arg[0] == '-' && arg[1] != '-' {
  43. switch arg {
  44. case "-b":
  45. util.Build = true
  46. default:
  47. op = arg
  48. }
  49. continue
  50. }
  51. if arg[0] == '-' && arg[1] == '-' {
  52. switch arg {
  53. case "--build":
  54. util.Build = true
  55. case "--bottomup":
  56. util.SortMode = util.BottomUp
  57. case "--topdown":
  58. util.SortMode = util.TopDown
  59. case "--complete":
  60. util.Shell = "sh"
  61. Complete()
  62. os.Exit(0)
  63. case "--fcomplete":
  64. util.Shell = "fish"
  65. Complete()
  66. os.Exit(0)
  67. case "--help":
  68. usage()
  69. os.Exit(0)
  70. case "--noconfirm":
  71. util.NoConfirm = true
  72. fallthrough
  73. default:
  74. options = append(options, arg)
  75. }
  76. continue
  77. }
  78. packages = append(packages, arg)
  79. }
  80. return
  81. }
  82. func main() {
  83. op, options, pkgs, err := parser()
  84. if err != nil {
  85. fmt.Println(err)
  86. os.Exit(1)
  87. }
  88. switch op {
  89. case "-Cd":
  90. err = CleanDependencies(pkgs)
  91. case "-G":
  92. for _, pkg := range pkgs {
  93. err = GetPkgbuild(pkg)
  94. if err != nil {
  95. fmt.Println(pkg+":", err)
  96. }
  97. }
  98. case "-Qstats":
  99. err = LocalStatistics(version)
  100. case "-Ss", "-Ssq", "-Sqs":
  101. if op == "-Ss" {
  102. util.SearchVerbosity = util.Detailed
  103. } else {
  104. util.SearchVerbosity = util.Minimal
  105. }
  106. if pkgs != nil {
  107. err = SyncSearch(pkgs)
  108. }
  109. case "-S":
  110. err = Install(pkgs, options)
  111. case "-Syu", "-Suy":
  112. err = Upgrade(options)
  113. case "-Si":
  114. err = SyncInfo(pkgs, options)
  115. case "yogurt":
  116. util.SearchVerbosity = util.NumberMenu
  117. if pkgs != nil {
  118. err = numberMenu(pkgs, options)
  119. }
  120. default:
  121. err = PassToPacman(op, pkgs, options)
  122. }
  123. if err != nil {
  124. fmt.Println(err)
  125. os.Exit(1)
  126. }
  127. }
  128. // NumberMenu presents a CLI for selecting packages to install.
  129. func numberMenu(pkgS []string, flags []string) (err error) {
  130. var num int
  131. aq, numaq, err := aur.Search(pkgS, true)
  132. if err != nil {
  133. fmt.Println("Error during AUR search:", err)
  134. }
  135. pq, numpq, err := pac.Search(pkgS)
  136. if err != nil {
  137. return
  138. }
  139. if numpq == 0 && numaq == 0 {
  140. return fmt.Errorf("no packages match search")
  141. }
  142. if util.SortMode == util.BottomUp {
  143. aq.PrintSearch(numpq)
  144. pq.PrintSearch()
  145. } else {
  146. pq.PrintSearch()
  147. aq.PrintSearch(numpq)
  148. }
  149. fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers:", "Type numbers to install. Separate each number with a space.")
  150. reader := bufio.NewReader(os.Stdin)
  151. numberBuf, overflow, err := reader.ReadLine()
  152. if err != nil || overflow {
  153. fmt.Println(err)
  154. return
  155. }
  156. numberString := string(numberBuf)
  157. var aurInstall []string
  158. var repoInstall []string
  159. result := strings.Fields(numberString)
  160. for _, numS := range result {
  161. num, err = strconv.Atoi(numS)
  162. if err != nil {
  163. continue
  164. }
  165. // Install package
  166. if num > numaq+numpq-1 || num < 0 {
  167. continue
  168. } else if num > numpq-1 {
  169. if util.SortMode == util.BottomUp {
  170. aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
  171. } else {
  172. aurInstall = append(aurInstall, aq[num-numpq].Name)
  173. }
  174. } else {
  175. if util.SortMode == util.BottomUp {
  176. repoInstall = append(repoInstall, pq[numpq-num-1].Name)
  177. } else {
  178. repoInstall = append(repoInstall, pq[num].Name)
  179. }
  180. }
  181. }
  182. if len(repoInstall) != 0 {
  183. pac.Install(repoInstall, flags)
  184. }
  185. if len(aurInstall) != 0 {
  186. q, n, err := aur.MultiInfo(aurInstall)
  187. if err != nil {
  188. return err
  189. } else if n != len(aurInstall) {
  190. q.MissingPackage(aurInstall)
  191. }
  192. var finalrm []string
  193. for _, aurpkg := range q {
  194. finalmdeps, err := aurpkg.Install(flags)
  195. finalrm = append(finalrm, finalmdeps...)
  196. if err != nil {
  197. // Do not abandon program, we might still be able to install the rest
  198. fmt.Println(err)
  199. }
  200. }
  201. if len(finalrm) != 0 {
  202. aur.RemoveMakeDeps(finalrm)
  203. }
  204. }
  205. return nil
  206. }