main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 initPaths() {
  11. if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
  12. if info, err := os.Stat(configHome); err == nil && info.IsDir() {
  13. configHome = filepath.Join(configHome, "yay")
  14. } else {
  15. configHome = filepath.Join(os.Getenv("HOME"), ".config/yay")
  16. }
  17. } else {
  18. configHome = filepath.Join(os.Getenv("HOME"), ".config/yay")
  19. }
  20. if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
  21. if info, err := os.Stat(cacheHome); err == nil && info.IsDir() {
  22. cacheHome = filepath.Join(cacheHome, "yay")
  23. } else {
  24. cacheHome = filepath.Join(os.Getenv("HOME"), ".cache/yay")
  25. }
  26. } else {
  27. cacheHome = filepath.Join(os.Getenv("HOME"), ".cache/yay")
  28. }
  29. configFile = filepath.Join(configHome, configFileName)
  30. vcsFile = filepath.Join(cacheHome, vcsFileName)
  31. }
  32. func initConfig() (err error) {
  33. defaultSettings(&config)
  34. if _, err = os.Stat(configFile); os.IsNotExist(err) {
  35. err = os.MkdirAll(filepath.Dir(configFile), 0755)
  36. if err != nil {
  37. err = fmt.Errorf("Unable to create config directory:\n%s\n"+
  38. "The error was:\n%s", filepath.Dir(configFile), err)
  39. return
  40. }
  41. // Save the default config if nothing is found
  42. config.saveConfig()
  43. } else {
  44. cfile, errf := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
  45. if errf != nil {
  46. fmt.Printf("Error reading config: %s\n", err)
  47. } else {
  48. defer cfile.Close()
  49. decoder := json.NewDecoder(cfile)
  50. err = decoder.Decode(&config)
  51. if err != nil {
  52. fmt.Println("Loading default Settings.\nError reading config:",
  53. err)
  54. defaultSettings(&config)
  55. }
  56. if _, err = os.Stat(config.BuildDir); os.IsNotExist(err) {
  57. err = os.MkdirAll(config.BuildDir, 0755)
  58. if err != nil {
  59. err = fmt.Errorf("Unable to create BuildDir directory:\n%s\n"+
  60. "The error was:\n%s", config.BuildDir, err)
  61. return
  62. }
  63. }
  64. }
  65. }
  66. return
  67. }
  68. func initVCS() (err error) {
  69. if _, err = os.Stat(vcsFile); os.IsNotExist(err) {
  70. err = os.MkdirAll(filepath.Dir(vcsFile), 0755)
  71. if err != nil {
  72. err = fmt.Errorf("Unable to create vcs directory:\n%s\n"+
  73. "The error was:\n%s", filepath.Dir(configFile), err)
  74. return
  75. }
  76. } else {
  77. vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
  78. if err == nil {
  79. defer vfile.Close()
  80. decoder := json.NewDecoder(vfile)
  81. _ = decoder.Decode(&savedInfo)
  82. }
  83. }
  84. return
  85. }
  86. func initAlpm() (err error) {
  87. var value string
  88. var exists bool
  89. //var double bool
  90. value, _, exists = cmdArgs.getArg("config")
  91. if exists {
  92. config.PacmanConf = value
  93. }
  94. alpmConf, err = readAlpmConfig(config.PacmanConf)
  95. if err != nil {
  96. err = fmt.Errorf("Unable to read Pacman conf: %s", err)
  97. return
  98. }
  99. value, _, exists = cmdArgs.getArg("dbpath", "b")
  100. if exists {
  101. alpmConf.DBPath = value
  102. }
  103. value, _, exists = cmdArgs.getArg("root", "r")
  104. if exists {
  105. alpmConf.RootDir = value
  106. }
  107. value, _, exists = cmdArgs.getArg("arch")
  108. if exists {
  109. alpmConf.Architecture = value
  110. }
  111. value, _, exists = cmdArgs.getArg("ignore")
  112. if exists {
  113. alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
  114. }
  115. value, _, exists = cmdArgs.getArg("ignoregroup")
  116. if exists {
  117. alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
  118. }
  119. //TODO
  120. //current system does not allow duplicate arguments
  121. //but pacman allows multiple cachdirs to be passed
  122. //for now only handle one cache dir
  123. value, _, exists = cmdArgs.getArg("cachdir")
  124. if exists {
  125. alpmConf.CacheDir = []string{value}
  126. }
  127. value, _, exists = cmdArgs.getArg("gpgdir")
  128. if exists {
  129. alpmConf.GPGDir = value
  130. }
  131. alpmHandle, err = alpmConf.CreateHandle()
  132. if err != nil {
  133. err = fmt.Errorf("Unable to CreateHandle: %s", err)
  134. return
  135. }
  136. value, _, _ = cmdArgs.getArg("color")
  137. if value == "always" || value == "auto" {
  138. useColor = true
  139. } else if value == "never" {
  140. useColor = false
  141. } else {
  142. useColor = alpmConf.Options&alpm.ConfColor > 0
  143. }
  144. alpmHandle.SetQuestionCallback(questionCallback)
  145. return
  146. }
  147. func main() {
  148. var status int
  149. var err error
  150. if 0 == os.Geteuid() {
  151. fmt.Println("Please avoid running yay as root/sudo.")
  152. }
  153. err = cmdArgs.parseCommandLine()
  154. if err != nil {
  155. fmt.Println(err)
  156. status = 1
  157. goto cleanup
  158. }
  159. initPaths()
  160. err = initConfig()
  161. if err != nil {
  162. fmt.Println(err)
  163. status = 1
  164. goto cleanup
  165. }
  166. err = initVCS()
  167. if err != nil {
  168. fmt.Println(err)
  169. status = 1
  170. goto cleanup
  171. }
  172. err = initAlpm()
  173. if err != nil {
  174. fmt.Println(err)
  175. status = 1
  176. goto cleanup
  177. }
  178. err = handleCmd()
  179. if err != nil {
  180. if err.Error() != "" {
  181. fmt.Println(err)
  182. }
  183. status = 1
  184. goto cleanup
  185. }
  186. cleanup:
  187. //cleanup
  188. //from here on out don't exit if an error occurs
  189. //if we fail to save the configuration
  190. //at least continue on and try clean up other parts
  191. if alpmHandle != nil {
  192. err = alpmHandle.Release()
  193. if err != nil {
  194. fmt.Println(err)
  195. status = 1
  196. }
  197. }
  198. os.Exit(status)
  199. }