main.go 3.5 KB

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