main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "runtime/debug"
  8. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  9. "github.com/leonelquinteros/gotext"
  10. "golang.org/x/term"
  11. "github.com/Jguer/yay/v11/pkg/db"
  12. "github.com/Jguer/yay/v11/pkg/db/ialpm"
  13. "github.com/Jguer/yay/v11/pkg/query"
  14. "github.com/Jguer/yay/v11/pkg/settings"
  15. "github.com/Jguer/yay/v11/pkg/settings/parser"
  16. "github.com/Jguer/yay/v11/pkg/text"
  17. )
  18. func initGotext() {
  19. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  20. localePath = envLocalePath
  21. }
  22. if lc := os.Getenv("LANGUAGE"); lc != "" {
  23. gotext.Configure(localePath, lc, "yay")
  24. } else if lc := os.Getenv("LC_ALL"); lc != "" {
  25. gotext.Configure(localePath, lc, "yay")
  26. } else if lc := os.Getenv("LC_MESSAGES"); lc != "" {
  27. gotext.Configure(localePath, lc, "yay")
  28. } else {
  29. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  30. }
  31. }
  32. func initAlpm(cmdArgs *parser.Arguments, pacmanConfigPath string) (*pacmanconf.Config, bool, error) {
  33. root := "/"
  34. if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
  35. root = value
  36. }
  37. pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
  38. if err != nil {
  39. cmdErr := err
  40. if stderr != "" {
  41. cmdErr = fmt.Errorf("%s\n%s", err, stderr)
  42. }
  43. return nil, false, cmdErr
  44. }
  45. if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
  46. pacmanConf.DBPath = dbPath
  47. }
  48. if arch := cmdArgs.GetArgs("arch"); arch != nil {
  49. pacmanConf.Architecture = append(pacmanConf.Architecture, arch...)
  50. }
  51. if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
  52. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
  53. }
  54. if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
  55. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
  56. }
  57. if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
  58. pacmanConf.CacheDir = cacheArray
  59. }
  60. if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
  61. pacmanConf.GPGDir = gpgDir
  62. }
  63. useColor := pacmanConf.Color && term.IsTerminal(int(os.Stdout.Fd()))
  64. switch value, _, _ := cmdArgs.GetArg("color"); value {
  65. case "always":
  66. useColor = true
  67. case "auto":
  68. useColor = term.IsTerminal(int(os.Stdout.Fd()))
  69. case "never":
  70. useColor = false
  71. }
  72. return pacmanConf, useColor, nil
  73. }
  74. func main() {
  75. var (
  76. err error
  77. ctx = context.Background()
  78. ret = 0
  79. )
  80. defer func() {
  81. if rec := recover(); rec != nil {
  82. text.Errorln(rec)
  83. debug.PrintStack()
  84. }
  85. os.Exit(ret)
  86. }()
  87. initGotext()
  88. if os.Geteuid() == 0 {
  89. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  90. }
  91. config, err = settings.NewConfig(yayVersion)
  92. if err != nil {
  93. if str := err.Error(); str != "" {
  94. text.Errorln(str)
  95. }
  96. ret = 1
  97. return
  98. }
  99. if errS := config.RunMigrations(
  100. settings.DefaultMigrations(), config.Runtime.ConfigPath); errS != nil {
  101. text.Errorln(errS)
  102. }
  103. cmdArgs := parser.MakeArguments()
  104. if err = config.ParseCommandLine(cmdArgs); err != nil {
  105. if str := err.Error(); str != "" {
  106. text.Errorln(str)
  107. }
  108. ret = 1
  109. return
  110. }
  111. if config.Runtime.SaveConfig {
  112. if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
  113. text.Errorln(errS)
  114. }
  115. }
  116. if config.SeparateSources {
  117. config.Runtime.QueryBuilder = query.NewSourceQueryBuilder(config.SortBy,
  118. config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
  119. } else {
  120. config.Runtime.QueryBuilder = query.NewMixedSourceQueryBuilder(config.SortBy,
  121. config.Runtime.Mode, config.SearchBy, config.BottomUp, config.SingleLineResults)
  122. }
  123. var useColor bool
  124. config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
  125. if err != nil {
  126. if str := err.Error(); str != "" {
  127. text.Errorln(str)
  128. }
  129. ret = 1
  130. return
  131. }
  132. config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
  133. text.UseColor = useColor
  134. dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
  135. if err != nil {
  136. if str := err.Error(); str != "" {
  137. text.Errorln(str)
  138. }
  139. ret = 1
  140. return
  141. }
  142. defer func() {
  143. if rec := recover(); rec != nil {
  144. text.Errorln(rec)
  145. debug.PrintStack()
  146. }
  147. dbExecutor.Cleanup()
  148. }()
  149. if err = handleCmd(ctx, cmdArgs, db.Executor(dbExecutor)); err != nil {
  150. if str := err.Error(); str != "" {
  151. text.Errorln(str)
  152. }
  153. if exitError, ok := err.(*exec.ExitError); ok {
  154. // mirror pacman exit code when applicable
  155. ret = exitError.ExitCode()
  156. return
  157. }
  158. // fallback
  159. ret = 1
  160. }
  161. }