main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/settings"
  11. "github.com/Jguer/yay/v10/pkg/text"
  12. )
  13. func initGotext() {
  14. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  15. localePath = envLocalePath
  16. }
  17. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  18. }
  19. func initConfig(configPath string) error {
  20. cfile, err := os.Open(configPath)
  21. if !os.IsNotExist(err) && err != nil {
  22. return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
  23. }
  24. defer cfile.Close()
  25. if !os.IsNotExist(err) {
  26. decoder := json.NewDecoder(cfile)
  27. if err = decoder.Decode(&config); err != nil {
  28. return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
  29. }
  30. }
  31. aurdest := os.Getenv("AURDEST")
  32. if aurdest != "" {
  33. config.BuildDir = aurdest
  34. }
  35. return nil
  36. }
  37. func initVCS(vcsFilePath string) error {
  38. vfile, err := os.Open(vcsFilePath)
  39. if !os.IsNotExist(err) && err != nil {
  40. return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
  41. }
  42. defer vfile.Close()
  43. if !os.IsNotExist(err) {
  44. decoder := json.NewDecoder(vfile)
  45. if err = decoder.Decode(&savedInfo); err != nil {
  46. return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
  47. }
  48. }
  49. return nil
  50. }
  51. func initBuildDir() error {
  52. if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
  53. if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
  54. return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
  55. }
  56. } else if err != nil {
  57. return err
  58. }
  59. return nil
  60. }
  61. func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*alpm.Handle, *pacmanconf.Config, error) {
  62. root := "/"
  63. if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
  64. root = value
  65. }
  66. pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
  67. if err != nil {
  68. return nil, nil, fmt.Errorf("%s", stderr)
  69. }
  70. if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
  71. pacmanConf.DBPath = dbPath
  72. }
  73. if arch, _, exists := cmdArgs.GetArg("arch"); exists {
  74. pacmanConf.Architecture = arch
  75. }
  76. if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
  77. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
  78. }
  79. if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
  80. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
  81. }
  82. if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
  83. pacmanConf.CacheDir = cacheArray
  84. }
  85. if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
  86. pacmanConf.GPGDir = gpgDir
  87. }
  88. alpmHandle, err := initAlpmHandle(pacmanConf, nil)
  89. if err != nil {
  90. return nil, nil, err
  91. }
  92. switch value, _, _ := cmdArgs.GetArg("color"); value {
  93. case "always":
  94. text.UseColor = true
  95. case "auto":
  96. text.UseColor = isTty()
  97. case "never":
  98. text.UseColor = false
  99. default:
  100. text.UseColor = pacmanConf.Color && isTty()
  101. }
  102. return alpmHandle, pacmanConf, nil
  103. }
  104. func initAlpmHandle(pacmanConf *pacmanconf.Config, oldAlpmHandle *alpm.Handle) (*alpm.Handle, error) {
  105. if oldAlpmHandle != nil {
  106. if errRelease := oldAlpmHandle.Release(); errRelease != nil {
  107. return nil, errRelease
  108. }
  109. }
  110. alpmHandle, err := alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath)
  111. if err != nil {
  112. return nil, errors.New(gotext.Get("unable to CreateHandle: %s", err))
  113. }
  114. if err := configureAlpm(pacmanConf, alpmHandle); err != nil {
  115. return nil, err
  116. }
  117. alpmHandle.SetQuestionCallback(questionCallback)
  118. alpmHandle.SetLogCallback(logCallback)
  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. exitOnError(handleCmd(cmdArgs, config.Runtime.AlpmHandle))
  163. os.Exit(cleanup(config.Runtime.AlpmHandle))
  164. }