main.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. err = config.ParseCommandLine(cmdArgs)
  86. if err != nil {
  87. if str := err.Error(); str != "" {
  88. fmt.Fprintln(os.Stderr, str)
  89. }
  90. ret = 1
  91. return
  92. }
  93. if config.Runtime.SaveConfig {
  94. if errS := config.Save(config.Runtime.ConfigPath); errS != nil {
  95. fmt.Fprintln(os.Stderr, err)
  96. }
  97. }
  98. var useColor bool
  99. config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
  100. if err != nil {
  101. if str := err.Error(); str != "" {
  102. fmt.Fprintln(os.Stderr, str)
  103. }
  104. ret = 1
  105. return
  106. }
  107. config.Runtime.CmdBuilder.SetPacmanDBPath(config.Runtime.PacmanConf.DBPath)
  108. text.UseColor = useColor
  109. dbExecutor, err := ialpm.NewExecutor(config.Runtime.PacmanConf)
  110. if err != nil {
  111. if str := err.Error(); str != "" {
  112. fmt.Fprintln(os.Stderr, str)
  113. }
  114. ret = 1
  115. return
  116. }
  117. defer dbExecutor.Cleanup()
  118. err = handleCmd(cmdArgs, db.Executor(dbExecutor))
  119. if err != nil {
  120. if str := err.Error(); str != "" && !strings.Contains(str, "exit status") {
  121. fmt.Fprintln(os.Stderr, str)
  122. }
  123. if exitError, ok := err.(*exec.ExitError); ok {
  124. // mirror pacman exit code when applicable
  125. ret = exitError.ExitCode()
  126. return
  127. }
  128. // fallback
  129. ret = 1
  130. return
  131. }
  132. }