main.go 3.7 KB

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