cmd.go 13 KB

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