main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/v11/pkg/db"
  10. "github.com/Jguer/yay/v11/pkg/db/ialpm"
  11. "github.com/Jguer/yay/v11/pkg/query"
  12. "github.com/Jguer/yay/v11/pkg/settings"
  13. "github.com/Jguer/yay/v11/pkg/settings/parser"
  14. "github.com/Jguer/yay/v11/pkg/text"
  15. )
  16. func initGotext() {
  17. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  18. localePath = envLocalePath
  19. }
  20. if lc := os.Getenv("LANGUAGE"); lc != "" {
  21. gotext.Configure(localePath, lc, "yay")
  22. } else if lc := os.Getenv("LC_ALL"); lc != "" {
  23. gotext.Configure(localePath, lc, "yay")
  24. } else if lc := os.Getenv("LC_MESSAGES"); lc != "" {
  25. gotext.Configure(localePath, lc, "yay")
  26. } else {
  27. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  28. }
  29. }
  30. func main() {
  31. var (
  32. err error
  33. ctx = context.Background()
  34. ret = 0
  35. )
  36. defer func() {
  37. if rec := recover(); rec != nil {
  38. text.Errorln(rec)
  39. debug.PrintStack()
  40. }
  41. os.Exit(ret)
  42. }()
  43. initGotext()
  44. if os.Geteuid() == 0 {
  45. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  46. }
  47. config, err = settings.NewConfig(yayVersion)
  48. if err != nil {
  49. if str := err.Error(); str != "" {
  50. text.Errorln(str)
  51. }
  52. ret = 1
  53. return
  54. }
  55. if config.Debug {
  56. text.GlobalLogger.Debug = true
  57. }
  58. if errS := config.RunMigrations(
  59. settings.DefaultMigrations(), config.Runtime.ConfigPath); errS != nil {
  60. text.Errorln(errS)
  61. }
  62. cmdArgs := parser.MakeArguments()
  63. if err = config.ParseCommandLine(cmdArgs); err != nil {
  64. if str := err.Error(); str != "" {
  65. text.Errorln(str)
  66. }
  67. ret = 1
  68. return
  69. }
  70. if config.Runtime.SaveConfig {
  71. if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
  72. text.Errorln(errS)
  73. }
  74. }
  75. if config.SeparateSources {
  76. config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(
  77. config.Runtime.AURClient, config.Runtime.AURCache,
  78. config.Runtime.Logger.Child("querybuilder"), config.SortBy,
  79. config.Runtime.Mode, config.SearchBy, config.BottomUp,
  80. config.SingleLineResults, config.NewInstallEngine)
  81. } else {
  82. config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(
  83. config.Runtime.AURClient, config.Runtime.AURCache,
  84. config.Runtime.Logger.Child("mixed.querybuilder"), config.SortBy,
  85. config.Runtime.Mode, config.SearchBy,
  86. config.BottomUp, config.SingleLineResults, config.NewInstallEngine)
  87. }
  88. var useColor bool
  89. config.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, config.PacmanConf)
  90. if err != nil {
  91. if str := err.Error(); str != "" {
  92. text.Errorln(str)
  93. }
  94. ret = 1
  95. return
  96. }
  97. config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
  98. text.UseColor = useColor
  99. dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
  100. if err != nil {
  101. if str := err.Error(); str != "" {
  102. text.Errorln(str)
  103. }
  104. ret = 1
  105. return
  106. }
  107. defer func() {
  108. if rec := recover(); rec != nil {
  109. text.Errorln(rec)
  110. debug.PrintStack()
  111. }
  112. dbExecutor.Cleanup()
  113. }()
  114. if err = handleCmd(ctx, config, cmdArgs, db.Executor(dbExecutor)); err != nil {
  115. if str := err.Error(); str != "" {
  116. text.Errorln(str)
  117. }
  118. exitError := &exec.ExitError{}
  119. if errors.As(err, &exitError) {
  120. // mirror pacman exit code when applicable
  121. ret = exitError.ExitCode()
  122. return
  123. }
  124. // fallback
  125. ret = 1
  126. }
  127. }