main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "golang.org/x/crypto/ssh/terminal"
  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/text"
  14. )
  15. func initGotext() {
  16. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  17. localePath = envLocalePath
  18. }
  19. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  20. }
  21. func initConfig(configPath string) error {
  22. cfile, err := os.Open(configPath)
  23. if !os.IsNotExist(err) && err != nil {
  24. return errors.New(gotext.Get("failed to open config file '%s': %s", configPath, err))
  25. }
  26. defer cfile.Close()
  27. if !os.IsNotExist(err) {
  28. decoder := json.NewDecoder(cfile)
  29. if err = decoder.Decode(&config); err != nil {
  30. return errors.New(gotext.Get("failed to read config file '%s': %s", configPath, err))
  31. }
  32. }
  33. aurdest := os.Getenv("AURDEST")
  34. if aurdest != "" {
  35. config.BuildDir = aurdest
  36. }
  37. return nil
  38. }
  39. func initVCS(vcsFilePath string) error {
  40. vfile, err := os.Open(vcsFilePath)
  41. if !os.IsNotExist(err) && err != nil {
  42. return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFilePath, err))
  43. }
  44. defer vfile.Close()
  45. if !os.IsNotExist(err) {
  46. decoder := json.NewDecoder(vfile)
  47. if err = decoder.Decode(&savedInfo); err != nil {
  48. return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFilePath, err))
  49. }
  50. }
  51. return nil
  52. }
  53. func initBuildDir() error {
  54. if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
  55. if err = os.MkdirAll(config.BuildDir, 0o755); err != nil {
  56. return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
  57. }
  58. } else if err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. func initAlpm(cmdArgs *settings.Arguments, pacmanConfigPath string) (*pacmanconf.Config, bool, error) {
  64. root := "/"
  65. if value, _, exists := cmdArgs.GetArg("root", "r"); exists {
  66. root = value
  67. }
  68. pacmanConf, stderr, err := pacmanconf.PacmanConf("--config", pacmanConfigPath, "--root", root)
  69. if err != nil {
  70. return nil, false, fmt.Errorf("%s", stderr)
  71. }
  72. if dbPath, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
  73. pacmanConf.DBPath = dbPath
  74. }
  75. if arch, _, exists := cmdArgs.GetArg("arch"); exists {
  76. pacmanConf.Architecture = arch
  77. }
  78. if ignoreArray := cmdArgs.GetArgs("ignore"); ignoreArray != nil {
  79. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, ignoreArray...)
  80. }
  81. if ignoreGroupsArray := cmdArgs.GetArgs("ignoregroup"); ignoreGroupsArray != nil {
  82. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, ignoreGroupsArray...)
  83. }
  84. if cacheArray := cmdArgs.GetArgs("cachedir"); cacheArray != nil {
  85. pacmanConf.CacheDir = cacheArray
  86. }
  87. if gpgDir, _, exists := cmdArgs.GetArg("gpgdir"); exists {
  88. pacmanConf.GPGDir = gpgDir
  89. }
  90. useColor := pacmanConf.Color && terminal.IsTerminal(int(os.Stdout.Fd()))
  91. switch value, _, _ := cmdArgs.GetArg("color"); value {
  92. case "always":
  93. useColor = true
  94. case "auto":
  95. useColor = terminal.IsTerminal(int(os.Stdout.Fd()))
  96. case "never":
  97. useColor = false
  98. }
  99. return pacmanConf, useColor, nil
  100. }
  101. func main() {
  102. ret := 0
  103. defer func() { os.Exit(ret) }()
  104. initGotext()
  105. if os.Geteuid() == 0 {
  106. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  107. }
  108. cmdArgs := settings.MakeArguments()
  109. runtime, err := settings.MakeRuntime()
  110. if err != nil {
  111. if str := err.Error(); str != "" {
  112. fmt.Fprintln(os.Stderr, str)
  113. }
  114. ret = 1
  115. return
  116. }
  117. config = settings.MakeConfig()
  118. config.Runtime = runtime
  119. err = initConfig(runtime.ConfigPath)
  120. if err != nil {
  121. if str := err.Error(); str != "" {
  122. fmt.Fprintln(os.Stderr, str)
  123. }
  124. ret = 1
  125. return
  126. }
  127. err = cmdArgs.ParseCommandLine(config)
  128. if err != nil {
  129. if str := err.Error(); str != "" {
  130. fmt.Fprintln(os.Stderr, str)
  131. }
  132. ret = 1
  133. return
  134. }
  135. if config.Runtime.SaveConfig {
  136. errS := config.SaveConfig(runtime.ConfigPath)
  137. if errS != nil {
  138. fmt.Fprintln(os.Stderr, err)
  139. }
  140. }
  141. config.ExpandEnv()
  142. err = initBuildDir()
  143. if err != nil {
  144. if str := err.Error(); str != "" {
  145. fmt.Fprintln(os.Stderr, str)
  146. }
  147. ret = 1
  148. return
  149. }
  150. err = initVCS(runtime.VCSPath)
  151. if err != nil {
  152. if str := err.Error(); str != "" {
  153. fmt.Fprintln(os.Stderr, str)
  154. }
  155. ret = 1
  156. return
  157. }
  158. var useColor bool
  159. config.Runtime.PacmanConf, useColor, err = initAlpm(cmdArgs, config.PacmanConf)
  160. if err != nil {
  161. if str := err.Error(); str != "" {
  162. fmt.Fprintln(os.Stderr, str)
  163. }
  164. ret = 1
  165. return
  166. }
  167. text.UseColor = useColor
  168. dbExecutor, err := ialpm.NewExecutor(runtime.PacmanConf)
  169. if err != nil {
  170. if str := err.Error(); str != "" {
  171. fmt.Fprintln(os.Stderr, str)
  172. }
  173. ret = 1
  174. return
  175. }
  176. defer dbExecutor.Cleanup()
  177. err = handleCmd(cmdArgs, db.Executor(dbExecutor))
  178. if err != nil {
  179. if str := err.Error(); str != "" {
  180. fmt.Fprintln(os.Stderr, str)
  181. }
  182. ret = 1
  183. return
  184. }
  185. }