main.go 3.1 KB

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