main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 errS := config.RunMigrations(
  55. settings.DefaultMigrations(), config.Runtime.ConfigPath); errS != nil {
  56. text.Errorln(errS)
  57. }
  58. cmdArgs := parser.MakeArguments()
  59. if err = config.ParseCommandLine(cmdArgs); err != nil {
  60. if str := err.Error(); str != "" {
  61. text.Errorln(str)
  62. }
  63. ret = 1
  64. return
  65. }
  66. if config.Runtime.SaveConfig {
  67. if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
  68. text.Errorln(errS)
  69. }
  70. }
  71. if config.SeparateSources {
  72. config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(config.SortBy,
  73. config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
  74. } else {
  75. config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(config.SortBy,
  76. config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
  77. }
  78. var useColor bool
  79. config.Runtime.PacmanConf, useColor, err = settings.RetrievePacmanConfig(cmdArgs, config.PacmanConf)
  80. if err != nil {
  81. if str := err.Error(); str != "" {
  82. text.Errorln(str)
  83. }
  84. ret = 1
  85. return
  86. }
  87. config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
  88. text.UseColor = useColor
  89. dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
  90. if err != nil {
  91. if str := err.Error(); str != "" {
  92. text.Errorln(str)
  93. }
  94. ret = 1
  95. return
  96. }
  97. defer func() {
  98. if rec := recover(); rec != nil {
  99. text.Errorln(rec)
  100. debug.PrintStack()
  101. }
  102. dbExecutor.Cleanup()
  103. }()
  104. if err = handleCmd(ctx, cmdArgs, db.Executor(dbExecutor)); err != nil {
  105. if str := err.Error(); str != "" {
  106. text.Errorln(str)
  107. }
  108. if exitError, ok := err.(*exec.ExitError); ok {
  109. // mirror pacman exit code when applicable
  110. ret = exitError.ExitCode()
  111. return
  112. }
  113. // fallback
  114. ret = 1
  115. }
  116. }