download.go 4.4 KB

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