main.go 4.7 KB

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