download.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 err == nil || 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 gitGetHash(path string, name string) (string, error) {
  41. stdout, stderr, err := passToGitCapture(filepath.Join(path, name), "rev-parse", "HEAD")
  42. if err != nil {
  43. return "", fmt.Errorf("%s%s", stderr, err)
  44. }
  45. return strings.TrimSpace(stdout), nil
  46. }
  47. func gitDownload(url string, path string, name string) error {
  48. _, err := os.Stat(filepath.Join(path, name, ".git"))
  49. if os.IsNotExist(err) {
  50. err = passToGit(path, "clone", url, name)
  51. if err != nil {
  52. return fmt.Errorf("error cloning %s", name)
  53. }
  54. return nil
  55. } else if err != nil {
  56. return fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
  57. }
  58. err = passToGit(filepath.Join(path, name), "fetch")
  59. if err != nil {
  60. return fmt.Errorf("error fetching %s", name)
  61. }
  62. err = passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD")
  63. if err != nil {
  64. return fmt.Errorf("error resetting %s", name)
  65. }
  66. err = passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff")
  67. if err != nil {
  68. return fmt.Errorf("error merging %s", name)
  69. }
  70. return nil
  71. }
  72. func gitDiff(path string, name string) error {
  73. err := passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}")
  74. return err
  75. }
  76. // DownloadAndUnpack downloads url tgz and extracts to path.
  77. func downloadAndUnpack(url string, path string) (err error) {
  78. err = os.MkdirAll(path, 0755)
  79. if err != nil {
  80. return
  81. }
  82. fileName := filepath.Base(url)
  83. tarLocation := filepath.Join(path, fileName)
  84. defer os.Remove(tarLocation)
  85. err = downloadFile(tarLocation, url)
  86. if err != nil {
  87. return
  88. }
  89. err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
  90. if err != nil {
  91. return
  92. }
  93. return
  94. }
  95. func getPkgbuilds(pkgs []string) error {
  96. //possibleAurs := make([]string, 0, 0)
  97. wd, err := os.Getwd()
  98. if err != nil {
  99. return err
  100. }
  101. missing, err := getPkgbuildsfromABS(pkgs, wd)
  102. if err != nil {
  103. return err
  104. }
  105. err = getPkgbuildsfromAUR(missing, wd)
  106. return err
  107. }
  108. // GetPkgbuild downloads pkgbuild from the ABS.
  109. func getPkgbuildsfromABS(pkgs []string, path string) (missing []string, err error) {
  110. dbList, err := alpmHandle.SyncDbs()
  111. if err != nil {
  112. return
  113. }
  114. nextPkg:
  115. for _, pkgN := range pkgs {
  116. for _, db := range dbList.Slice() {
  117. pkg, err := db.PkgByName(pkgN)
  118. if err == nil {
  119. var url string
  120. name := pkg.Base()
  121. if name == "" {
  122. name = pkg.Name()
  123. }
  124. if _, err := os.Stat(filepath.Join(path, name)); err == nil {
  125. fmt.Println(bold(red(arrow)), bold(cyan(name)), "directory already exists")
  126. continue nextPkg
  127. }
  128. switch db.Name() {
  129. case "core", "extra":
  130. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  131. case "community", "multilib":
  132. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  133. default:
  134. fmt.Println(name + " not in standard repositories")
  135. continue nextPkg
  136. }
  137. errD := downloadAndUnpack(url, cacheHome)
  138. if errD != nil {
  139. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  140. }
  141. errD = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
  142. if errD != nil {
  143. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  144. } else {
  145. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.Name()), "from ABS")
  146. }
  147. continue nextPkg
  148. }
  149. }
  150. missing = append(missing, pkgN)
  151. }
  152. if _, err := os.Stat(filepath.Join(cacheHome, "packages")); err == nil {
  153. os.RemoveAll(filepath.Join(cacheHome, "packages"))
  154. }
  155. return
  156. }
  157. // GetPkgbuild downloads pkgbuild from the AUR.
  158. func getPkgbuildsfromAUR(pkgs []string, dir string) (err error) {
  159. aq, err := aurInfoPrint(pkgs)
  160. if err != nil {
  161. return err
  162. }
  163. for _, pkg := range aq {
  164. var err error
  165. if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
  166. err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
  167. } else {
  168. err = downloadAndUnpack(baseURL+aq[0].URLPath, dir)
  169. }
  170. if err != nil {
  171. fmt.Println(err)
  172. } else {
  173. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.PackageBase), "from AUR")
  174. }
  175. }
  176. if len(aq) != len(pkgs) {
  177. return fmt.Errorf("Could not find all required packages")
  178. }
  179. return
  180. }