download.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. missing := false
  97. wd, err := os.Getwd()
  98. if err != nil {
  99. return err
  100. }
  101. pkgs = removeInvalidTargets(pkgs)
  102. aur, repo, err := packageSlices(pkgs)
  103. if len(repo) > 0 {
  104. missing, err = getPkgbuildsfromABS(repo, wd)
  105. if err != nil {
  106. return err
  107. }
  108. }
  109. if len(aur) > 0 {
  110. _missing, err := getPkgbuildsfromAUR(aur, wd)
  111. if err != nil {
  112. return err
  113. }
  114. missing = missing || _missing
  115. }
  116. if missing {
  117. err = fmt.Errorf("")
  118. }
  119. return err
  120. }
  121. // GetPkgbuild downloads pkgbuild from the ABS.
  122. func getPkgbuildsfromABS(pkgs []string, path string) (missing bool, err error) {
  123. dbList, err := alpmHandle.SyncDbs()
  124. if err != nil {
  125. return
  126. }
  127. nextPkg:
  128. for _, pkgN := range pkgs {
  129. pkgDb, name := splitDbFromName(pkgN)
  130. for _, db := range dbList.Slice() {
  131. if pkgDb != "" && db.Name() != pkgDb {
  132. continue
  133. }
  134. pkg, err := db.PkgByName(name)
  135. if err == nil {
  136. var url string
  137. name := pkg.Base()
  138. if name == "" {
  139. name = pkg.Name()
  140. }
  141. if _, err := os.Stat(filepath.Join(path, name)); err == nil {
  142. fmt.Println(bold(red(arrow)), bold(cyan(name)), "directory already exists")
  143. continue nextPkg
  144. }
  145. switch db.Name() {
  146. case "core", "extra":
  147. url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
  148. case "community", "multilib":
  149. url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
  150. default:
  151. fmt.Println(pkgN, "not in standard repositories")
  152. continue nextPkg
  153. }
  154. errD := downloadAndUnpack(url, cacheHome)
  155. if errD != nil {
  156. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  157. }
  158. errD = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
  159. if errD != nil {
  160. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(errD.Error())))
  161. } else {
  162. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.Name()), "from ABS")
  163. }
  164. continue nextPkg
  165. }
  166. }
  167. fmt.Println(pkgN, "could not find package in database")
  168. missing = true
  169. }
  170. if _, err := os.Stat(filepath.Join(cacheHome, "packages")); err == nil {
  171. os.RemoveAll(filepath.Join(cacheHome, "packages"))
  172. }
  173. return
  174. }
  175. // GetPkgbuild downloads pkgbuild from the AUR.
  176. func getPkgbuildsfromAUR(pkgs []string, dir string) (bool, error) {
  177. missing := false
  178. strippedPkgs := make([]string, 0)
  179. for _, pkg := range pkgs {
  180. _, name := splitDbFromName(pkg)
  181. strippedPkgs = append(strippedPkgs, name)
  182. }
  183. aq, err := aurInfoPrint(strippedPkgs)
  184. if err != nil {
  185. return missing, err
  186. }
  187. for _, pkg := range aq {
  188. if _, err := os.Stat(filepath.Join(dir, pkg.PackageBase)); err == nil {
  189. fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name)), "directory already exists")
  190. continue
  191. }
  192. if shouldUseGit(filepath.Join(dir, pkg.PackageBase)) {
  193. err = gitDownload(baseURL+"/"+pkg.PackageBase+".git", dir, pkg.PackageBase)
  194. } else {
  195. err = downloadAndUnpack(baseURL+aq[0].URLPath, dir)
  196. }
  197. if err != nil {
  198. fmt.Println(err)
  199. } else {
  200. fmt.Println(bold(yellow(arrow)), "Downloaded", cyan(pkg.PackageBase), "from AUR")
  201. }
  202. }
  203. if len(aq) != len(pkgs) {
  204. missing = true
  205. }
  206. return missing, err
  207. }