cmd.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. alpm "github.com/Jguer/go-alpm"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v10/pkg/completion"
  10. "github.com/Jguer/yay/v10/pkg/intrange"
  11. "github.com/Jguer/yay/v10/pkg/text"
  12. )
  13. var cmdArgs = makeArguments()
  14. func usage() {
  15. fmt.Println(`Usage:
  16. yay
  17. yay <operation> [...]
  18. yay <package(s)>
  19. operations:
  20. yay {-h --help}
  21. yay {-V --version}
  22. yay {-D --database} <options> <package(s)>
  23. yay {-F --files} [options] [package(s)]
  24. yay {-Q --query} [options] [package(s)]
  25. yay {-R --remove} [options] <package(s)>
  26. yay {-S --sync} [options] [package(s)]
  27. yay {-T --deptest} [options] [package(s)]
  28. yay {-U --upgrade} [options] <file(s)>
  29. New operations:
  30. yay {-Y --yay} [options] [package(s)]
  31. yay {-P --show} [options]
  32. yay {-G --getpkgbuild} [package(s)]
  33. If no arguments are provided 'yay -Syu' will be performed
  34. If no operation is provided -Y will be assumed
  35. New options:
  36. --repo Assume targets are from the repositories
  37. -a --aur Assume targets are from the AUR
  38. Permanent configuration options:
  39. --save Causes the following options to be saved back to the
  40. config file when used
  41. --aururl <url> Set an alternative AUR URL
  42. --builddir <dir> Directory used to download and run PKGBUILDS
  43. --absdir <dir> Directory used to store downloads from the ABS
  44. --editor <file> Editor to use when editing PKGBUILDs
  45. --editorflags <flags> Pass arguments to editor
  46. --makepkg <file> makepkg command to use
  47. --mflags <flags> Pass arguments to makepkg
  48. --pacman <file> pacman command to use
  49. --git <file> git command to use
  50. --gitflags <flags> Pass arguments to git
  51. --gpg <file> gpg command to use
  52. --gpgflags <flags> Pass arguments to gpg
  53. --config <file> pacman.conf file to use
  54. --makepkgconf <file> makepkg.conf file to use
  55. --nomakepkgconf Use the default makepkg.conf
  56. --requestsplitn <n> Max amount of packages to query per AUR request
  57. --completioninterval <n> Time in days to refresh completion cache
  58. --sortby <field> Sort AUR results by a specific field during search
  59. --searchby <field> Search for packages using a specified field
  60. --answerclean <a> Set a predetermined answer for the clean build menu
  61. --answerdiff <a> Set a predetermined answer for the diff menu
  62. --answeredit <a> Set a predetermined answer for the edit pkgbuild menu
  63. --answerupgrade <a> Set a predetermined answer for the upgrade menu
  64. --noanswerclean Unset the answer for the clean build menu
  65. --noanswerdiff Unset the answer for the edit diff menu
  66. --noansweredit Unset the answer for the edit pkgbuild menu
  67. --noanswerupgrade Unset the answer for the upgrade menu
  68. --cleanmenu Give the option to clean build PKGBUILDS
  69. --diffmenu Give the option to show diffs for build files
  70. --editmenu Give the option to edit/view PKGBUILDS
  71. --upgrademenu Show a detailed list of updates with the option to skip any
  72. --nocleanmenu Don't clean build PKGBUILDS
  73. --nodiffmenu Don't show diffs for build files
  74. --noeditmenu Don't edit/view PKGBUILDS
  75. --noupgrademenu Don't show the upgrade menu
  76. --askremovemake Ask to remove makedepends after install
  77. --removemake Remove makedepends after install
  78. --noremovemake Don't remove makedepends after install
  79. --cleanafter Remove package sources after successful install
  80. --nocleanafter Do not remove package sources after successful build
  81. --bottomup Shows AUR's packages first and then repository's
  82. --topdown Shows repository's packages first and then AUR's
  83. --devel Check development packages during sysupgrade
  84. --nodevel Do not check development packages
  85. --rebuild Always build target packages
  86. --rebuildall Always build all AUR packages
  87. --norebuild Skip package build if in cache and up to date
  88. --rebuildtree Always build all AUR packages even if installed
  89. --redownload Always download pkgbuilds of targets
  90. --noredownload Skip pkgbuild download if in cache and up to date
  91. --redownloadall Always download pkgbuilds of all AUR packages
  92. --provides Look for matching providers when searching for packages
  93. --noprovides Just look for packages by pkgname
  94. --pgpfetch Prompt to import PGP keys from PKGBUILDs
  95. --nopgpfetch Don't prompt to import PGP keys
  96. --useask Automatically resolve conflicts using pacman's ask flag
  97. --nouseask Confirm conflicts manually during the install
  98. --combinedupgrade Refresh then perform the repo and AUR upgrade together
  99. --nocombinedupgrade Perform the repo upgrade and AUR upgrade separately
  100. --batchinstall Build multiple AUR packages then install them together
  101. --nobatchinstall Build and install each AUR package one by one
  102. --sudo <file> sudo command to use
  103. --sudoflags <flags> Pass arguments to sudo
  104. --sudoloop Loop sudo calls in the background to avoid timeout
  105. --nosudoloop Do not loop sudo calls in the background
  106. --timeupdate Check packages' AUR page for changes during sysupgrade
  107. --notimeupdate Do not check packages' AUR page for changes
  108. show specific options:
  109. -c --complete Used for completions
  110. -d --defaultconfig Print default yay configuration
  111. -g --currentconfig Print current yay configuration
  112. -s --stats Display system package statistics
  113. -w --news Print arch news
  114. yay specific options:
  115. -c --clean Remove unneeded dependencies
  116. --gendb Generates development package DB used for updating
  117. getpkgbuild specific options:
  118. -f --force Force download for existing ABS packages`)
  119. }
  120. func handleCmd() error {
  121. if cmdArgs.existsArg("h", "help") {
  122. return handleHelp()
  123. }
  124. if config.SudoLoop && cmdArgs.needRoot() {
  125. sudoLoopBackground()
  126. }
  127. switch cmdArgs.op {
  128. case "V", "version":
  129. handleVersion()
  130. return nil
  131. case "D", "database":
  132. return show(passToPacman(cmdArgs))
  133. case "F", "files":
  134. return show(passToPacman(cmdArgs))
  135. case "Q", "query":
  136. return handleQuery()
  137. case "R", "remove":
  138. return handleRemove()
  139. case "S", "sync":
  140. return handleSync()
  141. case "T", "deptest":
  142. return show(passToPacman(cmdArgs))
  143. case "U", "upgrade":
  144. return show(passToPacman(cmdArgs))
  145. case "G", "getpkgbuild":
  146. return handleGetpkgbuild()
  147. case "P", "show":
  148. return handlePrint()
  149. case "Y", "--yay":
  150. return handleYay()
  151. }
  152. return fmt.Errorf(gotext.Get("unhandled operation"))
  153. }
  154. func handleQuery() error {
  155. if cmdArgs.existsArg("u", "upgrades") {
  156. return printUpdateList(cmdArgs)
  157. }
  158. return show(passToPacman(cmdArgs))
  159. }
  160. func handleHelp() error {
  161. if cmdArgs.op == "Y" || cmdArgs.op == "yay" {
  162. usage()
  163. return nil
  164. }
  165. return show(passToPacman(cmdArgs))
  166. }
  167. func handleVersion() {
  168. fmt.Printf("yay v%s - libalpm v%s\n", yayVersion, alpm.Version())
  169. }
  170. func handlePrint() (err error) {
  171. switch {
  172. case cmdArgs.existsArg("d", "defaultconfig"):
  173. tmpConfig := defaultSettings()
  174. tmpConfig.expandEnv()
  175. fmt.Printf("%v", tmpConfig)
  176. case cmdArgs.existsArg("g", "currentconfig"):
  177. fmt.Printf("%v", config)
  178. case cmdArgs.existsArg("n", "numberupgrades"):
  179. err = printNumberOfUpdates()
  180. case cmdArgs.existsArg("u", "upgrades"):
  181. err = printUpdateList(cmdArgs)
  182. case cmdArgs.existsArg("w", "news"):
  183. err = printNewsFeed()
  184. case cmdArgs.existsDouble("c", "complete"):
  185. err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, true)
  186. case cmdArgs.existsArg("c", "complete"):
  187. err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, false)
  188. case cmdArgs.existsArg("s", "stats"):
  189. err = localStatistics()
  190. default:
  191. err = nil
  192. }
  193. return err
  194. }
  195. func handleYay() error {
  196. if cmdArgs.existsArg("gendb") {
  197. return createDevelDB()
  198. }
  199. if cmdArgs.existsDouble("c") {
  200. return cleanDependencies(true)
  201. }
  202. if cmdArgs.existsArg("c", "clean") {
  203. return cleanDependencies(false)
  204. }
  205. if len(cmdArgs.targets) > 0 {
  206. return handleYogurt()
  207. }
  208. return nil
  209. }
  210. func handleGetpkgbuild() error {
  211. return getPkgbuilds(cmdArgs.targets)
  212. }
  213. func handleYogurt() error {
  214. config.SearchMode = numberMenu
  215. return displayNumberMenu(cmdArgs.targets)
  216. }
  217. func handleSync() error {
  218. targets := cmdArgs.targets
  219. if cmdArgs.existsArg("s", "search") {
  220. if cmdArgs.existsArg("q", "quiet") {
  221. config.SearchMode = minimal
  222. } else {
  223. config.SearchMode = detailed
  224. }
  225. return syncSearch(targets)
  226. }
  227. if cmdArgs.existsArg("p", "print", "print-format") {
  228. return show(passToPacman(cmdArgs))
  229. }
  230. if cmdArgs.existsArg("c", "clean") {
  231. return syncClean(cmdArgs)
  232. }
  233. if cmdArgs.existsArg("l", "list") {
  234. return syncList(cmdArgs)
  235. }
  236. if cmdArgs.existsArg("g", "groups") {
  237. return show(passToPacman(cmdArgs))
  238. }
  239. if cmdArgs.existsArg("i", "info") {
  240. return syncInfo(targets)
  241. }
  242. if cmdArgs.existsArg("u", "sysupgrade") {
  243. return install(cmdArgs)
  244. }
  245. if len(cmdArgs.targets) > 0 {
  246. return install(cmdArgs)
  247. }
  248. if cmdArgs.existsArg("y", "refresh") {
  249. return show(passToPacman(cmdArgs))
  250. }
  251. return nil
  252. }
  253. func handleRemove() error {
  254. err := show(passToPacman(cmdArgs))
  255. if err == nil {
  256. removeVCSPackage(cmdArgs.targets)
  257. }
  258. return err
  259. }
  260. // NumberMenu presents a CLI for selecting packages to install.
  261. func displayNumberMenu(pkgS []string) error {
  262. var (
  263. aurErr, repoErr error
  264. aq aurQuery
  265. pq repoQuery
  266. lenaq, lenpq int
  267. )
  268. pkgS = removeInvalidTargets(pkgS)
  269. if mode == modeAUR || mode == modeAny {
  270. aq, aurErr = narrowSearch(pkgS, true)
  271. lenaq = len(aq)
  272. }
  273. if mode == modeRepo || mode == modeAny {
  274. pq, repoErr = queryRepo(pkgS)
  275. lenpq = len(pq)
  276. if repoErr != nil {
  277. return repoErr
  278. }
  279. }
  280. if lenpq == 0 && lenaq == 0 {
  281. return fmt.Errorf(gotext.Get("no packages match search"))
  282. }
  283. switch config.SortMode {
  284. case topDown:
  285. if mode == modeRepo || mode == modeAny {
  286. pq.printSearch()
  287. }
  288. if mode == modeAUR || mode == modeAny {
  289. aq.printSearch(lenpq + 1)
  290. }
  291. case bottomUp:
  292. if mode == modeAUR || mode == modeAny {
  293. aq.printSearch(lenpq + 1)
  294. }
  295. if mode == modeRepo || mode == modeAny {
  296. pq.printSearch()
  297. }
  298. default:
  299. return fmt.Errorf(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  300. }
  301. if aurErr != nil {
  302. text.Errorln(gotext.Get("Error during AUR search: %s\n", aurErr))
  303. text.Warnln(gotext.Get("Showing repo packages only"))
  304. }
  305. text.Infoln(gotext.Get("Packages to install (eg: 1 2 3, 1-3 or ^4)"))
  306. text.Info()
  307. reader := bufio.NewReader(os.Stdin)
  308. numberBuf, overflow, err := reader.ReadLine()
  309. if err != nil {
  310. return err
  311. }
  312. if overflow {
  313. return fmt.Errorf(gotext.Get("input too long"))
  314. }
  315. include, exclude, _, otherExclude := intrange.ParseNumberMenu(string(numberBuf))
  316. arguments := cmdArgs.copyGlobal()
  317. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  318. for i, pkg := range pq {
  319. var target int
  320. switch config.SortMode {
  321. case topDown:
  322. target = i + 1
  323. case bottomUp:
  324. target = len(pq) - i
  325. default:
  326. return fmt.Errorf(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  327. }
  328. if (isInclude && include.Get(target)) || (!isInclude && !exclude.Get(target)) {
  329. arguments.addTarget(pkg.DB().Name() + "/" + pkg.Name())
  330. }
  331. }
  332. for i := range aq {
  333. var target int
  334. switch config.SortMode {
  335. case topDown:
  336. target = i + 1 + len(pq)
  337. case bottomUp:
  338. target = len(aq) - i + len(pq)
  339. default:
  340. return fmt.Errorf(gotext.Get("invalid sort mode. Fix with yay -Y --bottomup --save"))
  341. }
  342. if (isInclude && include.Get(target)) || (!isInclude && !exclude.Get(target)) {
  343. arguments.addTarget("aur/" + aq[i].Name)
  344. }
  345. }
  346. if len(arguments.targets) == 0 {
  347. fmt.Println(gotext.Get(" there is nothing to do"))
  348. return nil
  349. }
  350. if config.SudoLoop {
  351. sudoLoopBackground()
  352. }
  353. return install(arguments)
  354. }
  355. func syncList(parser *arguments) error {
  356. aur := false
  357. for i := len(parser.targets) - 1; i >= 0; i-- {
  358. if parser.targets[i] == "aur" && (mode == modeAny || mode == modeAUR) {
  359. parser.targets = append(parser.targets[:i], parser.targets[i+1:]...)
  360. aur = true
  361. }
  362. }
  363. if (mode == modeAny || mode == modeAUR) && (len(parser.targets) == 0 || aur) {
  364. localDB, err := alpmHandle.LocalDB()
  365. if err != nil {
  366. return err
  367. }
  368. resp, err := http.Get(config.AURURL + "/packages.gz")
  369. if err != nil {
  370. return err
  371. }
  372. defer resp.Body.Close()
  373. scanner := bufio.NewScanner(resp.Body)
  374. scanner.Scan()
  375. for scanner.Scan() {
  376. name := scanner.Text()
  377. if cmdArgs.existsArg("q", "quiet") {
  378. fmt.Println(name)
  379. } else {
  380. fmt.Printf("%s %s %s", magenta("aur"), bold(name), bold(green(gotext.Get("unknown-version"))))
  381. if localDB.Pkg(name) != nil {
  382. fmt.Print(bold(blue(gotext.Get(" [Installed]"))))
  383. }
  384. fmt.Println()
  385. }
  386. }
  387. }
  388. if (mode == modeAny || mode == modeRepo) && (len(parser.targets) != 0 || !aur) {
  389. return show(passToPacman(parser))
  390. }
  391. return nil
  392. }