download.go 6.1 KB

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