yay.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/jguer/yay/aur"
  11. vcs "github.com/jguer/yay/aur/vcs"
  12. "github.com/jguer/yay/config"
  13. pac "github.com/jguer/yay/pacman"
  14. )
  15. func usage() {
  16. fmt.Println(`usage: yay <operation> [...]
  17. operations:
  18. yay {-h --help}
  19. yay {-V --version}
  20. yay {-D --database} <options> <package(s)>
  21. yay {-F --files} [options] [package(s)]
  22. yay {-Q --query} [options] [package(s)]
  23. yay {-R --remove} [options] <package(s)>
  24. yay {-S --sync} [options] [package(s)]
  25. yay {-T --deptest} [options] [package(s)]
  26. yay {-U --upgrade} [options] <file(s)>
  27. New operations:
  28. yay -Qstats displays system information
  29. yay -Cd remove unneeded dependencies
  30. yay -G [package(s)] get pkgbuild from ABS or AUR
  31. New options:
  32. --topdown shows repository's packages first and then aur's
  33. --bottomup shows aur's packages first and then repository's
  34. --noconfirm skip user input on package install
  35. --devel Check -git/-svn/-hg development version
  36. --nodevel Disable development version checking
  37. `)
  38. }
  39. var version = "2.116"
  40. func parser() (op string, options []string, packages []string, changedConfig bool, err error) {
  41. if len(os.Args) < 2 {
  42. err = fmt.Errorf("no operation specified")
  43. return
  44. }
  45. changedConfig = false
  46. op = "yogurt"
  47. for _, arg := range os.Args[1:] {
  48. if arg[0] == '-' && arg[1] != '-' {
  49. switch arg {
  50. default:
  51. op = arg
  52. }
  53. continue
  54. }
  55. if arg[0] == '-' && arg[1] == '-' {
  56. changedConfig = true
  57. switch arg {
  58. case "--printconfig":
  59. fmt.Printf("%+v", config.YayConf)
  60. os.Exit(0)
  61. case "--gendb":
  62. err = aur.CreateDevelDB()
  63. if err != nil {
  64. fmt.Println(err)
  65. }
  66. err = vcs.SaveBranchInfo()
  67. if err != nil {
  68. fmt.Println(err)
  69. }
  70. os.Exit(0)
  71. case "--devel":
  72. config.YayConf.Devel = true
  73. case "--nodevel":
  74. config.YayConf.Devel = false
  75. case "--timeupdate":
  76. config.YayConf.TimeUpdate = true
  77. case "--notimeupdate":
  78. config.YayConf.TimeUpdate = false
  79. case "--topdown":
  80. config.YayConf.SortMode = config.TopDown
  81. case "--complete":
  82. config.YayConf.Shell = "sh"
  83. _ = complete()
  84. os.Exit(0)
  85. case "--fcomplete":
  86. config.YayConf.Shell = "fish"
  87. _ = complete()
  88. os.Exit(0)
  89. case "--help":
  90. usage()
  91. os.Exit(0)
  92. case "--noconfirm":
  93. config.YayConf.NoConfirm = true
  94. fallthrough
  95. default:
  96. options = append(options, arg)
  97. }
  98. continue
  99. }
  100. packages = append(packages, arg)
  101. }
  102. return
  103. }
  104. func main() {
  105. op, options, pkgs, changedConfig, err := parser()
  106. if err != nil {
  107. fmt.Println(err)
  108. os.Exit(1)
  109. }
  110. switch op {
  111. case "-Cd":
  112. err = cleanDependencies(pkgs)
  113. case "-G":
  114. for _, pkg := range pkgs {
  115. err = getPkgbuild(pkg)
  116. if err != nil {
  117. fmt.Println(pkg+":", err)
  118. }
  119. }
  120. case "-Qstats":
  121. err = localStatistics(version)
  122. case "-Ss", "-Ssq", "-Sqs":
  123. if op == "-Ss" {
  124. config.YayConf.SearchMode = config.Detailed
  125. } else {
  126. config.YayConf.SearchMode = config.Minimal
  127. }
  128. if pkgs != nil {
  129. err = syncSearch(pkgs)
  130. }
  131. case "-S":
  132. err = install(pkgs, options)
  133. case "-Sy":
  134. err = config.PassToPacman("-Sy", nil, nil)
  135. if err != nil {
  136. break
  137. }
  138. err = install(pkgs, options)
  139. case "-Syu", "-Suy", "-Su":
  140. if strings.Contains(op, "y") {
  141. err = config.PassToPacman("-Sy", nil, nil)
  142. if err != nil {
  143. break
  144. }
  145. }
  146. err = upgradePkgs(options)
  147. case "-Si":
  148. err = syncInfo(pkgs, options)
  149. case "yogurt":
  150. config.YayConf.SearchMode = config.NumberMenu
  151. if pkgs != nil {
  152. err = numberMenu(pkgs, options)
  153. }
  154. default:
  155. if op[0] == 'R' {
  156. vcs.RemovePackage(pkgs)
  157. }
  158. err = config.PassToPacman(op, pkgs, options)
  159. }
  160. var erra error
  161. if vcs.Updated {
  162. erra = vcs.SaveBranchInfo()
  163. if erra != nil {
  164. fmt.Println(err)
  165. }
  166. }
  167. if changedConfig {
  168. erra = config.SaveConfig()
  169. if erra != nil {
  170. fmt.Println(err)
  171. }
  172. }
  173. erra = config.AlpmHandle.Release()
  174. if erra != nil {
  175. fmt.Println(err)
  176. }
  177. if err != nil {
  178. fmt.Println(err)
  179. os.Exit(1)
  180. }
  181. }
  182. // NumberMenu presents a CLI for selecting packages to install.
  183. func numberMenu(pkgS []string, flags []string) (err error) {
  184. var num int
  185. aq, err := aur.NarrowSearch(pkgS, true)
  186. if err != nil {
  187. fmt.Println("Error during AUR search:", err)
  188. }
  189. numaq := len(aq)
  190. pq, numpq, err := pac.Search(pkgS)
  191. if err != nil {
  192. return
  193. }
  194. if numpq == 0 && numaq == 0 {
  195. return fmt.Errorf("no packages match search")
  196. }
  197. if config.YayConf.SortMode == config.BottomUp {
  198. printAURSearch(aq, numpq)
  199. pq.PrintSearch()
  200. } else {
  201. pq.PrintSearch()
  202. printAURSearch(aq, numpq)
  203. }
  204. fmt.Printf("\x1b[32m%s\x1b[0m\nNumbers: ", "Type numbers to install. Separate each number with a space.")
  205. reader := bufio.NewReader(os.Stdin)
  206. numberBuf, overflow, err := reader.ReadLine()
  207. if err != nil || overflow {
  208. fmt.Println(err)
  209. return
  210. }
  211. numberString := string(numberBuf)
  212. var aurInstall []string
  213. var repoInstall []string
  214. result := strings.Fields(numberString)
  215. for _, numS := range result {
  216. num, err = strconv.Atoi(numS)
  217. if err != nil {
  218. continue
  219. }
  220. // Install package
  221. if num > numaq+numpq-1 || num < 0 {
  222. continue
  223. } else if num > numpq-1 {
  224. if config.YayConf.SortMode == config.BottomUp {
  225. aurInstall = append(aurInstall, aq[numaq+numpq-num-1].Name)
  226. } else {
  227. aurInstall = append(aurInstall, aq[num-numpq].Name)
  228. }
  229. } else {
  230. if config.YayConf.SortMode == config.BottomUp {
  231. repoInstall = append(repoInstall, pq[numpq-num-1].Name())
  232. } else {
  233. repoInstall = append(repoInstall, pq[num].Name())
  234. }
  235. }
  236. }
  237. if len(repoInstall) != 0 {
  238. err = config.PassToPacman("-S", repoInstall, flags)
  239. }
  240. if len(aurInstall) != 0 {
  241. err = aur.Install(aurInstall, flags)
  242. }
  243. return err
  244. }
  245. // Complete provides completion info for shells
  246. func complete() (err error) {
  247. path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.Shell + ".cache"
  248. if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
  249. os.MkdirAll(os.Getenv("HOME")+"/.cache/yay/", 0755)
  250. out, err := os.Create(path)
  251. if err != nil {
  252. return err
  253. }
  254. if aur.CreateAURList(out) != nil {
  255. defer os.Remove(path)
  256. }
  257. err = pac.CreatePackageList(out)
  258. out.Close()
  259. return err
  260. }
  261. in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
  262. if err != nil {
  263. return err
  264. }
  265. defer in.Close()
  266. _, err = io.Copy(os.Stdout, in)
  267. return err
  268. }