download.go 4.4 KB

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