print.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. remote := dbExecutor.InstalledRemotePackages()
  69. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  70. fmt.Println(text.Bold(text.Cyan("===========================================")))
  71. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  72. text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  73. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  74. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  75. for path, size := range info.pacmanCaches {
  76. text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
  77. }
  78. text.Infoln(gotext.Get("Size of yay cache %s: %s", cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
  79. fmt.Println(text.Bold(text.Cyan("===========================================")))
  80. text.Infoln(gotext.Get("Ten biggest packages:"))
  81. biggestPackages(dbExecutor)
  82. fmt.Println(text.Bold(text.Cyan("===========================================")))
  83. aurData, err := cfg.Runtime.AURClient.Get(ctx, &aur.Query{
  84. Needles: remoteNames,
  85. By: aur.Name,
  86. })
  87. if err != nil {
  88. return err
  89. }
  90. warnings := query.NewWarnings(cfg.Runtime.Logger.Child("print"))
  91. for i := range aurData {
  92. warnings.AddToWarnings(remote, &aurData[i])
  93. }
  94. warnings.Print()
  95. return nil
  96. }
  97. func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
  98. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  99. ) error {
  100. quietMode := cmdArgs.ExistsArg("q", "quiet")
  101. // TODO: handle quiet mode in a better way
  102. logger := text.NewLogger(io.Discard, os.Stderr, os.Stdin, cfg.Debug, "update-list")
  103. dbExecutor.SetLogger(logger.Child("db"))
  104. oldNoConfirm := settings.NoConfirm
  105. settings.NoConfirm = true
  106. // restoring global NoConfirm to make tests work properly
  107. defer func() { settings.NoConfirm = oldNoConfirm }()
  108. targets := mapset.NewThreadUnsafeSet(cmdArgs.Targets...)
  109. grapher := dep.NewGrapher(dbExecutor, cfg.Runtime.AURClient, false, true,
  110. false, false, cmdArgs.ExistsArg("needed"), logger.Child("grapher"))
  111. upService := upgrade.NewUpgradeService(
  112. grapher, cfg.Runtime.AURClient, dbExecutor, cfg.Runtime.VCSStore,
  113. cfg, true, logger.Child("upgrade"))
  114. graph, errSysUp := upService.GraphUpgrades(ctx, nil,
  115. enableDowngrade, filter)
  116. if errSysUp != nil {
  117. return errSysUp
  118. }
  119. if graph.Len() == 0 {
  120. return fmt.Errorf("")
  121. }
  122. noTargets := targets.Cardinality() == 0
  123. foreignFilter := cmdArgs.ExistsArg("m", "foreign")
  124. nativeFilter := cmdArgs.ExistsArg("n", "native")
  125. noUpdates := true
  126. _ = graph.ForEach(func(pkgName string, ii *dep.InstallInfo) error {
  127. if !ii.Upgrade {
  128. return nil
  129. }
  130. if noTargets || targets.Contains(pkgName) {
  131. if ii.Source == dep.Sync && foreignFilter {
  132. return nil
  133. } else if ii.Source == dep.AUR && nativeFilter {
  134. return nil
  135. }
  136. if quietMode {
  137. fmt.Printf("%s\n", pkgName)
  138. } else {
  139. fmt.Printf("%s %s -> %s\n", text.Bold(pkgName), text.Bold(text.Green(ii.LocalVersion)),
  140. text.Bold(text.Green(ii.Version)))
  141. }
  142. targets.Remove(pkgName)
  143. noUpdates = false
  144. }
  145. return nil
  146. })
  147. missing := false
  148. targets.Each(func(pkgName string) bool {
  149. if dbExecutor.LocalPackage(pkgName) == nil {
  150. cfg.Runtime.Logger.Errorln(gotext.Get("package '%s' was not found", pkgName))
  151. missing = true
  152. }
  153. return false
  154. })
  155. if missing || noUpdates {
  156. return fmt.Errorf("")
  157. }
  158. return nil
  159. }