main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "os"
  7. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  8. "github.com/leonelquinteros/gotext"
  9. "github.com/Jguer/yay/v10/pkg/db"
  10. "github.com/Jguer/yay/v10/pkg/db/ialpm"
  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) (*pacmanconf.Config, bool, 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, false, 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. useColor := pacmanConf.Color && isTty()
  90. switch value, _, _ := cmdArgs.GetArg("color"); value {
  91. case "always":
  92. useColor = true
  93. case "auto":
  94. useColor = isTty()
  95. case "never":
  96. useColor = false
  97. }
  98. return pacmanConf, useColor, nil
  99. }
  100. func main() {
  101. ret := 0
  102. defer func() { os.Exit(ret) }()
  103. initGotext()
  104. if os.Geteuid() == 0 {
  105. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  106. }
  107. cmdArgs := settings.MakeArguments()
  108. runtime, err := settings.MakeRuntime()
  109. if err != nil {
  110. if str := err.Error(); str != "" {
  111. fmt.Fprintln(os.Stderr, str)
  112. }
  113. ret = 1
  114. return
  115. }
  116. config = settings.MakeConfig()
  117. config.Runtime = runtime
  118. err = initConfig(runtime.ConfigPath)
  119. if err != nil {
  120. if str := err.Error(); str != "" {
  121. fmt.Fprintln(os.Stderr, str)
  122. }
  123. ret = 1
  124. return
  125. }
  126. err = cmdArgs.ParseCommandLine(config)
  127. if err != nil {
  128. if str := err.Error(); str != "" {
  129. fmt.Fprintln(os.Stderr, str)
  130. }
  131. ret = 1
  132. return
  133. }
  134. if config.Runtime.SaveConfig {
  135. errS := config.SaveConfig(runtime.ConfigPath)
  136. if errS != nil {
  137. fmt.Fprintln(os.Stderr, err)
  138. }
  139. }
  140. config.ExpandEnv()
  141. err = initBuildDir()
  142. if err != nil {
  143. if str := err.Error(); str != "" {
  144. fmt.Fprintln(os.Stderr, str)
  145. }
  146. ret = 1
  147. return
  148. }
  149. err = initVCS(runtime.VCSPath)
  150. if err != nil {
  151. if str := err.Error(); str != "" {
  152. fmt.Fprintln(os.Stderr, str)
  153. }
  154. ret = 1
  155. return
  156. }
  157. var useColor bool
  158. config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
  159. if err != nil {
  160. if str := err.Error(); str != "" {
  161. fmt.Fprintln(os.Stderr, str)
  162. }
  163. ret = 1
  164. return
  165. }
  166. text.UseColor = useColor
  167. dbExecutor, err := ialpm.NewExecutor(runtime.PacmanConf)
  168. if err != nil {
  169. if str := err.Error(); str != "" {
  170. fmt.Fprintln(os.Stderr, str)
  171. }
  172. ret = 1
  173. return
  174. }
  175. defer dbExecutor.Cleanup()
  176. err = handleCmd(cmdArgs, db.Executor(dbExecutor))
  177. if err != nil {
  178. if str := err.Error(); str != "" {
  179. fmt.Fprintln(os.Stderr, str)
  180. }
  181. ret = 1
  182. return
  183. }
  184. }