print.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strconv"
  8. aur "github.com/Jguer/aur"
  9. mapset "github.com/deckarep/golang-set/v2"
  10. "github.com/leonelquinteros/gotext"
  11. "github.com/Jguer/yay/v12/pkg/db"
  12. "github.com/Jguer/yay/v12/pkg/dep"
  13. "github.com/Jguer/yay/v12/pkg/query"
  14. "github.com/Jguer/yay/v12/pkg/settings"
  15. "github.com/Jguer/yay/v12/pkg/settings/parser"
  16. "github.com/Jguer/yay/v12/pkg/text"
  17. "github.com/Jguer/yay/v12/pkg/upgrade"
  18. )
  19. // PrintInfo prints package info like pacman -Si.
  20. func PrintInfo(config *settings.Configuration, a *aur.Pkg, extendedInfo bool) {
  21. text.PrintInfoValue(gotext.Get("Repository"), "aur")
  22. text.PrintInfoValue(gotext.Get("Name"), a.Name)
  23. text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...)
  24. text.PrintInfoValue(gotext.Get("Version"), a.Version)
  25. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  26. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  27. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  28. text.PrintInfoValue(gotext.Get("Groups"), a.Groups...)
  29. text.PrintInfoValue(gotext.Get("Licenses"), a.License...)
  30. text.PrintInfoValue(gotext.Get("Provides"), a.Provides...)
  31. text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...)
  32. text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...)
  33. text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...)
  34. text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...)
  35. text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...)
  36. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  37. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  38. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  39. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  40. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  41. if a.OutOfDate != 0 {
  42. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  43. } else {
  44. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  45. }
  46. if extendedInfo {
  47. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  48. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  49. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  50. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  51. }
  52. fmt.Println()
  53. }
  54. // BiggestPackages prints the name of the ten biggest packages in the system.
  55. func biggestPackages(dbExecutor db.Executor) {
  56. pkgS := dbExecutor.BiggestPackages()
  57. if len(pkgS) < 10 {
  58. return
  59. }
  60. for i := 0; i < 10; i++ {
  61. fmt.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
  62. }
  63. }
  64. // localStatistics prints installed packages statistics.
  65. func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) error {
  66. info := statistics(cfg, dbExecutor)
  67. remoteNames := dbExecutor.InstalledRemotePackageNames()
  68. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  69. fmt.Println(text.Bold(text.Cyan("===========================================")))
  70. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  71. text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  72. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  73. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  74. for path, size := range info.pacmanCaches {
  75. text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
  76. }
  77. text.Infoln(gotext.Get("Size of yay cache %s: %s", cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
  78. fmt.Println(text.Bold(text.Cyan("===========================================")))
  79. text.Infoln(gotext.Get("Ten biggest packages:"))
  80. biggestPackages(dbExecutor)
  81. fmt.Println(text.Bold(text.Cyan("===========================================")))
  82. query.AURInfoPrint(ctx, cfg.Runtime.AURClient, remoteNames, cfg.RequestSplitN)
  83. return nil
  84. }
  85. func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
  86. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  87. ) error {
  88. quietMode := cmdArgs.ExistsArg("q", "quiet")
  89. logger := cfg.Runtime.Logger.Child("update-list")
  90. if quietMode { // TODO: handle quiet mode in a better way
  91. logger = text.NewLogger(io.Discard, os.Stdin, cfg.Debug, "update-list")
  92. }
  93. targets := mapset.NewThreadUnsafeSet(cmdArgs.Targets...)
  94. grapher := dep.NewGrapher(dbExecutor, cfg.Runtime.AURCache, false, settings.NoConfirm,
  95. false, false, cmdArgs.ExistsArg("needed"), logger.Child("grapher"))
  96. upService := upgrade.NewUpgradeService(
  97. grapher, cfg.Runtime.AURCache, dbExecutor, cfg.Runtime.VCSStore,
  98. cfg, settings.NoConfirm, logger.Child("upgrade"))
  99. graph, errSysUp := upService.GraphUpgrades(ctx, nil,
  100. enableDowngrade, filter)
  101. if errSysUp != nil {
  102. return errSysUp
  103. }
  104. if graph.Len() == 0 {
  105. return fmt.Errorf("")
  106. }
  107. noTargets := targets.Cardinality() == 0
  108. foreignFilter := cmdArgs.ExistsArg("m", "foreign")
  109. nativeFilter := cmdArgs.ExistsArg("n", "native")
  110. _ = graph.ForEach(func(pkgName string, ii *dep.InstallInfo) error {
  111. if noTargets || targets.Contains(pkgName) {
  112. if ii.Source == dep.Sync && foreignFilter {
  113. return nil
  114. } else if ii.Source == dep.AUR && nativeFilter {
  115. return nil
  116. }
  117. if quietMode {
  118. fmt.Printf("%s\n", pkgName)
  119. } else {
  120. fmt.Printf("%s %s -> %s\n", text.Bold(pkgName), text.Green(ii.LocalVersion),
  121. text.Green(ii.Version))
  122. }
  123. targets.Remove(pkgName)
  124. }
  125. return nil
  126. })
  127. missing := false
  128. targets.Each(func(pkgName string) bool {
  129. if dbExecutor.LocalPackage(pkgName) == nil {
  130. cfg.Runtime.Logger.Errorln(gotext.Get("package '%s' was not found", pkgName))
  131. missing = true
  132. }
  133. return false
  134. })
  135. if missing {
  136. return fmt.Errorf("")
  137. }
  138. return nil
  139. }