print.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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("Version"), a.Version)
  24. text.PrintInfoValue(gotext.Get("Description"), a.Description)
  25. text.PrintInfoValue(gotext.Get("URL"), a.URL)
  26. text.PrintInfoValue(gotext.Get("Licenses"), a.License...)
  27. text.PrintInfoValue(gotext.Get("Groups"), a.Groups...)
  28. text.PrintInfoValue(gotext.Get("Provides"), a.Provides...)
  29. text.PrintInfoValue(gotext.Get("Depends On"), a.Depends...)
  30. text.PrintInfoValue(gotext.Get("Optional Deps"), a.OptDepends...)
  31. text.PrintInfoValue(gotext.Get("Make Deps"), a.MakeDepends...)
  32. text.PrintInfoValue(gotext.Get("Check Deps"), a.CheckDepends...)
  33. text.PrintInfoValue(gotext.Get("Conflicts With"), a.Conflicts...)
  34. text.PrintInfoValue(gotext.Get("Replaces"), a.Replaces...)
  35. text.PrintInfoValue(gotext.Get("AUR URL"), config.AURURL+"/packages/"+a.Name)
  36. text.PrintInfoValue(gotext.Get("First Submitted"), text.FormatTimeQuery(a.FirstSubmitted))
  37. text.PrintInfoValue(gotext.Get("Keywords"), a.Keywords...)
  38. text.PrintInfoValue(gotext.Get("Last Modified"), text.FormatTimeQuery(a.LastModified))
  39. text.PrintInfoValue(gotext.Get("Maintainer"), a.Maintainer)
  40. text.PrintInfoValue(gotext.Get("Popularity"), fmt.Sprintf("%f", a.Popularity))
  41. text.PrintInfoValue(gotext.Get("Votes"), fmt.Sprintf("%d", a.NumVotes))
  42. if a.OutOfDate != 0 {
  43. text.PrintInfoValue(gotext.Get("Out-of-date"), text.FormatTimeQuery(a.OutOfDate))
  44. } else {
  45. text.PrintInfoValue(gotext.Get("Out-of-date"), "No")
  46. }
  47. if extendedInfo {
  48. text.PrintInfoValue("ID", fmt.Sprintf("%d", a.ID))
  49. text.PrintInfoValue(gotext.Get("Package Base ID"), fmt.Sprintf("%d", a.PackageBaseID))
  50. text.PrintInfoValue(gotext.Get("Package Base"), a.PackageBase)
  51. text.PrintInfoValue(gotext.Get("Snapshot URL"), config.AURURL+a.URLPath)
  52. }
  53. fmt.Println()
  54. }
  55. // BiggestPackages prints the name of the ten biggest packages in the system.
  56. func biggestPackages(dbExecutor db.Executor) {
  57. pkgS := dbExecutor.BiggestPackages()
  58. if len(pkgS) < 10 {
  59. return
  60. }
  61. for i := 0; i < 10; i++ {
  62. fmt.Printf("%s: %s\n", text.Bold(pkgS[i].Name()), text.Cyan(text.Human(pkgS[i].ISize())))
  63. }
  64. }
  65. // localStatistics prints installed packages statistics.
  66. func localStatistics(ctx context.Context, cfg *settings.Configuration, dbExecutor db.Executor) error {
  67. info := statistics(cfg, dbExecutor)
  68. remoteNames := dbExecutor.InstalledRemotePackageNames()
  69. remote := dbExecutor.InstalledRemotePackages()
  70. text.Infoln(gotext.Get("Yay version v%s", yayVersion))
  71. fmt.Println(text.Bold(text.Cyan("===========================================")))
  72. text.Infoln(gotext.Get("Total installed packages: %s", text.Cyan(strconv.Itoa(info.Totaln))))
  73. text.Infoln(gotext.Get("Foreign installed packages: %s", text.Cyan(strconv.Itoa(len(remoteNames)))))
  74. text.Infoln(gotext.Get("Explicitly installed packages: %s", text.Cyan(strconv.Itoa(info.Expln))))
  75. text.Infoln(gotext.Get("Total Size occupied by packages: %s", text.Cyan(text.Human(info.TotalSize))))
  76. for path, size := range info.pacmanCaches {
  77. text.Infoln(gotext.Get("Size of pacman cache %s: %s", path, text.Cyan(text.Human(size))))
  78. }
  79. text.Infoln(gotext.Get("Size of yay cache %s: %s", cfg.BuildDir, text.Cyan(text.Human(info.yayCache))))
  80. fmt.Println(text.Bold(text.Cyan("===========================================")))
  81. text.Infoln(gotext.Get("Ten biggest packages:"))
  82. biggestPackages(dbExecutor)
  83. fmt.Println(text.Bold(text.Cyan("===========================================")))
  84. aurData, err := cfg.Runtime.AURClient.Get(ctx, &aur.Query{
  85. Needles: remoteNames,
  86. By: aur.Name,
  87. })
  88. if err != nil {
  89. return err
  90. }
  91. warnings := query.NewWarnings(cfg.Runtime.Logger.Child("print"))
  92. for i := range aurData {
  93. warnings.AddToWarnings(remote, &aurData[i])
  94. }
  95. warnings.Print()
  96. return nil
  97. }
  98. func printUpdateList(ctx context.Context, cfg *settings.Configuration, cmdArgs *parser.Arguments,
  99. dbExecutor db.Executor, enableDowngrade bool, filter upgrade.Filter,
  100. ) error {
  101. quietMode := cmdArgs.ExistsArg("q", "quiet")
  102. // TODO: handle quiet mode in a better way
  103. logger := text.NewLogger(io.Discard, os.Stderr, os.Stdin, cfg.Debug, "update-list")
  104. dbExecutor.SetLogger(logger.Child("db"))
  105. oldNoConfirm := settings.NoConfirm
  106. settings.NoConfirm = true
  107. // restoring global NoConfirm to make tests work properly
  108. defer func() { settings.NoConfirm = oldNoConfirm }()
  109. targets := mapset.NewThreadUnsafeSet(cmdArgs.Targets...)
  110. grapher := dep.NewGrapher(dbExecutor, cfg.Runtime.AURClient, false, true,
  111. false, false, cmdArgs.ExistsArg("needed"), logger.Child("grapher"))
  112. upService := upgrade.NewUpgradeService(
  113. grapher, cfg.Runtime.AURClient, dbExecutor, cfg.Runtime.VCSStore,
  114. cfg, true, logger.Child("upgrade"))
  115. graph, errSysUp := upService.GraphUpgrades(ctx, nil,
  116. enableDowngrade, filter)
  117. if errSysUp != nil {
  118. return errSysUp
  119. }
  120. if graph.Len() == 0 {
  121. return fmt.Errorf("")
  122. }
  123. noTargets := targets.Cardinality() == 0
  124. foreignFilter := cmdArgs.ExistsArg("m", "foreign")
  125. nativeFilter := cmdArgs.ExistsArg("n", "native")
  126. noUpdates := true
  127. _ = graph.ForEach(func(pkgName string, ii *dep.InstallInfo) error {
  128. if !ii.Upgrade {
  129. return nil
  130. }
  131. if noTargets || targets.Contains(pkgName) {
  132. if ii.Source == dep.Sync && foreignFilter {
  133. return nil
  134. } else if ii.Source == dep.AUR && nativeFilter {
  135. return nil
  136. }
  137. if quietMode {
  138. fmt.Printf("%s\n", pkgName)
  139. } else {
  140. fmt.Printf("%s %s -> %s\n", text.Bold(pkgName), text.Bold(text.Green(ii.LocalVersion)),
  141. text.Bold(text.Green(ii.Version)))
  142. }
  143. targets.Remove(pkgName)
  144. noUpdates = false
  145. }
  146. return nil
  147. })
  148. missing := false
  149. targets.Each(func(pkgName string) bool {
  150. if dbExecutor.LocalPackage(pkgName) == nil {
  151. cfg.Runtime.Logger.Errorln(gotext.Get("package '%s' was not found", pkgName))
  152. missing = true
  153. }
  154. return false
  155. })
  156. if missing || noUpdates {
  157. return fmt.Errorf("")
  158. }
  159. return nil
  160. }