main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.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, config.SortBy,
  84. config.Runtime.Mode, config.SearchBy,
  85. config.BottomUp, config.SingleLineResults, config.NewInstallEngine)
  86. }
  87. var useColor bool
  88. config.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, config.PacmanConf)
  89. if err != nil {
  90. if str := err.Error(); str != "" {
  91. text.Errorln(str)
  92. }
  93. ret = 1
  94. return
  95. }
  96. config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
  97. text.UseColor = useColor
  98. dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
  99. if err != nil {
  100. if str := err.Error(); str != "" {
  101. text.Errorln(str)
  102. }
  103. ret = 1
  104. return
  105. }
  106. defer func() {
  107. if rec := recover(); rec != nil {
  108. text.Errorln(rec)
  109. debug.PrintStack()
  110. }
  111. dbExecutor.Cleanup()
  112. }()
  113. if err = handleCmd(ctx, config, cmdArgs, db.Executor(dbExecutor)); err != nil {
  114. if str := err.Error(); str != "" {
  115. text.Errorln(str)
  116. }
  117. exitError := &exec.ExitError{}
  118. if errors.As(err, &exitError) {
  119. // mirror pacman exit code when applicable
  120. ret = exitError.ExitCode()
  121. return
  122. }
  123. // fallback
  124. ret = 1
  125. }
  126. }