main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.0" // 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. cfg, err := settings.NewConfig(yayVersion)
  52. if err != nil {
  53. if str := err.Error(); str != "" {
  54. text.Errorln(str)
  55. }
  56. ret = 1
  57. return
  58. }
  59. if cfg.Debug {
  60. text.GlobalLogger.Debug = true
  61. }
  62. if errS := cfg.RunMigrations(
  63. settings.DefaultMigrations(), cfg.Runtime.ConfigPath); errS != nil {
  64. text.Errorln(errS)
  65. }
  66. cmdArgs := parser.MakeArguments()
  67. if err = cfg.ParseCommandLine(cmdArgs); err != nil {
  68. if str := err.Error(); str != "" {
  69. text.Errorln(str)
  70. }
  71. ret = 1
  72. return
  73. }
  74. if cfg.Runtime.SaveConfig {
  75. if errS := cfg.Save(cfg.Runtime.ConfigPath); errS != nil {
  76. text.Errorln(errS)
  77. }
  78. }
  79. if cfg.SeparateSources {
  80. cfg.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
  81. cfg.Runtime.AURClient, cfg.Runtime.AURCache,
  82. cfg.Runtime.Logger.Child("querybuilder"), cfg.SortBy,
  83. cfg.Runtime.Mode, cfg.SearchBy, cfg.BottomUp,
  84. cfg.SingleLineResults, cfg.NewInstallEngine)
  85. } else {
  86. cfg.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
  87. cfg.Runtime.AURClient, cfg.Runtime.AURCache,
  88. cfg.Runtime.Logger.Child("mixed.querybuilder"), cfg.SortBy,
  89. cfg.Runtime.Mode, cfg.SearchBy,
  90. cfg.BottomUp, cfg.SingleLineResults, cfg.NewInstallEngine)
  91. }
  92. var useColor bool
  93. cfg.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, cfg.PacmanConf)
  94. if err != nil {
  95. if str := err.Error(); str != "" {
  96. text.Errorln(str)
  97. }
  98. ret = 1
  99. return
  100. }
  101. cfg.Runtime.CmdBuilder.SetPacmanDBPath(cfg.Runtime.PacmanConf.DBPath)
  102. text.UseColor = useColor
  103. dbExecutor, err := ialpm.NewExecutor(cfg.Runtime.PacmanConf)
  104. if err != nil {
  105. if str := err.Error(); str != "" {
  106. text.Errorln(str)
  107. }
  108. ret = 1
  109. return
  110. }
  111. defer func() {
  112. if rec := recover(); rec != nil {
  113. text.Errorln(rec)
  114. debug.PrintStack()
  115. }
  116. dbExecutor.Cleanup()
  117. }()
  118. if err = handleCmd(ctx, cfg, cmdArgs, db.Executor(dbExecutor)); err != nil {
  119. if str := err.Error(); str != "" {
  120. text.Errorln(str)
  121. }
  122. exitError := &exec.ExitError{}
  123. if errors.As(err, &exitError) {
  124. // mirror pacman exit code when applicable
  125. ret = exitError.ExitCode()
  126. return
  127. }
  128. // fallback
  129. ret = 1
  130. }
  131. }