main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "strings"
  8. alpm "github.com/Jguer/go-alpm"
  9. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  10. "github.com/leonelquinteros/gotext"
  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, 0755); 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(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 value, _, exists := cmdArgs.GetArg("dbpath", "b"); exists {
  72. pacmanConf.DBPath = value
  73. }
  74. if value, _, exists := cmdArgs.GetArg("arch"); exists {
  75. pacmanConf.Architecture = value
  76. }
  77. if value, _, exists := cmdArgs.GetArg("ignore"); exists {
  78. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, strings.Split(value, ",")...)
  79. }
  80. if value, _, exists := cmdArgs.GetArg("ignoregroup"); exists {
  81. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, strings.Split(value, ",")...)
  82. }
  83. // TODO
  84. // current system does not allow duplicate arguments
  85. // but pacman allows multiple cachedirs to be passed
  86. // for now only handle one cache dir
  87. if value, _, exists := cmdArgs.GetArg("cachedir"); exists {
  88. pacmanConf.CacheDir = []string{value}
  89. }
  90. if value, _, exists := cmdArgs.GetArg("gpgdir"); exists {
  91. pacmanConf.GPGDir = value
  92. }
  93. alpmHandle, err := initAlpmHandle(pacmanConf, nil)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. switch value, _, _ := cmdArgs.GetArg("color"); value {
  98. case "always":
  99. text.UseColor = true
  100. case "auto":
  101. text.UseColor = isTty()
  102. case "never":
  103. text.UseColor = false
  104. default:
  105. text.UseColor = pacmanConf.Color && isTty()
  106. }
  107. return alpmHandle, pacmanConf, nil
  108. }
  109. func initAlpmHandle(pacmanConf *pacmanconf.Config, oldAlpmHandle *alpm.Handle) (*alpm.Handle, error) {
  110. if oldAlpmHandle != nil {
  111. if errRelease := oldAlpmHandle.Release(); errRelease != nil {
  112. return nil, errRelease
  113. }
  114. }
  115. alpmHandle, err := alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath)
  116. if err != nil {
  117. return nil, errors.New(gotext.Get("unable to CreateHandle: %s", err))
  118. }
  119. if err := configureAlpm(pacmanConf, alpmHandle); err != nil {
  120. return nil, err
  121. }
  122. alpmHandle.SetQuestionCallback(questionCallback)
  123. alpmHandle.SetLogCallback(logCallback)
  124. return alpmHandle, nil
  125. }
  126. func exitOnError(err error) {
  127. if err != nil {
  128. if str := err.Error(); str != "" {
  129. fmt.Fprintln(os.Stderr, str)
  130. }
  131. cleanup(config.Runtime.AlpmHandle)
  132. os.Exit(1)
  133. }
  134. }
  135. func cleanup(alpmHandle *alpm.Handle) int {
  136. if alpmHandle != nil {
  137. if err := alpmHandle.Release(); err != nil {
  138. fmt.Fprintln(os.Stderr, err)
  139. return 1
  140. }
  141. }
  142. return 0
  143. }
  144. func main() {
  145. initGotext()
  146. if os.Geteuid() == 0 {
  147. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  148. }
  149. runtime, err := settings.MakeRuntime()
  150. exitOnError(err)
  151. config = settings.MakeConfig()
  152. config.Runtime = runtime
  153. exitOnError(initConfig(runtime.ConfigPath))
  154. exitOnError(cmdArgs.ParseCommandLine(config))
  155. if config.Runtime.SaveConfig {
  156. errS := config.SaveConfig(runtime.ConfigPath)
  157. if errS != nil {
  158. fmt.Fprintln(os.Stderr, err)
  159. }
  160. }
  161. config.ExpandEnv()
  162. exitOnError(initBuildDir())
  163. exitOnError(initVCS(runtime.VCSPath))
  164. config.Runtime.AlpmHandle, config.Runtime.PacmanConf, err = initAlpm(config.PacmanConf)
  165. exitOnError(err)
  166. exitOnError(handleCmd(config.Runtime.AlpmHandle))
  167. os.Exit(cleanup(config.Runtime.AlpmHandle))
  168. }