main.go 3.7 KB

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