main.go 4.3 KB

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