main.go 3.6 KB

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