download.go 5.9 KB

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