main.go 3.6 KB

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