main.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package main // import "github.com/Jguer/yay"
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. alpm "github.com/Jguer/go-alpm"
  10. pacmanconf "github.com/Morganamilo/go-pacmanconf"
  11. "github.com/leonelquinteros/gotext"
  12. "github.com/Jguer/yay/v9/pkg/text"
  13. )
  14. func setPaths() error {
  15. if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
  16. configHome = filepath.Join(configHome, "yay")
  17. } else if configHome = os.Getenv("HOME"); configHome != "" {
  18. configHome = filepath.Join(configHome, ".config/yay")
  19. } else {
  20. return errors.New(gotext.Get("%s and %s unset", "XDG_CONFIG_HOME", "HOME"))
  21. }
  22. if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
  23. cacheHome = filepath.Join(cacheHome, "yay")
  24. } else if cacheHome = os.Getenv("HOME"); cacheHome != "" {
  25. cacheHome = filepath.Join(cacheHome, ".cache/yay")
  26. } else {
  27. return errors.New(gotext.Get("%s and %s unset", "XDG_CACHE_HOME", "HOME"))
  28. }
  29. configFile = filepath.Join(configHome, configFileName)
  30. vcsFile = filepath.Join(cacheHome, vcsFileName)
  31. return nil
  32. }
  33. func initGotext() {
  34. if envLocalePath := os.Getenv("LOCALE_PATH"); envLocalePath != "" {
  35. localePath = envLocalePath
  36. }
  37. gotext.Configure(localePath, os.Getenv("LANG"), "yay")
  38. }
  39. func initConfig() error {
  40. cfile, err := os.Open(configFile)
  41. if !os.IsNotExist(err) && err != nil {
  42. return errors.New(gotext.Get("failed to open config file '%s': %s", configFile, err))
  43. }
  44. defer cfile.Close()
  45. if !os.IsNotExist(err) {
  46. decoder := json.NewDecoder(cfile)
  47. if err = decoder.Decode(&config); err != nil {
  48. return errors.New(gotext.Get("failed to read config file '%s': %s", configFile, err))
  49. }
  50. }
  51. aurdest := os.Getenv("AURDEST")
  52. if aurdest != "" {
  53. config.BuildDir = aurdest
  54. }
  55. return nil
  56. }
  57. func initVCS() error {
  58. vfile, err := os.Open(vcsFile)
  59. if !os.IsNotExist(err) && err != nil {
  60. return errors.New(gotext.Get("failed to open vcs file '%s': %s", vcsFile, err))
  61. }
  62. defer vfile.Close()
  63. if !os.IsNotExist(err) {
  64. decoder := json.NewDecoder(vfile)
  65. if err = decoder.Decode(&savedInfo); err != nil {
  66. return errors.New(gotext.Get("failed to read vcs file '%s': %s", vcsFile, err))
  67. }
  68. }
  69. return nil
  70. }
  71. func initHomeDirs() error {
  72. if _, err := os.Stat(configHome); os.IsNotExist(err) {
  73. if err = os.MkdirAll(configHome, 0755); err != nil {
  74. return errors.New(gotext.Get("failed to create config directory '%s': %s", configHome, err))
  75. }
  76. } else if err != nil {
  77. return err
  78. }
  79. if _, err := os.Stat(cacheHome); os.IsNotExist(err) {
  80. if err = os.MkdirAll(cacheHome, 0755); err != nil {
  81. return errors.New(gotext.Get("failed to create cache directory '%s': %s", cacheHome, err))
  82. }
  83. } else if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. func initBuildDir() error {
  89. if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
  90. if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
  91. return errors.New(gotext.Get("failed to create BuildDir directory '%s': %s", config.BuildDir, err))
  92. }
  93. } else if err != nil {
  94. return err
  95. }
  96. return nil
  97. }
  98. func initAlpm() error {
  99. var err error
  100. var stderr string
  101. root := "/"
  102. if value, _, exists := cmdArgs.getArg("root", "r"); exists {
  103. root = value
  104. }
  105. pacmanConf, stderr, err = pacmanconf.PacmanConf("--config", config.PacmanConf, "--root", root)
  106. if err != nil {
  107. return fmt.Errorf("%s", stderr)
  108. }
  109. if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
  110. pacmanConf.DBPath = value
  111. }
  112. if value, _, exists := cmdArgs.getArg("arch"); exists {
  113. pacmanConf.Architecture = value
  114. }
  115. if value, _, exists := cmdArgs.getArg("ignore"); exists {
  116. pacmanConf.IgnorePkg = append(pacmanConf.IgnorePkg, strings.Split(value, ",")...)
  117. }
  118. if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
  119. pacmanConf.IgnoreGroup = append(pacmanConf.IgnoreGroup, strings.Split(value, ",")...)
  120. }
  121. // TODO
  122. // current system does not allow duplicate arguments
  123. // but pacman allows multiple cachedirs to be passed
  124. // for now only handle one cache dir
  125. if value, _, exists := cmdArgs.getArg("cachedir"); exists {
  126. pacmanConf.CacheDir = []string{value}
  127. }
  128. if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
  129. pacmanConf.GPGDir = value
  130. }
  131. if err := initAlpmHandle(); err != nil {
  132. return err
  133. }
  134. switch value, _, _ := cmdArgs.getArg("color"); value {
  135. case "always":
  136. useColor = true
  137. case "auto":
  138. useColor = isTty()
  139. case "never":
  140. useColor = false
  141. default:
  142. useColor = pacmanConf.Color && isTty()
  143. }
  144. return nil
  145. }
  146. func initAlpmHandle() error {
  147. if alpmHandle != nil {
  148. if errRelease := alpmHandle.Release(); errRelease != nil {
  149. return errRelease
  150. }
  151. }
  152. var err error
  153. if alpmHandle, err = alpm.Initialize(pacmanConf.RootDir, pacmanConf.DBPath); err != nil {
  154. return errors.New(gotext.Get("unable to CreateHandle: %s", err))
  155. }
  156. if err := configureAlpm(); err != nil {
  157. return err
  158. }
  159. alpmHandle.SetQuestionCallback(questionCallback)
  160. alpmHandle.SetLogCallback(logCallback)
  161. return nil
  162. }
  163. func exitOnError(err error) {
  164. if err != nil {
  165. if str := err.Error(); str != "" {
  166. fmt.Fprintln(os.Stderr, str)
  167. }
  168. cleanup()
  169. os.Exit(1)
  170. }
  171. }
  172. func cleanup() int {
  173. if alpmHandle != nil {
  174. if err := alpmHandle.Release(); err != nil {
  175. fmt.Fprintln(os.Stderr, err)
  176. return 1
  177. }
  178. }
  179. return 0
  180. }
  181. func main() {
  182. initGotext()
  183. if os.Geteuid() == 0 {
  184. text.Warnln(gotext.Get("Avoid running yay as root/sudo."))
  185. }
  186. exitOnError(setPaths())
  187. config = defaultSettings()
  188. exitOnError(initHomeDirs())
  189. exitOnError(initConfig())
  190. exitOnError(cmdArgs.parseCommandLine())
  191. if shouldSaveConfig {
  192. err := config.saveConfig()
  193. if err != nil {
  194. fmt.Fprintln(os.Stderr, err)
  195. }
  196. }
  197. config.expandEnv()
  198. exitOnError(initBuildDir())
  199. exitOnError(initVCS())
  200. exitOnError(initAlpm())
  201. exitOnError(handleCmd())
  202. os.Exit(cleanup())
  203. }