download.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. )
  10. // Decide what download method to use:
  11. // Use the config option when the destination does not already exits
  12. // If .git exists in the destination uer git
  13. // Otherwise use a tarrball
  14. func shouldUseGit(path string) bool {
  15. _, err := os.Stat(path)
  16. if os.IsNotExist(err) {
  17. return config.GitClone
  18. }
  19. _, err = os.Stat(filepath.Join(path, ".git"))
  20. return os.IsExist(err)
  21. }
  22. func downloadFile(path string, url string) (err error) {
  23. // Create the file
  24. out, err := os.Create(path)
  25. if err != nil {
  26. return err
  27. }
  28. defer out.Close()
  29. // Get the data
  30. resp, err := http.Get(url)
  31. if err != nil {
  32. return err
  33. }
  34. defer resp.Body.Close()
  35. // Writer the body to file
  36. _, err = io.Copy(out, resp.Body)
  37. return err
  38. }
  39. func gitDownload(url string, path string, name string) error {
  40. _, err := os.Stat(filepath.Join(path, name, ".git"))
  41. if os.IsNotExist(err) {
  42. err = passToGit(path, "clone", url, name)
  43. if err != nil {
  44. return fmt.Errorf("error cloning %s", name)
  45. }
  46. return nil
  47. } else if err != nil {
  48. return fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  49. }
  50. err = passToGit(filepath.Join(path, name), "fetch")
  51. if err != nil {
  52. return fmt.Errorf("error fetching %s", name)
  53. }
  54. err = passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD")
  55. if err != nil {
  56. return fmt.Errorf("error resetting %s", name)
  57. }
  58. err = passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff")
  59. if err != nil {
  60. return fmt.Errorf("error merging %s", name)
  61. }
  62. return nil
  63. }
  64. // DownloadAndUnpack downloads url tgz and extracts to path.
  65. func downloadAndUnpack(url string, path string) (err error) {
  66. err = os.MkdirAll(path, 0755)
  67. if err != nil {
  68. return
  69. }
  70. fileName := filepath.Base(url)
  71. tarLocation := filepath.Join(path, fileName)
  72. defer os.Remove(tarLocation)
  73. err = downloadFile(tarLocation, url)
  74. if err != nil {
  75. return
  76. }
  77. err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
  78. if err != nil {
  79. return
  80. }
  81. return
  82. }
  83. func getPkgbuilds(pkgs []string) error {
  84. //possibleAurs := make([]string, 0, 0)
  85. wd, err := os.Getwd()
  86. if err != nil {
  87. return err
  88. }
  89. missing, err := getPkgbuildsfromABS(pkgs, wd)
  90. if err != nil {
  91. return err
  92. }
  93. err = getPkgbuildsfromAUR(missing, wd)
  94. return err
  95. }
  96. // GetPkgbuild downloads pkgbuild from the ABS.
  97. func getPkgbuildsfromABS(pkgs []string, path string) (missing []string, err error) {
  98. dbList, err := alpmHandle.SyncDbs()
  99. if err != nil {
  100. return
  101. }
  102. nextPkg:
  103. for _, pkgN := range pkgs {
  104. for _, db := range dbList.Slice() {
  105. pkg, err := db.PkgByName(pkgN)
  106. if err == nil {
  107. var url string
  108. name := pkg.Base()
  109. if name == "" {
  110. name = pkg.Name()
  111. }
  112. if _, err := os.Stat(filepath.Join(path, name)); err == nil {
  113. fmt.Println(bold(red(arrow)), bold(cyan(name)), "directory already exists")
  114. continue nextPkg
  115. }
  116. switch db.Name() {
  117. case "core", "extra":
  118. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  119. case "community", "multilib":
  120. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  121. default:
  122. fmt.Println(name + " not in standard repositories")
  123. continue nextPkg
  124. }
  125. errD := downloadAndUnpack(url, cacheHome)
  126. if errD != nil {
  127. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  128. }
  129. errD = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
  130. if errD != nil {
  131. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  132. } else {
  133. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.Name()), "from ABS")
  134. }
  135. continue nextPkg
  136. }
  137. }
  138. missing = append(missing, pkgN)
  139. }
  140. if _, err := os.Stat(filepath.Join(cacheHome, "packages")); err == nil {
  141. os.RemoveAll(filepath.Join(cacheHome, "packages"))
  142. }
  143. return
  144. }
  145. // GetPkgbuild downloads pkgbuild from the AUR.
  146. func getPkgbuildsfromAUR(pkgs []string, dir string) (err error) {
  147. aq, err := aurInfoPrint(pkgs)
  148. if err != nil {
  149. return err
  150. }
  151. for _, pkg := range aq {
  152. var err error
  153. if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
  154. err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
  155. } else {
  156. err = downloadAndUnpack(baseURL+aq[0].URLPath, dir)
  157. }
  158. if err != nil {
  159. fmt.Println(err)
  160. } else {
  161. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.PackageBase), "from AUR")
  162. }
  163. }
  164. if len(aq) != len(pkgs) {
  165. return fmt.Errorf("Could not find all required packages")
  166. }
  167. return
  168. }