main.go 4.7 KB

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