main.go 3.2 KB

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