123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- package main
- import (
- "fmt"
- "io"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
- alpm "github.com/jguer/go-alpm"
- )
- // Decide what download method to use:
- // Use the config option when the destination does not already exits
- // If .git exists in the destination uer git
- // Otherwise use a tarrball
- func shouldUseGit(path string) bool {
- _, err := os.Stat(path)
- if os.IsNotExist(err) {
- return config.GitClone
- }
- _, err = os.Stat(filepath.Join(path, ".git"))
- return err == nil || os.IsExist(err)
- }
- func downloadFile(path string, url string) (err error) {
- // Create the file
- out, err := os.Create(path)
- if err != nil {
- return err
- }
- defer out.Close()
- // Get the data
- resp, err := http.Get(url)
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- // Writer the body to file
- _, err = io.Copy(out, resp.Body)
- return err
- }
- func gitHasDiff(path string, name string) (bool, error) {
- stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
- if err != nil {
- return false, fmt.Errorf("%s%s", stderr, err)
- }
- lines := strings.Split(stdout, "\n")
- head := lines[0]
- upstream := lines[1]
- return head != upstream, nil
- }
- func gitDownload(url string, path string, name string) (bool, error) {
- _, err := os.Stat(filepath.Join(path, name, ".git"))
- if os.IsNotExist(err) {
- cmd := passToGit(path, "clone", "--no-progress", url, name)
- cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
- _, stderr, err := capture(cmd)
- if err != nil {
- return false, fmt.Errorf("error cloning %s: stderr", name, stderr)
- }
- return true, nil
- } else if err != nil {
- return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
- }
- cmd := passToGit(filepath.Join(path, name), "fetch")
- cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
- _, stderr, err := capture(cmd)
- if err != nil {
- return false, fmt.Errorf("error fetching %s: %s", name, stderr)
- }
- return false, nil
- }
- func gitMerge(path string, name string) error {
- _, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
- if err != nil {
- return fmt.Errorf("error resetting %s: %s", name, stderr)
- }
- _, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
- if err != nil {
- return fmt.Errorf("error merging %s: %s", name, stderr)
- }
- return nil
- }
- func gitDiff(path string, name string) error {
- err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
- return err
- }
- // DownloadAndUnpack downloads url tgz and extracts to path.
- func downloadAndUnpack(url string, path string) (err error) {
- err = os.MkdirAll(path, 0755)
- if err != nil {
- return
- }
- fileName := filepath.Base(url)
- tarLocation := filepath.Join(path, fileName)
- defer os.Remove(tarLocation)
- err = downloadFile(tarLocation, url)
- if err != nil {
- return
- }
- err = exec.Command(config.TarBin, "-xf", tarLocation, "-C", path).Run()
- if err != nil {
- return
- }
- return
- }
- func getPkgbuilds(pkgs []string) error {
- missing := false
- wd, err := os.Getwd()
- if err != nil {
- return err
- }
- pkgs = removeInvalidTargets(pkgs)
- aur, repo, err := packageSlices(pkgs)
- for n := range aur {
- _, pkg := splitDbFromName(aur[n])
- aur[n] = pkg
- }
- info, err := aurInfoPrint(aur)
- if err != nil {
- return err
- }
- if len(repo) > 0 {
- missing, err = getPkgbuildsfromABS(repo, wd)
- if err != nil {
- return err
- }
- }
- if len(aur) > 0 {
- bases := getBases(info)
- toSkip := pkgbuildsToSkip(bases, nil)
- if _, err = downloadPkgbuilds(bases, toSkip, wd); err != nil {
- return err
- }
- missing = missing || len(aur) != len(info)
- }
- if missing {
- err = fmt.Errorf("")
- }
- return err
- }
- // GetPkgbuild downloads pkgbuild from the ABS.
- func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
- missing := false
- dbList, err := alpmHandle.SyncDbs()
- if err != nil {
- return missing, err
- }
- for n, pkgN := range pkgs {
- pkgDb, name := splitDbFromName(pkgN)
- var pkg *alpm.Package
- var err error
- var url string
- if pkgDb != "" {
- if db, err := alpmHandle.SyncDbByName(pkgDb); err == nil {
- pkg, err = db.PkgByName(name)
- }
- } else {
- dbList.ForEach(func(db alpm.Db) error {
- if pkg, err = db.PkgByName(name); err == nil {
- return fmt.Errorf("")
- }
- return nil
- })
- }
- if pkg == nil {
- fmt.Println(name, "could not find package in database")
- missing = true
- continue
- }
- name = pkg.Base()
- if name == "" {
- name = pkg.Name()
- }
- if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
- fmt.Println(err)
- continue
- }
- switch pkg.DB().Name() {
- case "core", "extra", "testing":
- url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
- case "community", "multilib", "community-testing", "multilib-testing":
- url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
- default:
- fmt.Println(name, "not in standard repositories")
- continue
- }
- if err = downloadAndUnpack(url, cacheHome); err != nil {
- fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(err.Error())))
- }
- err = exec.Command("mv", filepath.Join(cacheHome, "packages", name, "trunk"), filepath.Join(path, name)).Run()
- if err != nil {
- fmt.Println(bold(red(arrow)), bold(cyan(pkg.Name())), bold(red(err.Error())))
- } else {
- fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", n+1, len(pkgs), cyan(pkg.Name()))
- }
- }
- err = os.RemoveAll(filepath.Join(cacheHome, "packages"))
- return missing, err
- }
|