download.go 6.0 KB

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