main.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "os"
  7. alpm "github.com/Jguer/go-alpm"
  8. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  9. "github.com/leonelquinteros/gotext"
  10. "github.com/Jguer/yay/v10/pkg/db"
  11. "github.com/Jguer/yay/v10/pkg/settings"
  12. "github.com/Jguer/yay/v10/pkg/text"
  13. )
  14. func initGotext() {
  15. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  16. localePath = envLocalePath
  17. }
  18. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  19. }
  20. func initConfig(configPath string) error {
  21. cfile, err := os.Open(configPath)
  22. if !os.IsNotExist(err) && err != nil {
  23. return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
  24. }
  25. defer cfile.Close()
  26. if !os.IsNotExist(err) {
  27. decoder := json.NewDecoder(cfile)
  28. if err = decoder.Decode(&config); err != nil {
  29. return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
  30. }
  31. }
  32. aurdest := os.Getenv("AURDEST")
  33. if aurdest != "" {
  34. config.BuildDir = aurdest
  35. }
  36. return nil
  37. }
  38. func initVCS(vcsFilePath string) error {
  39. vfile, err := os.Open(vcsFilePath)
  40. if !os.IsNotExist(err) && err != nil {
  41. return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
  42. }
  43. defer vfile.Close()
  44. if !os.IsNotExist(err) {
  45. decoder := json.NewDecoder(vfile)
  46. if err = decoder.Decode(&savedInfo); err != nil {
  47. return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
  48. }
  49. }
  50. return nil
  51. }
  52. func initBuildDir() error {
  53. if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
  54. if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
  55. return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
  56. }
  57. } else if err != nil {
  58. return err
  59. }
  60. return nil
  61. }
  62. func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*alpm.Handle, *pacmanconf.Config, error) {
  63. root := "/"
  64. if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
  65. root = value
  66. }
  67. pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
  68. if err != nil {
  69. return nil, nil, fmt.Errorf("%s", stderr)
  70. }
  71. if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
  72. pacmanConf.DBPath = dbPath
  73. }
  74. if arch, _, exists := cmdArgs.GetArg("arch"); exists {
  75. pacmanConf.Architecture = arch
  76. }
  77. if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
  78. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
  79. }
  80. if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
  81. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
  82. }
  83. if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
  84. pacmanConf.CacheDir = cacheArray
  85. }
  86. if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
  87. pacmanConf.GPGDir = gpgDir
  88. }
  89. alpmHandle, err := initAlpmHandle(pacmanConf, nil)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. switch value, _, _ := cmdArgs.GetArg("color"); value {
  94. case "always":
  95. text.UseColor = true
  96. case "auto":
  97. text.UseColor = isTty()
  98. case "never":
  99. text.UseColor = false
  100. default:
  101. text.UseColor = pacmanConf.Color && isTty()
  102. }
  103. return alpmHandle, pacmanConf, nil
  104. }
  105. func initAlpmHandle(pacmanConf *pacmanconf.Config, oldAlpmHandle *alpm.Handle) (*alpm.Handle, error) {
  106. if oldAlpmHandle != nil {
  107. if errRelease := oldAlpmHandle.Release(); errRelease != nil {
  108. return nil, errRelease
  109. }
  110. }
  111. alpmHandle, err := alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath)
  112. if err != nil {
  113. return nil, errors.New(gotext.Get("unable to CreateHandle: %s", err))
  114. }
  115. if err := configureAlpm(pacmanConf, alpmHandle); err != nil {
  116. return nil, err
  117. }
  118. alpmHandle.SetQuestionCallback(questionCallback)
  119. return alpmHandle, nil
  120. }
  121. func exitOnError(err error) {
  122. if err != nil {
  123. if str := err.Error(); str != "" {
  124. fmt.Fprintln(os.Stderr, str)
  125. }
  126. cleanup(config.Runtime.AlpmHandle)
  127. os.Exit(1)
  128. }
  129. }
  130. func cleanup(alpmHandle *alpm.Handle) int {
  131. if alpmHandle != nil {
  132. if err := alpmHandle.Release(); err != nil {
  133. fmt.Fprintln(os.Stderr, err)
  134. return 1
  135. }
  136. }
  137. return 0
  138. }
  139. func main() {
  140. initGotext()
  141. if os.Geteuid() == 0 {
  142. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  143. }
  144. cmdArgs := settings.MakeArguments()
  145. runtime, err := settings.MakeRuntime()
  146. exitOnError(err)
  147. config = settings.MakeConfig()
  148. config.Runtime = runtime
  149. exitOnError(initConfig(runtime.ConfigPath))
  150. exitOnError(cmdArgs.ParseCommandLine(config))
  151. if config.Runtime.SaveConfig {
  152. errS := config.SaveConfig(runtime.ConfigPath)
  153. if errS != nil {
  154. fmt.Fprintln(os.Stderr, err)
  155. }
  156. }
  157. config.ExpandEnv()
  158. exitOnError(initBuildDir())
  159. exitOnError(initVCS(runtime.VCSPath))
  160. config.Runtime.AlpmHandle, config.Runtime.PacmanConf, err = initAlpm(cmdArgs, config.PacmanConf)
  161. exitOnError(err)
  162. config.Runtime.DBExecutor, err = db.NewAlpmExecutor(config.Runtime.AlpmHandle, runtime.PacmanConf, questionCallback)
  163. exitOnError(err)
  164. exitOnError(handleCmd(cmdArgs, config.Runtime.AlpmHandle))
  165. os.Exit(cleanup(config.Runtime.AlpmHandle))
  166. }