cmd.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. if config.SortMode == bottomUp {
  279. if mode == modeAUR || mode == modeAny {
  280. aq.printSearch(lenpq + 1)
  281. }
  282. if mode == modeRepo || mode == modeAny {
  283. pq.printSearch()
  284. }
  285. } else {
  286. if mode == modeRepo || mode == modeAny {
  287. pq.printSearch()
  288. }
  289. if mode == modeAUR || mode == modeAny {
  290. aq.printSearch(lenpq + 1)
  291. }
  292. }
  293. if aurErr != nil {
  294. fmt.Fprintf(os.Stderr, "Error during AUR search: %s\n", aurErr)
  295. fmt.Fprintln(os.Stderr, "Showing repo packages only")
  296. }
  297. fmt.Println(bold(green(arrow + " Packages to install (eg: 1 2 3, 1-3 or ^4)")))
  298. fmt.Print(bold(green(arrow + " ")))
  299. reader := bufio.NewReader(os.Stdin)
  300. numberBuf, overflow, err := reader.ReadLine()
  301. if err != nil {
  302. return err
  303. }
  304. if overflow {
  305. return fmt.Errorf("Input too long")
  306. }
  307. include, exclude, _, otherExclude := parseNumberMenu(string(numberBuf))
  308. arguments := makeArguments()
  309. isInclude := len(exclude) == 0 && len(otherExclude) == 0
  310. for i, pkg := range pq {
  311. target := len(pq) - i
  312. if config.SortMode == topDown {
  313. target = i + 1
  314. }
  315. if (isInclude && include.get(target)) || (!isInclude && !exclude.get(target)) {
  316. arguments.addTarget(pkg.DB().Name() + "/" + pkg.Name())
  317. }
  318. }
  319. for i, pkg := range aq {
  320. target := len(aq) - i + len(pq)
  321. if config.SortMode == topDown {
  322. target = i + 1 + len(pq)
  323. }
  324. if (isInclude && include.get(target)) || (!isInclude && !exclude.get(target)) {
  325. arguments.addTarget("aur/" + pkg.Name)
  326. }
  327. }
  328. if len(arguments.targets) == 0 {
  329. fmt.Println("There is nothing to do")
  330. return nil
  331. }
  332. if config.SudoLoop {
  333. sudoLoopBackground()
  334. }
  335. err = install(arguments)
  336. return err
  337. }
  338. func syncList(parser *arguments) error {
  339. aur := false
  340. for i := len(parser.targets) - 1; i >= 0; i-- {
  341. if parser.targets[i] == "aur" && (mode == modeAny || mode == modeAUR) {
  342. parser.targets = append(parser.targets[:i], parser.targets[i+1:]...)
  343. aur = true
  344. }
  345. }
  346. if (mode == modeAny || mode == modeAUR) && (len(parser.targets) == 0 || aur) {
  347. localDB, err := alpmHandle.LocalDB()
  348. if err != nil {
  349. return err
  350. }
  351. resp, err := http.Get(config.AURURL + "/packages.gz")
  352. if err != nil {
  353. return err
  354. }
  355. defer resp.Body.Close()
  356. scanner := bufio.NewScanner(resp.Body)
  357. scanner.Scan()
  358. for scanner.Scan() {
  359. name := scanner.Text()
  360. if cmdArgs.existsArg("q", "quiet") {
  361. fmt.Println(name)
  362. } else {
  363. fmt.Printf("%s %s %s", magenta("aur"), bold(name), bold(green("unknown-version")))
  364. if _, err := localDB.Pkg(name); err == nil {
  365. fmt.Print(bold(blue(" [Installed]")))
  366. }
  367. fmt.Println()
  368. }
  369. }
  370. }
  371. if (mode == modeAny || mode == modeRepo) && (len(parser.targets) != 0 || !aur) {
  372. return show(passToPacman(parser))
  373. }
  374. return nil
  375. }