main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "context"
  4. "errors"
  5. "os"
  6. "os/exec"
  7. "runtime/debug"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v12/pkg/db"
  10. "github.com/Jguer/yay/v12/pkg/db/ialpm"
  11. "github.com/Jguer/yay/v12/pkg/query"
  12. "github.com/Jguer/yay/v12/pkg/settings"
  13. "github.com/Jguer/yay/v12/pkg/settings/parser"
  14. "github.com/Jguer/yay/v12/pkg/text"
  15. )
  16. var (
  17. yayVersion = "12.0.4" // To be set by compiler.
  18. localePath = "/usr/share/locale" // To be set by compiler.
  19. )
  20. func initGotext() {
  21. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  22. localePath = envLocalePath
  23. }
  24. if lc := os.Getenv("LANGUAGE"); lc != "" {
  25. gotext.Configure(localePath, lc, "yay")
  26. } else if lc := os.Getenv("LC_ALL"); lc != "" {
  27. gotext.Configure(localePath, lc, "yay")
  28. } else if lc := os.Getenv("LC_MESSAGES"); lc != "" {
  29. gotext.Configure(localePath, lc, "yay")
  30. } else {
  31. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  32. }
  33. }
  34. func main() {
  35. var (
  36. err error
  37. ctx = context.Background()
  38. ret = 0
  39. )
  40. defer func() {
  41. if rec := recover(); rec != nil {
  42. text.Errorln(rec)
  43. debug.PrintStack()
  44. }
  45. os.Exit(ret)
  46. }()
  47. initGotext()
  48. if os.Geteuid() == 0 {
  49. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  50. }
  51. configPath := settings.GetConfigPath()
  52. // Parse config
  53. cfg, err := settings.NewConfig(configPath, yayVersion)
  54. if err != nil {
  55. if str := err.Error(); str != "" {
  56. text.Errorln(str)
  57. }
  58. ret = 1
  59. return
  60. }
  61. if cfg.Debug {
  62. text.GlobalLogger.Debug = true
  63. }
  64. if errS := cfg.RunMigrations(
  65. settings.DefaultMigrations(), configPath, yayVersion); errS != nil {
  66. text.Errorln(errS)
  67. }
  68. cmdArgs := parser.MakeArguments()
  69. // Parse command line
  70. if err = cfg.ParseCommandLine(cmdArgs); err != nil {
  71. if str := err.Error(); str != "" {
  72. text.Errorln(str)
  73. }
  74. ret = 1
  75. return
  76. }
  77. if cfg.SaveConfig {
  78. if errS := cfg.Save(configPath, yayVersion); errS != nil {
  79. text.Errorln(errS)
  80. }
  81. }
  82. // Build runtime
  83. runtime, err := settings.BuildRuntime(cfg, cmdArgs, yayVersion)
  84. if err != nil {
  85. if str := err.Error(); str != "" {
  86. text.Errorln(str)
  87. }
  88. ret = 1
  89. return
  90. }
  91. cfg.Runtime = runtime
  92. cfg.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
  93. cfg.Runtime.AURClient,
  94. cfg.Runtime.Logger.Child("mixed.querybuilder"), cfg.SortBy,
  95. cfg.Mode, cfg.SearchBy,
  96. cfg.BottomUp, cfg.SingleLineResults, cfg.SeparateSources)
  97. var useColor bool
  98. cfg.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, cfg.PacmanConf)
  99. if err != nil {
  100. if str := err.Error(); str != "" {
  101. text.Errorln(str)
  102. }
  103. ret = 1
  104. return
  105. }
  106. cfg.Runtime.CmdBuilder.SetPacmanDBPath(cfg.Runtime.PacmanConf.DBPath)
  107. text.UseColor = useColor
  108. dbExecutor, err := ialpm.NewExecutor(cfg.Runtime.PacmanConf, runtime.Logger.Child("db"))
  109. if err != nil {
  110. if str := err.Error(); str != "" {
  111. text.Errorln(str)
  112. }
  113. ret = 1
  114. return
  115. }
  116. defer func() {
  117. if rec := recover(); rec != nil {
  118. text.Errorln(rec)
  119. debug.PrintStack()
  120. }
  121. dbExecutor.Cleanup()
  122. }()
  123. if err = handleCmd(ctx, cfg, cmdArgs, db.Executor(dbExecutor)); err != nil {
  124. if str := err.Error(); str != "" {
  125. text.Errorln(str)
  126. if cmdArgs.ExistsArg("c") && cmdArgs.ExistsArg("y") && cmdArgs.Op == "S" {
  127. // Remove after 2023-10-01
  128. text.Errorln("Did you mean 'yay -Yc'?")
  129. }
  130. }
  131. exitError := &exec.ExitError{}
  132. if errors.As(err, &exitError) {
  133. // mirror pacman exit code when applicable
  134. ret = exitError.ExitCode()
  135. return
  136. }
  137. // fallback
  138. ret = 1
  139. }
  140. }