cmd.go 13 KB

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