cmd.go 13 KB

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