cmd.go 12 KB

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