main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. alpm "github.com/jguer/go-alpm"
  9. )
  10. func setPaths() error {
  11. if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
  12. configHome = filepath.Join(configHome, "yay")
  13. } else if configHome = os.Getenv("HOME"); configHome != "" {
  14. configHome = filepath.Join(configHome, ".config/yay")
  15. } else {
  16. return fmt.Errorf("XDG_CONFIG_HOME and HOME unset")
  17. }
  18. if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
  19. cacheHome = filepath.Join(cacheHome, "yay")
  20. } else if cacheHome = os.Getenv("HOME"); cacheHome != "" {
  21. cacheHome = filepath.Join(cacheHome, ".cache/yay")
  22. } else {
  23. return fmt.Errorf("XDG_CACHE_HOME and HOME unset")
  24. }
  25. configFile = filepath.Join(configHome, configFileName)
  26. vcsFile = filepath.Join(cacheHome, vcsFileName)
  27. return nil
  28. }
  29. func initConfig() error {
  30. cfile, err := os.Open(configFile)
  31. if !os.IsNotExist(err) && err != nil {
  32. return fmt.Errorf("Failed to open config file '%s': %s", configFile, err)
  33. }
  34. defer cfile.Close()
  35. if !os.IsNotExist(err) {
  36. decoder := json.NewDecoder(cfile)
  37. if err = decoder.Decode(&config); err != nil {
  38. return fmt.Errorf("Failed to read config '%s': %s", configFile, err)
  39. }
  40. }
  41. return nil
  42. }
  43. func initVCS() error {
  44. vfile, err := os.Open(vcsFile)
  45. if !os.IsNotExist(err) && err != nil {
  46. return fmt.Errorf("Failed to open vcs file '%s': %s", vcsFile, err)
  47. }
  48. defer vfile.Close()
  49. if !os.IsNotExist(err) {
  50. decoder := json.NewDecoder(vfile)
  51. if err = decoder.Decode(&savedInfo); err != nil {
  52. return fmt.Errorf("Failed to read vcs '%s': %s", vcsFile, err)
  53. }
  54. }
  55. return nil
  56. }
  57. func initHomeDirs() error {
  58. if _, err := os.Stat(configHome); os.IsNotExist(err) {
  59. if err = os.MkdirAll(configHome, 0755); err != nil {
  60. return fmt.Errorf("Failed to create config directory '%s': %s", configHome, err)
  61. }
  62. } else if err != nil {
  63. return err
  64. }
  65. if _, err := os.Stat(cacheHome); os.IsNotExist(err) {
  66. if err = os.MkdirAll(cacheHome, 0755); err != nil {
  67. return fmt.Errorf("Failed to create cache directory '%s': %s", cacheHome, err)
  68. }
  69. } else if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. func initBuildDir() error {
  75. if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
  76. if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
  77. return fmt.Errorf("Failed to create BuildDir directory '%s': %s", config.BuildDir, err)
  78. }
  79. } else if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func initAlpm() error {
  85. var err error
  86. if alpmConf, err = readAlpmConfig(config.PacmanConf); err != nil {
  87. return fmt.Errorf("Unable to read Pacman conf: %s", err)
  88. }
  89. if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
  90. alpmConf.DBPath = value
  91. }
  92. if value, _, exists := cmdArgs.getArg("root", "r"); exists {
  93. alpmConf.RootDir = value
  94. }
  95. if value, _, exists := cmdArgs.getArg("arch"); exists {
  96. alpmConf.Architecture = value
  97. }
  98. if value, _, exists := cmdArgs.getArg("ignore"); exists {
  99. alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
  100. }
  101. if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
  102. alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
  103. }
  104. //TODO
  105. //current system does not allow duplicate arguments
  106. //but pacman allows multiple cachdirs to be passed
  107. //for now only handle one cache dir
  108. if value, _, exists := cmdArgs.getArg("cachdir"); exists {
  109. alpmConf.CacheDir = []string{value}
  110. }
  111. if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
  112. alpmConf.GPGDir = value
  113. }
  114. if err = initAlpmHandle(); err != nil {
  115. return err
  116. }
  117. if value, _, _ := cmdArgs.getArg("color"); value == "always" || value == "auto" {
  118. useColor = true
  119. } else if value == "never" {
  120. useColor = false
  121. } else {
  122. useColor = alpmConf.Options&alpm.ConfColor > 0
  123. }
  124. return nil
  125. }
  126. func initAlpmHandle() error {
  127. var err error
  128. if alpmHandle != nil {
  129. if err := alpmHandle.Release(); err != nil {
  130. return err
  131. }
  132. }
  133. if alpmHandle, err = alpmConf.CreateHandle(); err != nil {
  134. return fmt.Errorf("Unable to CreateHandle: %s", err)
  135. }
  136. alpmHandle.SetQuestionCallback(questionCallback)
  137. alpmHandle.SetLogCallback(logCallback)
  138. return nil
  139. }
  140. func exitOnError(err error) {
  141. if err != nil {
  142. if str := err.Error(); str != "" {
  143. fmt.Println(str)
  144. }
  145. cleanup()
  146. os.Exit(1)
  147. }
  148. }
  149. func cleanup() int {
  150. if alpmHandle != nil {
  151. if err := alpmHandle.Release(); err != nil {
  152. fmt.Println(err)
  153. return 1
  154. }
  155. }
  156. return 0
  157. }
  158. func main() {
  159. if 0 == os.Geteuid() {
  160. fmt.Println("Please avoid running yay as root/sudo.")
  161. }
  162. exitOnError(setPaths())
  163. defaultSettings(&config)
  164. exitOnError(initHomeDirs())
  165. exitOnError(initConfig())
  166. exitOnError(cmdArgs.parseCommandLine())
  167. exitOnError(initBuildDir())
  168. exitOnError(initVCS())
  169. exitOnError(initAlpm())
  170. exitOnError(handleCmd())
  171. os.Exit(cleanup())
  172. }