cmd.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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/types"
  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. --editor <file> Editor to use when editing PKGBUILDs
  40. --editorflags <flags> Pass arguments to editor
  41. --makepkg <file> makepkg command to use
  42. --mflags <flags> Pass arguments to makepkg
  43. --pacman <file> pacman command to use
  44. --tar <file> bsdtar command to use
  45. --git <file> git command to use
  46. --gitflags <flags> Pass arguments to git
  47. --gpg <file> gpg command to use
  48. --gpgflags <flags> Pass arguments to gpg
  49. --config <file> pacman.conf file to use
  50. --makepkgconf <file> makepkg.conf file to use
  51. --nomakepkgconf Use the default makepkg.conf
  52. --requestsplitn <n> Max amount of packages to query per AUR request
  53. --completioninterval <n> Time in days to to refresh completion cache
  54. --sortby <field> Sort AUR results by a specific field during search
  55. --answerclean <a> Set a predetermined answer for the clean build menu
  56. --answerdiff <a> Set a predetermined answer for the diff menu
  57. --answeredit <a> Set a predetermined answer for the edit pkgbuild menu
  58. --answerupgrade <a> Set a predetermined answer for the upgrade menu
  59. --noanswerclean Unset the answer for the clean build menu
  60. --noanswerdiff Unset the answer for the edit diff menu
  61. --noansweredit Unset the answer for the edit pkgbuild menu
  62. --noanswerupgrade Unset the answer for the upgrade menu
  63. --cleanmenu Give the option to clean build PKGBUILDS
  64. --diffmenu Give the option to show diffs for build files
  65. --editmenu Give the option to edit/view PKGBUILDS
  66. --upgrademenu Show a detailed list of updates with the option to skip any
  67. --nocleanmenu Don't clean build PKGBUILDS
  68. --nodiffmenu Don't show diffs for build files
  69. --noeditmenu Don't edit/view PKGBUILDS
  70. --noupgrademenu Don't show the upgrade menu
  71. --askremovemake Ask to remove makedepends after install
  72. --removemake Remove makedepends after install
  73. --noremovemake Don't remove makedepends after install
  74. --cleanafter Remove package sources after successful install
  75. --nocleanafter Do not remove package sources after successful build
  76. --bottomup Shows AUR's packages first and then repository's
  77. --topdown Shows repository's packages first and then AUR's
  78. --devel Check development packages during sysupgrade
  79. --nodevel Do not check development packages
  80. --gitclone Use git clone for PKGBUILD retrieval
  81. --nogitclone Never use git clone for PKGBUILD retrieval
  82. --rebuild Always build target packages
  83. --rebuildall Always build all AUR packages
  84. --norebuild Skip package build if in cache and up to date
  85. --rebuildtree Always build all AUR packages even if installed
  86. --redownload Always download pkgbuilds of targets
  87. --noredownload Skip pkgbuild download if in cache and up to date
  88. --redownloadall Always download pkgbuilds of all AUR packages
  89. --provides Look for matching providers when searching for packages
  90. --noprovides Just look for packages by pkgname
  91. --pgpfetch Prompt to import PGP keys from PKGBUILDs
  92. --nopgpfetch Don't prompt to import PGP keys
  93. --useask Automatically resolve conflicts using pacman's ask flag
  94. --nouseask Confirm conflicts manually during the install
  95. --combinedupgrade Refresh then perform the repo and AUR upgrade together
  96. --nocombinedupgrade Perform the repo upgrade and AUR upgrade separately
  97. --sudoloop Loop sudo calls in the background to avoid timeout
  98. --nosudoloop Do not loop sudo calls in the background
  99. --timeupdate Check packages' AUR page for changes during sysupgrade
  100. --notimeupdate Do not check packages' AUR page for changes
  101. show specific options:
  102. -c --complete Used for completions
  103. -d --defaultconfig Print default yay configuration
  104. -g --currentconfig Print current yay configuration
  105. -s --stats Display system package statistics
  106. -w --news Print arch news
  107. yay specific options:
  108. -c --clean Remove unneeded dependencies
  109. --gendb Generates development package DB used for updating
  110. getpkgbuild specific options:
  111. -f --force Force download for existing tar packages
  112. If no arguments are provided 'yay -Syu' will be performed
  113. If no operation is provided -Y will be assumed`)
  114. }
  115. func handleCmd() (err error) {
  116. if cmdArgs.existsArg("h", "help") {
  117. err = handleHelp()
  118. return
  119. }
  120. if config.SudoLoop && cmdArgs.needRoot() {
  121. sudoLoopBackground()
  122. }
  123. switch cmdArgs.op {
  124. case "V", "version":
  125. handleVersion()
  126. case "D", "database":
  127. err = show(passToPacman(cmdArgs))
  128. case "F", "files":
  129. err = show(passToPacman(cmdArgs))
  130. case "Q", "query":
  131. err = handleQuery()
  132. case "R", "remove":
  133. err = handleRemove()
  134. case "S", "sync":
  135. err = handleSync()
  136. case "T", "deptest":
  137. err = show(passToPacman(cmdArgs))
  138. case "U", "upgrade":
  139. err = show(passToPacman(cmdArgs))
  140. case "G", "getpkgbuild":
  141. err = handleGetpkgbuild()
  142. case "P", "show":
  143. err = handlePrint()
  144. case "Y", "--yay":
  145. err = handleYay()
  146. default:
  147. //this means we allowed an op but not implement it
  148. //if this happens it an error in the code and not the usage
  149. err = fmt.Errorf("unhandled operation")
  150. }
  151. return
  152. }
  153. func handleQuery() error {
  154. if cmdArgs.existsArg("u", "upgrades") {
  155. return printUpdateList(cmdArgs)
  156. }
  157. return show(passToPacman(cmdArgs))
  158. }
  159. func handleHelp() error {
  160. if cmdArgs.op == "Y" || cmdArgs.op == "yay" {
  161. usage()
  162. return nil
  163. }
  164. return show(passToPacman(cmdArgs))
  165. }
  166. func handleVersion() {
  167. fmt.Printf("yay v%s - libalpm v%s\n", version, alpm.Version())
  168. }
  169. func handlePrint() (err error) {
  170. switch {
  171. case cmdArgs.existsArg("d", "defaultconfig"):
  172. tmpConfig := defaultSettings()
  173. tmpConfig.expandEnv()
  174. fmt.Printf("%v", tmpConfig)
  175. case cmdArgs.existsArg("g", "currentconfig"):
  176. fmt.Printf("%v", config)
  177. case cmdArgs.existsArg("n", "numberupgrades"):
  178. err = printNumberOfUpdates()
  179. case cmdArgs.existsArg("u", "upgrades"):
  180. err = printUpdateList(cmdArgs)
  181. case cmdArgs.existsArg("w", "news"):
  182. err = printNewsFeed()
  183. case cmdArgs.existsDouble("c", "complete"):
  184. err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, true)
  185. case cmdArgs.existsArg("c", "complete"):
  186. err = completion.Show(alpmHandle, config.AURURL, cacheHome, config.CompletionInterval, false)
  187. case cmdArgs.existsArg("s", "stats"):
  188. err = localStatistics()
  189. default:
  190. err = nil
  191. }
  192. return err
  193. }
  194. func handleYay() error {
  195. //_, options, targets := cmdArgs.formatArgs()
  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) (err 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 err
  278. }
  279. }
  280. if lenpq == 0 && lenaq == 0 {
  281. return fmt.Errorf("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("Invalid Sort Mode. Fix with yay -Y --bottomup --save")
  300. }
  301. if aurErr != nil {
  302. fmt.Fprintf(os.Stderr, "Error during AUR search: %s\n", aurErr)
  303. fmt.Fprintln(os.Stderr, "Showing repo packages only")
  304. }
  305. fmt.Println(bold(green(arrow + " Packages to install (eg: 1 2 3, 1-3 or ^4)")))
  306. fmt.Print(bold(green(arrow + " ")))
  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("Input too long")
  314. }
  315. include, exclude, _, otherExclude := types.ParseNumberMenu(string(numberBuf))
  316. arguments := makeArguments()
  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("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, pkg := 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("Invalid Sort Mode. Fix with yay -Y --bottomup --save")
  341. }
  342. if (isInclude && include.Get(target)) || (!isInclude && !exclude.Get(target)) {
  343. arguments.addTarget("aur/" + pkg.Name)
  344. }
  345. }
  346. if len(arguments.targets) == 0 {
  347. fmt.Println("There is nothing to do")
  348. return nil
  349. }
  350. if config.SudoLoop {
  351. sudoLoopBackground()
  352. }
  353. err = install(arguments)
  354. return err
  355. }
  356. func syncList(parser *arguments) error {
  357. aur := false
  358. for i := len(parser.targets) - 1; i >= 0; i-- {
  359. if parser.targets[i] == "aur" && (mode == modeAny || mode == modeAUR) {
  360. parser.targets = append(parser.targets[:i], parser.targets[i+1:]...)
  361. aur = true
  362. }
  363. }
  364. if (mode == modeAny || mode == modeAUR) && (len(parser.targets) == 0 || aur) {
  365. localDB, err := alpmHandle.LocalDB()
  366. if err != nil {
  367. return err
  368. }
  369. resp, err := http.Get(config.AURURL + "/packages.gz")
  370. if err != nil {
  371. return err
  372. }
  373. defer resp.Body.Close()
  374. scanner := bufio.NewScanner(resp.Body)
  375. scanner.Scan()
  376. for scanner.Scan() {
  377. name := scanner.Text()
  378. if cmdArgs.existsArg("q", "quiet") {
  379. fmt.Println(name)
  380. } else {
  381. fmt.Printf("%s %s %s", magenta("aur"), bold(name), bold(green("unknown-version")))
  382. if localDB.Pkg(name) != nil {
  383. fmt.Print(bold(blue(" [Installed]")))
  384. }
  385. fmt.Println()
  386. }
  387. }
  388. }
  389. if (mode == modeAny || mode == modeRepo) && (len(parser.targets) != 0 || !aur) {
  390. return show(passToPacman(parser))
  391. }
  392. return nil
  393. }